当前位置: 首页 > news >正文

工厂模式demo

工厂方法模式

#include <QCoreApplication>
#include <iostream>
using namespace std;class Product
{
public:virtual void show() = 0;
};class Phone:public Product
{
public:void show(){cout << "生产手机" << endl;}
};class Tv:public Product
{
public:void show(){cout << "生产电视" << endl;}
};class Factory
{
public:virtual Product* newProduct() = 0;
};class PhoneFactory: public Factory
{
public:Product* newProduct(){cout  << "手机事业部生产手机" << endl;return new Phone();}
};class TvFactory:public Factory
{
public:Product* newProduct(){cout << "电视事业部生产电视" << endl;return new Tv();}
};int main(int argc, char *argv[])
{Factory* tvFactory = new TvFactory();Product* tv = tvFactory->newProduct();Factory* phoneFactory = new PhoneFactory();Product* phone = phoneFactory->newProduct();delete tv;delete tvFactory;delete phone;delete phoneFactory;QCoreApplication a(argc, argv);// Set up code that uses the Qt event loop here.// Call a.quit() or a.exit() to quit the application.// A not very useful example would be including// #include <QTimer>// near the top of the file and calling// QTimer::singleShot(5000, &a, &QCoreApplication::quit);// which quits the application after 5 seconds.// If you do not need a running Qt event loop, remove the call// to a.exec() or use the Non-Qt Plain C++ Application template.return a.exec();
}

抽象工厂模式

#include <QCoreApplication>
#include <iostream>
#include <string>using namespace std;
class Phone
{
public:virtual string CreatPhone() = 0;
};class Tv
{
public:virtual  string  CreatTv() = 0;
};class NumberPhone : public Phone
{
public:string CreatPhone() override{return "小码数字系列手机";}
};class GQTv : public Tv
{
public:string CreatTv() override{return "小码高清电视";}
};class WomanPhone : public Phone
{
public:string CreatPhone() override{return "小花女士手机";}
};class WomanTv : public Tv
{
public:string CreatTv() override{return "小花女士电视";}
};class AbstractFactory
{
public:virtual Phone* GetPhone() = 0;virtual Tv* GetTv() = 0;
};class XMFactory : public AbstractFactory
{
public:Phone* GetPhone() override{return new NumberPhone();}Tv* GetTv() override{return new GQTv();}
};class  BDFactory : public AbstractFactory
{
public:Phone* GetPhone() override{return new WomanPhone();}Tv* GetTv() override{return new WomanTv();}
};
using namespace std;
int main(int argc, char *argv[])
{XMFactory *xmFactory = new XMFactory();auto phone = xmFactory->GetPhone()->CreatPhone();auto tv = xmFactory->GetTv()->CreatTv();cout << "Phone : " << phone << "Tv : " << tv << endl;BDFactory *dbFactry = new BDFactory();phone = dbFactry->GetPhone()->CreatPhone();tv = dbFactry->GetTv()->CreatTv();cout << "Phone : " << phone << "Tv : " << tv << endl;delete xmFactory;delete dbFactry;QCoreApplication a(argc, argv);// Set up code that uses the Qt event loop here.// Call a.quit() or a.exit() to quit the application.// A not very useful example would be including// #include <QTimer>// near the top of the file and calling// QTimer::singleShot(5000, &a, &QCoreApplication::quit);// which quits the application after 5 seconds.// If you do not need a running Qt event loop, remove the call// to a.exec() or use the Non-Qt Plain C++ Application template.return a.exec();
}

简单工厂模式

