当前位置: 首页 > 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();
}

相关文章:

  • 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系统详解
  • 做网站什么字体/厦门做网站公司有哪些
  • 遵义做什么网站好/seo是哪个国家
  • 最友善的建设网站技术/热搜榜排名今日第一
  • 河南郑州建设厅网站/百度app官网下载安装
  • 长春好的做网站公司有哪些/seo sem是什么职位
  • 旅游投资公司网站建设/关键词查询爱站网