1, What is it
- The main problem solved by the factory pattern is to separate the process of creating objects originally distributed in various places and deliver them to the factory for processing. Other places want to directly call the factory method to obtain objects
- We can abstract the public interfaces of some classes to form an abstract parent class or interface. In this way, we can declare a pointer to the parent class to point to the actual implementation of the child class to achieve the purpose of polymorphism
2, Advantages and disadvantages
- advantage
- Defines the interface for creating objects and encapsulates the process of object creation
- Delay the work of materializing classes into subclasses
- shortcoming
- Programmers must know the name of subclasses. When there are many subclasses, naming is a very troublesome thing
- In the parent class, you do not know which specific subclass to instantiate
3, Classification and introduction of factory mode
-
The factory mode is mainly divided into the following three categories
-
Simple factory
-
Setting scenario: go to a restaurant to order. There are various dishes. You need to model the hotel
#include <iostream> using namespace std; //Define an abstract base class class hotel { public: virtual ~hotel(){}; virtual void order()=0;//Ordering is an interface class }; //Next, define subclasses that inherit from the base class class fruit:public hotel { public: void order() { cout<<"eat fruit"<<endl; } }; class beef:public hotel { public: void order() { cout<<"Eat beef"<<endl; } }; class chicken:public hotel { public: void order() { cout<<"Eat chicken"<<endl; } }; enum HotelType { Fruit, Beef, Chicken }; class Factory { public: hotel* CrateHotel(HotelType type) { switch(type) { case Fruit: return new fruit(); //Assign the pointer of the subclass to the parent class to realize polymorphism break; case Beef: return new beef(); break; case Chicken: return new chicken(); break; } } }; int main() { Factory factory; factory.CrateHotel(Beef)->order(); factory.CrateHotel(Beef)->order(); factory.CrateHotel(Fruit)->order(); return 0; }
-
-
-
Factory method
-
Setting scenario: assuming that the number of guests increases recently, each dish needs to be produced independently, each production line only produces one dish, and a factory is provided for each subclass
#include <iostream> using namespace std; //Define an abstract base class class hotel{ public: virtual ~hotel(){}; virtual void order()=0;//Ordering is an interface class }; //Next, define subclasses that inherit from the base class class fruit:public hotel{ public: void order(){ cout<<"eat fruit"<<endl; } }; class beef:public hotel{ public: void order(){ cout<<"Eat beef"<<endl; } }; class chicken:public hotel { public: void order(){ cout<<"Eat chicken"<<endl; } }; //Define factory base class class FactoryBase{ public: virtual hotel* CreateHotel()=0; }; //The following inherits the factory base class and gives each subclass an independent factory //This is how independent factories are produced class Fruit:public FactoryBase{ public: hotel* CreateHotel(){ return new fruit(); } }; class Beef:public FactoryBase { public: hotel* CreateHotel(){ return new beef(); } }; class Chicken:public FactoryBase { public: hotel* CreateHotel(){ return new chicken(); } }; int main() { Fruit ft; ft.CreateHotel()->order(); Beef bf; bf.CreateHotel()->order(); Chicken ch; ch.CreateHotel()->order(); return 0; }
-
- Compare with simple factory - The application of factory method pattern is not only to encapsulate the creation of objects, but to implement the creation of objects in the same factory subclass. Simple factories are all created in the same factory - Composition of factory method model - Abstract factory role: This is the core of the factory pattern and has nothing to do with the application. It is the interface that the specific factory role must implement or the parent class that must inherit.**For example, on the Internet FactoryBase** - Specific factory role: it contains code related to specific business logic, which is called by the application to create the corresponding specific product object.**Such as the above ft,bf** - Abstract product role: it is the parent class inherited by a specific product or the interface implemented.**Such as the above hotel** - Specific product role: the object created by a specific factory role is an instance of this role.**Such as the above fruit Instantiate object** - shortcoming - Every time you add a product, you need to add a class. When there are many products, you need to add a lot of classes
-
Abstract factory
-
Setting scenario: suppose the hotel finds that besides cooking, selling wine is also very profitable, so it decides to sell wine. Food and wine have different price grades. For example, delivery room A sells fruit and white wine, and delivery room B sells beef and red wine
-
Compare with factory method
- It is more complex when creating objects
-
Conditions to be met by the abstract factory pattern
- There are multiple products of the same family in the system, and the system can only consume one family of products at a time
- Those belonging to the same product family shall be used
#include <iostream> using namespace std; //Define an abstract base class class hotel{ public: virtual ~hotel(){}; virtual void order()=0;//Ordering is an interface class }; //Next, define subclasses that inherit from the base class class fruit:public hotel{ public: void order(){ cout<<"eat fruit"<<endl; } }; class beef:public hotel{ public: void order(){ cout<<"Eat beef"<<endl; } }; class chicken:public hotel { public: void order(){ cout<<"Eat chicken"<<endl; } }; //Define wine base class class wine{ public: ~wine(){}; virtual void sold()=0;//Wine selling price interface inheritance class }; class WriteWine:public wine { public: void sold(){ cout<<"181 bottles of white wine"<<endl; } }; class RedWine:public wine { public: void sold(){ cout<<"181 bottles of red wine"<<endl; } }; class FactoryBase{ public: virtual hotel* CreateHotel()=0;//Dish factory inheritance interface virtual wine* CreateWine()=0;//Winery inheritance interface }; class FactoryA:public FactoryBase { public: hotel* CreateHotel(){ return new fruit; } wine* CreateWine(){ return new WriteWine(); } }; class FactoryB:public FactoryBase { public: hotel* CreateHotel(){ return new beef; } wine* CreateWine(){ return new RedWine(); } }; int main() { FactoryA a; a.CreateHotel()->order(); a.CreateWine()->sold(); FactoryB b; b.CreateHotel()->order(); b.CreateWine()->sold(); return 0; }
-