2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
The simple factory pattern mainly solves the interface selection problem.
The core part of the Factory is responsible for implementing the internal logic of creating all products. The factory class can be directly called by the outside world to create the required objects.
- // SingletonPattern.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
- //
-
- #include <iostream>
- using namespace std;
-
- //抽象汽车
- class AbstractCar
- {
- public:
- virtual void ShowCarName() = 0;
- };
-
- //具体产品的汽车继承->抽象汽车类,并且对他方法进行重写操作
- //凯美瑞
- class Camry :public AbstractCar
- {
- virtual void ShowCarName()
- {
- cout << "Camry Car." << endl;
- }
- };
- //迈腾
- class Magotan :public AbstractCar
- {
- virtual void ShowCarName()
- {
- cout << "Magotan Car." << endl;
- }
- };
- //奥迪
- class Aodi :public AbstractCar
- {
- virtual void ShowCarName()
- {
- cout << "Aodi Car." << endl;
- }
- };
-
- //奔驰
- class Benz :public AbstractCar
- {
- virtual void ShowCarName()
- {
- cout << "Benz Car." << endl;
- }
- };
-
- //通过传递参数来确定需要生成汽车
- //汽车工厂
- class Carfactory
- {
- public:
- static AbstractCar* CreateCarFunc(string cname)
- {
- if (cname == "Camry")
- //具体生产细节....
- return new Camry;
- else if (cname == "Magotan")
- //具体生产细节....
- return new Magotan;
- else if (cname == "Aodi")
- //具体生产细节....
- return new Aodi;
- else if (cname == "Benz")
- //具体生产细节....
- return new Benz;
- else
- return nullptr;
- }
- };
- int main()
- {
-
- //创建工厂
- Carfactory* fty = new Carfactory;
-
- //创建汽车
-
- AbstractCar* car;
-
- //指定工厂需要创建的汽车
-
- car = fty->CreateCarFunc("Benz");
-
- car->ShowCarName();
-
- delete car;
-
-
- return 0;
-
- }
-
The Abstract Factory Pattern belongs to the creational pattern in the design pattern and is used to build a product family. The abstract factory is the most abstract and general form of all factory patterns. The abstract factory refers to a factory pattern used when there are multiple abstract roles. The abstract factory pattern can provide an interface to the client, allowing the client to create product objects in multiple product families without having to specify the specific product. Each form of the factory pattern is a solution to a certain problem. The factory method is aimed at multiple product series structures; while the abstract factory pattern is aimed at multiple product family structures, and there are multiple product series in a product family.
Provides an interface for creating a series of interdependent objects without specifying their specific classes. Main purpose: Mainly solves the choice of interface problems.
In the abstract factory pattern, the client is no longer responsible for creating objects, but instead passes this responsibility to a specific factory class. The client is only responsible for calling the object, thereby clarifying the responsibilities of each class.
There are four roles in the abstract factory pattern:Abstract Factory Role、Specific factory roles, Abstract Product Role,Specific product roles.
Code Sample
- #include <iostream>
- using namespace std;
-
- // 抽象西瓜
- class AbstractWatermelon {
- public:
- virtual void PrintName() = 0;
- AbstractWatermelon() {
- cout << "抽象西瓜构造函数." << endl;
- }
- ~AbstractWatermelon() {
- cout << "抽象西瓜析构函数." << endl;
- }
- };
-
- // 抽象桔子
- class AbstractOrange {
- public:
- virtual void PrintName() = 0;
- AbstractOrange() {
- cout << "抽象桔子构造函数." << endl;
- }
- ~AbstractOrange() {
- cout << "抽象桔子析构函数." << endl;
- }
- };
-
- // 中国西瓜
- class ChinaWatermelon :public AbstractWatermelon {
- public:
- virtual void PrintName() {
- cout << "中国西瓜最好吃." << endl;
- }
- ChinaWatermelon() {
- cout << "中国西瓜构造函数." << endl;
- }
- ~ChinaWatermelon() {
- cout << "中国西瓜析构函数." << endl;
- }
- };
-
- // 中国桔子
- class ChinaOrange :public AbstractOrange
- {
- public:
- virtual void PrintName() {
- cout << "中国桔子最甜的.n" << endl;
- }
- ChinaOrange() {
- cout << "中国桔子构造函数." << endl;
- }
- ~ChinaOrange() {
- cout << "中国桔子析构函数" << endl;
- }
- };
-
- // 埃及西瓜
- class EgyptWatermelon :public AbstractWatermelon {
- public:
- virtual void PrintName() {
- cout << "埃及西瓜:早在四千年前就种植西瓜." << endl;
- }
- EgyptWatermelon() {
- cout << "埃及西瓜构造函数." << endl;
- }
- ~EgyptWatermelon() {
- cout << "埃及西瓜析构函数." << endl;
- }
- };
-
- // 埃及桔子
- class EgyptOrange :public AbstractOrange {
- public:
- virtual void PrintName() {
- cout << "埃及桔子光照条件最好,供应高质量的桔子.n" << endl;
- }
- EgyptOrange() {
- cout << "埃及桔子构造函数." << endl;
- }
- ~EgyptOrange() {
- cout << "埃及桔子析构函数." << endl;
- }
- };
-
-
- // 抽象工厂 主要针对产品转换
- class AbstrctFactory {
- public:
- virtual AbstractWatermelon* CreateWatermelon() = 0;
- virtual AbstractOrange* CreateOrange() = 0;
-
- AbstrctFactory() {
- cout << "抽象工厂构造函数." << endl;
- }
- ~AbstrctFactory() {
- cout << "抽象工厂析构函数." << endl;
- }
- };
-
- // 中国工厂
- class ChinaFactory :public AbstrctFactory
- {
- public:
- virtual AbstractWatermelon* CreateWatermelon() {
- return new ChinaWatermelon;
- }
- virtual AbstractOrange* CreateOrange() {
- return new ChinaOrange;
- }
- ChinaFactory() {
- cout << "中国工厂构造函数." << endl;
- }
- ~ChinaFactory() {
- cout << "中国工厂析构函数." << endl;
- }
-
-
- };
-
- // 埃及工厂
- class EgyptFactory :public AbstrctFactory {
- public:
- virtual AbstractWatermelon* CreateWatermelon() {
- return new EgyptWatermelon;
- }
- virtual AbstractOrange* CreateOrange() {
- return new EgyptOrange;
- }
- EgyptFactory() {
- cout << "埃及工厂构造函数." << endl;
- }
- ~EgyptFactory() {
- cout << "埃及工厂析构函数." << endl;
- }
- };
-
-
- int main()
- {
- cout << "****************************************************************************" << endl;
- //抽象工厂基类指针派生类对象
- AbstrctFactory* cfactory = new ChinaFactory;
- /*
- 抽象工厂构造函数.
- 中国工厂构造函数.
- */
- //抽象西瓜基类指针
- AbstractWatermelon* cwatermelon = cfactory->CreateWatermelon();
- /*
- 抽象西瓜构造函数.
- 中国西瓜构造函数.
- */
- AbstractOrange* corange = cfactory->CreateOrange();
- /*
- 抽象桔子构造函数.
- 中国桔子构造函数.
- */
- cwatermelon->PrintName();
- /*
- 中国西瓜最好吃.
- */
- corange->PrintName();
- /*
- 中国桔子最甜的.
- */
-
- delete corange; corange = nullptr;
- //抽象桔子析构函数.
- delete cwatermelon; cwatermelon = nullptr;
- //抽象西瓜析构函数.
- delete cfactory; cfactory = nullptr;
- //抽象工厂析构函数.
- cout << "****************************************************************************" << endl;
-
-
- cout << "nn****************************************************************************" << endl;
- AbstrctFactory* efactory = new EgyptFactory;
- /*
- 抽象工厂构造函数.
- 埃及工厂构造函数
- */
- AbstractWatermelon* ewatermelon = efactory->CreateWatermelon();
- /*
- 抽象西瓜构造函数
- 埃及西瓜构造函数.
- */
- AbstractOrange* eorange = efactory->CreateOrange();
- /*
- 抽象橘子构造函数
- 埃及橘子构造函数.
- */
- ewatermelon->PrintName();
- /*
- 埃及西瓜:早在四千年前就种植西瓜.
- */
- eorange->PrintName();
- /*
- 埃及桔子光照条件最好,供应高质量的桔子.
- */
- delete eorange; eorange = nullptr;//抽象桔子析构函数.
- delete ewatermelon; ewatermelon = nullptr;//抽象西瓜析构函数.
- delete efactory; efactory = nullptr;
- /*
- 抽象工厂析构函数.
- */
- cout << "****************************************************************************" << endl;
- return 0;
- }