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

工厂模式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://www.xdnf.cn/news/13702.html

相关文章:

  • Peiiieee的Linux笔记(1)
  • 基于大模型预测的上睑下垂综合诊疗技术方案
  • 浅析4D-bev标注技术在自动驾驶领域的重要性
  • 数据库更新!万方
  • centos转移mysql的数据存储目录
  • 猎犬:快速 友好的桌面文本搜索软件 支持30+格式与高精度OCR
  • HTTP系列---有状态
  • 在MATLAB命令行执行ros2node 和 ros2subscriber后,执行ros2 topic list,MATLAB卡死
  • 云服务器如何搭建多站点?Nginx多域名部署方案详解 (2025)
  • 中国第七次人口普查100m网格化人口数据集(Tif/分省/分市)
  • 使用 VLC Media Player 轻松提取视频中的音频文件
  • 一分钟部署nginx-公网IP访问内网
  • RED DA认证-EN18031网络安全常见问题以及解答
  • 玛哈特零件矫平机:精密制造中的平整度守护者
  • gRPC 与 JSON 之间的类型映射规则
  • PH热榜 | 2025-06-12
  • odoo CRM中销售管道的自定义与阶段管理
  • 使用Optimization tool优化后禁用windows更新批量的脚本
  • 深入解析Web信息探测与分析技术:网站指纹识别、敏感文件扫描与端口探测实战
  • 《linux2.4内存管理》第 4 章 进程地址空间
  • 执行应用共享内存空间 同步QT进行快速捕获数据流
  • 5.4.1树的存储结构
  • 如何搭建反向海淘代购系统?
  • 服务器数据恢复—重装系统导致XFS文件系统分区无法访问的数据恢复案例
  • 主流Java Redis客户端(Jedis、Lettuce、Redisson)差异对比
  • element-ui table实现默认选中,且不可修改
  • RIFD技术打造智能化实训新模式
  • RAG检索前处理
  • [Java恶补day23] 35. 搜索插入位置
  • GitHub 趋势日报 (2025年06月11日)