Recommended reading Design pattern C + + factory method pattern
Brand awareness is gradually increasing, not only producing one product. If we continue to use one factory for one product, then if there are three brands Huawei, Xiaomi and apple. They each want three products: mobile phones, headphones and laptops. At this time, we need to create 9 factories to realize it. With more classes, the implementation and maintenance of code is also very troublesome. At this point, we must introduce a new pattern -- abstract method pattern
Definition idea: abstract factory pattern is a generalized version of factory method pattern. Factory method pattern is a special abstract factory pattern. In the factory method pattern, each specific factory can only generate one specific product, while in the abstract factory method pattern, each specific factory can generate multiple specific products. Abstract factory pattern is frequently used in actual software development
advantage:
- It is convenient to add new specific factories and product families without modifying the existing system, which conforms to the "opening and closing principle"
- Abstract factory pattern can realize the design purpose of high cohesion and low coupling
Disadvantages:
- When adding new product objects, it is difficult to extend the abstract factory to produce new kinds of products
Usage scenario:
- When you need to create a product family, or want to assemble the relevant products manufactured
- Users do not care about the creation process of objects and decouple the creation and use of objects
In the abstract factory model, we can create different factories according to different manufacturers, and each factory can produce its own products
Product grade: different manufacturers of the same product
Product family: different products from the same manufacturer
Code implementation:
First implement an abstract class of mobile phone
//Abstract mobile phone class AbstractPhone { public: virtual void ShowName() = 0; };
Then all manufacturers' mobile phones have to rewrite the functions provided by the abstract mobile phone
//Huawei Mobile class HuaweiPhone : public AbstractPhone { public: virtual void ShowName() { cout << "Huawei Mobile" << endl; } }; //Mi phones class XiaomiPhone : public AbstractPhone { public: virtual void ShowName() { cout << "Mi phones" << endl; } }; //iPhone class ApplePhone : public AbstractPhone { public: virtual void ShowName() { cout << "iPhone" << endl; } };
By analogy, the number of abstract products that need to be created depends on the number of products, and each manufacturer needs to rewrite the corresponding function
//Abstract earphone class AbstractHeadset { public: virtual void ShowName() = 0; }; //Huawei headset class HuaweiHeadset : public AbstractHeadset { public: virtual void ShowName() { cout << "Huawei headset" << endl; } }; //Millet headset class XiaomiHeadset : public AbstractHeadset { public: virtual void ShowName() { cout << "Millet headset" << endl; } }; //Apple headset class AppleHeadset : public AbstractHeadset { public: virtual void ShowName() { cout << "Little apple headset" << endl; } }; //Abstract computer class AbstractComputer { public: virtual void ShowName() = 0; }; //Huawei computer class HuaweiComputer : public AbstractComputer { public: virtual void ShowName() { cout << "Huawei computer" << endl; } }; //Xiaomi computer class XiaomiComputer : public AbstractComputer { public: virtual void ShowName() { cout << "Xiaomi computer" << endl; } }; //Apple Computer class AppleComputer : public AbstractComputer { public: virtual void ShowName() { cout << "Apple Computer" << endl; } };
When the product is available, you need to create an abstract factory and specify what functions the specific factory should have
//Abstract factory for product family class AbstrackFactory { public: virtual AbstractPhone* CreatePhone() = 0; virtual AbstractHeadset* CreateHeadset() = 0; virtual AbstractComputer* CreateComputer() = 0; }
Correspondingly, each manufacturer should have its own factory, and the functions of the abstract factory should be rewritten in the specific factory
//Huawei factory class HuaweiFactory : public AbstrackFactory { public: virtual AbstractPhone* CreatePhone() { return new HuaweiPhone(); } virtual AbstractHeadset* CreateHeadset() { return new HuaweiHeadset(); } virtual AbstractComputer* CreateComputer() { return new HuaweiComputer(); } }; //Millet factory class XiaomiFactory : public AbstrackFactory { public: virtual AbstractPhone* CreatePhone() { return new XiaomiPhone(); } virtual AbstractHeadset* CreateHeadset() { return new XiaomiHeadset(); } virtual AbstractComputer* CreateComputer() { return new XiaomiComputer(); } }; //Apple factory class AppleFactory : public AbstrackFactory { public: virtual AbstractPhone* CreatePhone() { return new ApplePhone(); } virtual AbstractHeadset* CreateHeadset() { return new AppleHeadset(); } virtual AbstractComputer* CreateComputer() { return new AppleComputer(); } };
Test:
void test() { //Create a factory AbstrackFactory* factory = nullptr; //Create product AbstractPhone* phone = nullptr; AbstractHeadset* headset = nullptr; AbstractComputer* computer = nullptr; //Specify that the factory is a Huawei factory factory = new HuaweiFactory(); //Products created through Huawei factory phone = factory->CreatePhone(); headset = factory->CreateHeadset(); computer = factory->CreateComputer(); phone->ShowName(); headset->ShowName(); computer->ShowName(); delete phone; delete headset; delete computer; delete factory; }