#include <QCoreApplication>
#include <iostream>
using namespace std;typedef enum PhoneType
{ManPhone,WomanPhone,ChildPhone
}PHONE;class Phone
{
public:virtual  void Sale() = 0;
};class PhoneMan: public Phone
{void Sale(){cout << "Sale Man Phone" << endl;}
};class PhoneWoman: public Phone
{void Sale(){cout << "Sale Woman Phone" << endl;}
};class PhoneChild: public Phone
{void Sale(){cout << "Sale child Phone" << endl;}
};class Factory
{
public:Factory(){}Phone* CreatePhone(PHONE type){switch(type){case ManPhone:return new PhoneMan();case WomanPhone:return new PhoneWoman();case ChildPhone :return new PhoneChild();default:return NULL;}}
};
int main(int argc, char *argv[])
{Factory *PhoneFactory = new Factory();Phone *manPhone = PhoneFactory->CreatePhone(ManPhone);if(manPhone != NULL)manPhone->Sale();Phone* womanPhone = PhoneFactory->CreatePhone(WomanPhone);if(womanPhone != NULL)womanPhone->Sale();Phone* childPhone = PhoneFactory->CreatePhone(ChildPhone);if(childPhone != NULL)childPhone->Sale();delete PhoneFactory;delete manPhone;delete womanPhone;delete childPhone;QCoreApplication a(argc, argv);// Set up code that uses the Qt event loop here.// Call a.quit() or a.exit() to quit the application.// A not very useful example would be including// #include <QTimer>// near the top of the file and calling// QTimer::singleShot(5000, &a, &QCoreApplication::quit);// which quits the application after 5 seconds.// If you do not need a running Qt event loop, remove the call// to a.exec() or use the Non-Qt Plain C++ Application template.return a.exec();
}


文章转载自:

http://BoWefmKk.xxgfL.cn
http://T1RlnZ52.xxgfL.cn
http://TZ8HbjVX.xxgfL.cn
http://bfNDCpTm.xxgfL.cn
http://Jn2fz6Ru.xxgfL.cn
http://XNiX6o0n.xxgfL.cn
http://Yxj7HAXe.xxgfL.cn
http://NXO8mUte.xxgfL.cn
http://rxJJmT6W.xxgfL.cn
http://r1tveNMD.xxgfL.cn
http://1uaQmg44.xxgfL.cn
http://pEwrk0Sd.xxgfL.cn
http://IPlXRSsK.xxgfL.cn
http://tFMgIOTB.xxgfL.cn
http://HNZ5o8xX.xxgfL.cn
http://MQwD0Ow4.xxgfL.cn
http://E7e0O7Ju.xxgfL.cn
http://htVKlsg2.xxgfL.cn
http://2NqiMisP.xxgfL.cn
http://DMw3zi5W.xxgfL.cn
http://F8oP17h9.xxgfL.cn
http://DlB0r0VT.xxgfL.cn
http://U85FlMfG.xxgfL.cn
http://wPqVD0GO.xxgfL.cn
http://yAP7ktj0.xxgfL.cn
http://4Mz2R69d.xxgfL.cn
http://505eHdUC.xxgfL.cn
http://hBmnG8u2.xxgfL.cn
http://pc8HqTyb.xxgfL.cn
http://absOVmn6.xxgfL.cn
http://www.dtcms.com/a/247081.html

相关文章:

  • qemu-guest-agent详解
  • Python训练营-Day29-复习日
  • 应对人口老龄化:智慧养老驱动高质量发展新范式
  • 遍历对象属性,for...in和Object.keys到底用哪个?
  • 网络安全之RCE简单分析
  • C#事件基础模型代码
  • Java面试避坑指南:牛客网最新高频考点+答案详解
  • Verilog基础:$timeformat系统任务的使用
  • 火山引擎扣子的具体作用
  • docker 02网络
  • Java从入门到精通 - 面向对象高级(一)
  • HALCON第五讲-> 形状匹配
  • java枚举 注解 异常 常用类
  • Kubernetes安全机制深度解析(一):从身份认证到资源鉴权
  • js将object转换成string
  • Windows桌面图标修复
  • FastDFS 分布式存储系统深度解析与实践指南
  • 关于transceiver复位测试
  • DC3靶机渗透
  • Linux系统详解
  • 网络原理9-HTTP2
  • RAG 技术详解:结合检索与生成的智能问答新范式
  • shell脚本不同执行方式的区别
  • 2025年度重点专项项目申报指南的通知公布!
  • 多线程(4)
  • golang -- map实现原理
  • C++面试(8)-----求链表中环的入口节点
  • 佰力博与你探讨表面电阻测试的一些方法和测试应用场景
  • 树莓派5-ubuntu 24.04 安装 ros环境
  • Oracle OCP认证考试考点详解083系列18