// Gold.h : 此文件包含 "Gold" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GOLD_H #define GOLD_H #include <iostream> using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// 黄金 /// </summary> class Gold { public: /// <summary> /// 镶嵌材料名称 黄金 /// </summary> virtual void getName() = 0; /// <summary> /// 虚构 /// </summary> virtual ~Gold() {} }; } #endif // Jade.h : 此文件包含 "Jade" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef JADE_H #define JADE_H #include <iostream> using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// 玉 /// </summary> class Jade { public: /// <summary> /// 镶嵌材料名称 玉 /// </summary> virtual void getName() = 0; /// <summary> /// 虚构 /// </summary> virtual ~Jade() {} }; } #endif // Pearl.h : 此文件包含 "Pearl" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef PEARL_H #define PEARL_H #include <iostream> using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// 珍珠 /// </summary> class Pearl { public: /// <summary> /// 镶嵌材料名称 珍珠 /// </summary> virtual void getName() = 0; /// <summary> /// 虚构 /// </summary> virtual ~Pearl() {} }; } #endif // Diamond.h : 此文件包含 "Diamond" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef DIAMOND_H #define DIAMOND_H #include <iostream> using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// 钻石 /// </summary> class Diamond { public: /// <summary> /// 镶嵌材料名称 钻石 /// </summary> virtual void getName() = 0; /// <summary> /// 虚构 /// </summary> virtual ~Diamond() {} }; } #endif // GoldChina.h : 此文件包含 "GoldChina" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GOLDCHINA_H #define GOLDCHINA_H #include <iostream> #include "Gold.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// /// </summary> class GoldChina:public Gold { public: /// <summary> /// /// </summary> virtual void getName() { cout << "中国厂商产的黄金材料" << endl; } }; } #endif // JadeChina.h : 此文件包含 "JadeChina" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef JADECHINA_H #define JADECHINA_H #include <iostream> #include "Jade.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// /// </summary> class JadeChina:public Jade { public: /// <summary> /// /// </summary> virtual void getName() { cout << "中国厂商产的玉材料" << endl; } }; } #endif // PearlChina.h : 此文件包含 "PearlChina" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef PEARLCHINA_H #define PEARLCHINA_H #include <iostream> #include "Pearl.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// /// </summary> class PearlChina:public Pearl { public: /// <summary> /// /// </summary> virtual void getName() { cout << "中国厂商产的珍珠材料" << endl; } }; } #endif // DiamondChina.h : 此文件包含 "DiamondChina" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef DIAMONDCHINA_H #define DIAMONDCHINA_H #include <iostream> #include "Diamond.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// /// </summary> class DiamondChina:public Diamond { public: /// <summary> /// /// </summary> virtual void getName() { cout << "中国厂商产的钻石材料" << endl; } }; } #endif
// GoldChina.h : 此文件包含 "GoldChina" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GOLDCAMBODIA_H #define GOLDCAMBODIA_H #include <iostream> #include "Gold.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// /// </summary> class GoldCambodia :public Gold { public: virtual void getName() { cout << "柬埔寨厂商产的黄金材料" << endl; } }; } #endif #pragma once #ifndef JADECAMBODIA_H #define JADECAMBODIA_H #include <iostream> #include "Jade.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// /// </summary> class JadeCambodia :public Jade { public: virtual void getName() { cout << "柬埔寨厂商产的玉材料" << endl; } }; } #endif // PearlChina.h : 此文件包含 "PearlChina" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef PEARLCAMBODIA_H #define PEARLCAMBODIA_H #include <iostream> #include "Pearl.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// /// </summary> class PearlCambodia :public Pearl { public: virtual void getName() { cout << "柬埔寨厂商产的珍珠材料" << endl; } }; } #endif // DiamondChina.h : 此文件包含 "DiamondChina" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef DIAMONDCAMBODIA_H #define DIAMONDCAMBODIA_H #include <iostream> #include "Diamond.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// /// </summary> class DiamondCambodia :public Diamond { public: virtual void getName() { cout << "柬埔寨厂商产的钻石材料" << endl; } }; } #endif // GoldAmerica.h : 此文件包含 "GoldAmerica" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GOLDAMERICA_H #define GOLDAMERICA_H #include <iostream> #include "Gold.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// /// </summary> class GoldAmerica :public Gold { public: /// <summary> /// /// </summary> virtual void getName() { cout << "美国厂商产的黄金材料" << endl; } }; } #endif // JadeAmerica.h : 此文件包含 "JadeAmerica" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #ifndef JADEAMERICA_H #define JADEAMERICA_H #include <iostream> #include "Jade.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// /// </summary> class JadeAmerica :public Jade { public: /// <summary> /// /// </summary> virtual void getName() { cout << "美国厂商产的玉材料" << endl; } }; } #endif // PearlAmerica.h : 此文件包含 "PearlAmerica" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef PEARLAMERICA_H #define PEARLAMERICA_H #include <iostream> #include "Pearl.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// /// </summary> class PearlAmerica :public Pearl { public: /// <summary> /// /// </summary> virtual void getName() { cout << "美国厂商产的珍珠材料" << endl; } }; } #endif // DiamondChina.h : 此文件包含 "DiamondChina" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef DIAMONDAMERICA_H #define DIAMONDAMERICA_H #include <iostream> #include "Diamond.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// /// </summary> class DiamondAmerica :public Diamond { public: /// <summary> /// /// </summary> virtual void getName() { cout << "美国厂商产的钻石材料" << endl; } }; } #endif
// Diamond.h : 此文件包含 "Diamond" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef JEWELRYCO_H #define JEWELRYCO_H #include <iostream> #include "Diamond.h" #include "Gold.h" #include "Jade.h" #include "Pearl.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// /// </summary> class JewelryCo { public: /// <summary> /// 构造函数 /// </summary> /// <param name="tmpgold"></param> /// <param name="tmpjade"></param> /// <param name="tmpperal"></param> /// <param name="tmpdiamond"></param> JewelryCo(Gold* tmpgold, Jade* tmpjade, Pearl* tmpperal, Diamond* tmpdiamond) { gold = tmpgold; jade = tmpjade; peral = tmpperal; diamond = tmpdiamond; } /// <summary> /// /// </summary> void Assemble() //组装珠宝首饰工厂 { cout << "镶嵌公司产品:成功组装了一个珠宝首饰(材料来源):" << endl; gold->getName(); jade->getName(); peral->getName(); diamond->getName(); } private: Gold* gold; Jade* jade; Pearl* peral; Diamond* diamond; }; } #endif
// JewelryAbstractFactory.h : 此文件包含 "JewelryAbstractFactory" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef JEWELRYABSTRACTFACTORY_H #define JEWELRYABSTRACTFACTORY_H #include <iostream> #include "Diamond.h" #include "Gold.h" #include "Jade.h" #include "Pearl.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// 抽象工厂类 /// </summary> class JewelryAbstractFactory { public: //所创建的部件应该稳定的保持这三个部件,才适合抽象工厂模式 /// <summary> /// 创建 黄金 /// </summary> /// <returns></returns> virtual Gold* createGold() = 0; /// <summary> /// 创建 钻石 /// </summary> /// <returns></returns> virtual Diamond* createDiamond() = 0; /// <summary> /// 创建 玉 /// </summary> /// <returns></returns> virtual Jade* createJade() = 0; /// <summary> /// 创建 珍珠 /// </summary> /// <returns></returns> virtual Pearl* createPearl() = 0; /// <summary> /// /// </summary> virtual ~JewelryAbstractFactory() {} }; } #endif // JewelryChinaFactory.h : 此文件包含 "JewelryChinaFactory" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef JEWELRYCHINAFACTORY_H #define JEWELRYCHINAFACTORY_H #include <iostream> #include "Diamond.h" #include "Gold.h" #include "Jade.h" #include "Pearl.h" #include "DiamondChina.h" #include "GoldChina.h" #include "JadeChina.h" #include "PearlChina.h" #include "JewelryAbstractFactory.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// 创建一个中国工厂 /// </summary> class JewelryChinaFactory:public JewelryAbstractFactory { public: /// <summary> /// /// </summary> /// <returns></returns> virtual Gold* createGold() { return new GoldChina(); } /// <summary> /// /// </summary> /// <returns></returns> virtual Jade* createJade() { return new JadeChina(); } /// <summary> /// /// </summary> /// <returns></returns> virtual Pearl* createPearl() { return new PearlChina(); } /// <summary> /// /// </summary> /// <returns></returns> virtual Diamond* createDiamond() { return new DiamondChina(); } }; } #endif // JewelryCambodiaFactory.h : 此文件包含 "JewelryCambodiaFactory" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef JEWELRYACAMBODIAFACTORY_H #define JEWELRYCAMBODIAFACTORY_H #include <iostream> #include "Diamond.h" #include "Gold.h" #include "Jade.h" #include "Pearl.h" #include "DiamondCambodia.h" #include "GoldCambodia.h" #include "JadeCambodia.h" #include "PearlCambodia.h" #include "JewelryAbstractFactory.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// 创建一个柬埔寨工厂 /// </summary> class JewelryCambodiaFactory :public JewelryAbstractFactory { public: /// <summary> /// /// </summary> /// <returns></returns> virtual Gold* createGold() { return new GoldCambodia(); } /// <summary> /// /// </summary> /// <returns></returns> virtual Jade* createJade() { return new JadeCambodia(); } /// <summary> /// /// </summary> /// <returns></returns> virtual Pearl* createPearl() { return new PearlCambodia(); } /// <summary> /// /// </summary> /// <returns></returns> virtual Diamond* createDiamond() { return new DiamondCambodia(); } }; } #endif // JewelryChinaFactory.h : 此文件包含 "JewelryChinaFactory" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef JEWELRYAMERICAFACTORY_H #define JEWELRYAMERICAFACTORY_H #include <iostream> #include "Diamond.h" #include "Gold.h" #include "Jade.h" #include "Pearl.h" #include "DiamondAmerica.h" #include "GoldAmerica.h" #include "JadeAmerica.h" #include "PearlAmerica.h" #include "JewelryAbstractFactory.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// 创建一个美国工厂 /// </summary> class JewelryAmericaFactory :public JewelryAbstractFactory { public: /// <summary> /// /// </summary> /// <returns></returns> virtual Gold* createGold() { return new GoldAmerica(); } /// <summary> /// /// </summary> /// <returns></returns> virtual Jade* createJade() { return new JadeAmerica(); } /// <summary> /// /// </summary> /// <returns></returns> virtual Pearl* createPearl() { return new PearlAmerica(); } /// <summary> /// /// </summary> /// <returns></returns> virtual Diamond* createDiamond() { return new DiamondAmerica(); } }; } #endif
// JewelryChinaFactory.h : 此文件包含 "JewelryChinaFactory" 类。Abstract Factory Pattern C++ 14 // 2023年4月30日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef JEWELRYAMERICAFACTORY_H #define JEWELRYAMERICAFACTORY_H #include <iostream> #include "Diamond.h" #include "Gold.h" #include "Jade.h" #include "Pearl.h" #include "DiamondAmerica.h" #include "GoldAmerica.h" #include "JadeAmerica.h" #include "PearlAmerica.h" #include "JewelryAbstractFactory.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// 创建一个美国工厂 /// </summary> class JewelryAmericaFactory :public JewelryAbstractFactory { public: /// <summary> /// /// </summary> /// <returns></returns> virtual Gold* createGold() { return new GoldAmerica(); } /// <summary> /// /// </summary> /// <returns></returns> virtual Jade* createJade() { return new JadeAmerica(); } /// <summary> /// /// </summary> /// <returns></returns> virtual Pearl* createPearl() { return new PearlAmerica(); } /// <summary> /// /// </summary> /// <returns></returns> virtual Diamond* createDiamond() { return new DiamondAmerica(); } }; } #endif // GeovinDu.cpp : 此文件包含 "GeovinDu" 类。Abstract Factory Pattern C++ 14 // 2023年4月29日 涂聚文 Geovin Du edit. #include "GeovinDu.h" #include "Diamond.h" #include "Gold.h" #include "Jade.h" #include "Pearl.h" #include "DiamondChina.h" #include "GoldChina.h" #include "JadeChina.h" #include "PearlChina.h" #include "DiamondCambodia.h" #include "GoldCambodia.h" #include "JadeCambodia.h" #include "PearlCambodia.h" #include "DiamondAmerica.h" #include "GoldAmerica.h" #include "JadeAmerica.h" #include "PearlAmerica.h" #include "JewelryAbstractFactory.h" #include "JewelryChinaFactory.h" #include "JewelryCambodiaFactory.h" #include "JewelryAmericaFactory.h" #include "JewelryCo.h" using namespace std; namespace DuJewelryAbstractFactoryPattern { /// <summary> /// /// </summary> void GeovinDu::displayAbstractFactory() { //创建第一个珠宝首饰工厂------------------------------------ //(1)创建一个中国工厂 DuJewelryAbstractFactoryPattern::JewelryAbstractFactory* pChinaFactory = new DuJewelryAbstractFactoryPattern::JewelryChinaFactory(); //(2)创建中国产的各种部件 DuJewelryAbstractFactoryPattern::Gold* pChinaGold = pChinaFactory->createGold(); DuJewelryAbstractFactoryPattern::Jade* pChinaJade = pChinaFactory->createJade(); DuJewelryAbstractFactoryPattern::Pearl* pChinaPearl = pChinaFactory->createPearl(); DuJewelryAbstractFactoryPattern::Diamond* pChinaDiamond = pChinaFactory->createDiamond(); //(3)创建珠宝首饰 DuJewelryAbstractFactoryPattern::JewelryCo* pbd1co = new DuJewelryAbstractFactoryPattern::JewelryCo(pChinaGold, pChinaJade, pChinaPearl, pChinaDiamond); pbd1co->Assemble(); //创建第二个珠宝首饰------------------------------------ //(1)创建另外两个工厂:柬埔寨工厂,美国工厂 DuJewelryAbstractFactoryPattern::JewelryAbstractFactory* pCambodiaFactory = new DuJewelryAbstractFactoryPattern::JewelryCambodiaFactory(); DuJewelryAbstractFactoryPattern::JewelryAbstractFactory* pAmericaFactory = new DuJewelryAbstractFactoryPattern::JewelryAmericaFactory(); //(2)创建中国产的部件,柬埔寨产的部件,美国产的部件 DuJewelryAbstractFactoryPattern::Gold* pChinaGold2 = pChinaFactory->createGold(); DuJewelryAbstractFactoryPattern::Jade* pCambodiaJade = pCambodiaFactory->createJade(); DuJewelryAbstractFactoryPattern::Pearl* pAmericaPearl = pAmericaFactory->createPearl(); DuJewelryAbstractFactoryPattern::Diamond* pAmericaDiamond = pAmericaFactory->createDiamond(); //(3)创建珠宝首饰 DuJewelryAbstractFactoryPattern::JewelryCo* pbd2co = new DuJewelryAbstractFactoryPattern::JewelryCo(pChinaGold2, pCambodiaJade, pAmericaPearl, pAmericaDiamond); pbd2co->Assemble(); //组装珠宝首饰 //最后记得释放内存------------------------------------ delete pbd1co; delete pChinaGold; delete pChinaJade; delete pChinaPearl; delete pChinaDiamond; delete pChinaFactory; //----------- delete pbd2co; delete pChinaGold2; delete pCambodiaJade; delete pAmericaPearl; delete pAmericaDiamond; delete pAmericaFactory; delete pCambodiaFactory; } }
调用:
// ConsoleDuAbstractFactoryPattern.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // 抽象工厂模式 Abstract Factory Pattern // GeovinDu.cpp : 此文件包含 "GeovinDu" 类。 Abstract Factory Methon Pattern C++ 14 // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. 文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社 #define _UNICODE #include <iostream> #include "GeovinDu.h" using namespace std; using namespace DuJewelryAbstractFactoryPattern; int main() { std::cout << "Hello World!涂聚文 Geovin Du\n"; GeovinDu geovindu; geovindu.displayAbstractFactory(); system("pause"); return 0; } // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 // 调试程序: F5 或调试 >“开始调试”菜单 // 入门使用技巧: // 1. 使用解决方案资源管理器窗口添加/管理文件 // 2. 使用团队资源管理器窗口连接到源代码管理 // 3. 使用输出窗口查看生成输出和其他消息 // 4. 使用错误列表窗口查看错误 // 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 // 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件 #define UNICODE
输出:
Hello World!涂聚文 Geovin Du 镶嵌公司产品:成功组装了一个珠宝首饰(材料来源): 中国厂商产的黄金材料 中国厂商产的玉材料 中国厂商产的珍珠材料 中国厂商产的钻石材料 镶嵌公司产品:成功组装了一个珠宝首饰(材料来源): 中国厂商产的黄金材料 柬埔寨厂商产的玉材料 美国厂商产的珍珠材料 美国厂商产的钻石材料 请按任意键继续. . .
标签:Diamond,DuJewelryAbstractFactoryPattern,Pattern,Abstract,namespace,virtual,cpp,i From: https://www.cnblogs.com/geovindu/p/17367527.html