Simple Factory mode Factory mode abstract Factory mode

Source: Internet
Author: User

In fact, this three C + + design pattern has been learning for a long time, only to remember tonight to write these three design patterns, why use C + + design patterns? For example, you've been driving, just driving, upside down, turning, these are all basic, but you're not very good at driving, and not cool. Drift, just like the design patterns in C + +, you learn C + + based on a skilled application of new skills, can make your car more open.

Why use a simple factory model, remember the last time, although I knocked over the code, but when someone suddenly asked, why do you want to use the Simple Factory mode, I suddenly froze, the code is very simple, a look on the understand. It is the thought that matters. Specifically defines a class to be responsible for creating an instance of another class, an object-specific class that creates an interface for an external burst of objects to be called externally. The factory model has a very image description, the class of the object is like a factory, and the object that needs to be built is a product; the person who processes the product in the factory and uses the product does not care about how the product is produced. From the perspective of software development, this effectively reduces the coupling between modules.

This time I use the wife and vegetables to explain the simple factory model, I have a wife, she is the factory factory, wife factory will do the dish is producta (tomato scrambled eggs), PRODUCTB (fried cake silk), productc (mouth fish), I am a husband. I said to my wife today, I want to eat productb (fried bread), and then the wife agreed, and I do not need a wife how to do, or is not my wife do (she can also call takeout), finally I just see PRODUCTB (fried cake silk) just fine.

The following UML I just made a simple painting, and did not add data members, but you see in the code, I will say in detail

The first simple factory model is like this, in the client just create the food you want to eat, then as long as a wife factory, let her do it, and then, you can wait for dinner. On the code:

     /* Simple Factory mode   */#include <iostream>using namespace Std;class product{public:virtual void operation () =0;~ Product () {}}; Class Producta:public product{public:void operation () {cout<< "Scrambled eggs with tomatoes" <<endl;} ~producta () {}};class productb:public product{public:void operation () {cout<< "Fried cake silk" <<endl;} ~PRODUCTB () {}};class productc:public product{public:void operation () {cout<< "mouth fish" <<ENDL;} ~PRODUCTC () {}};class factory{protected:product *p; public:factory (Product *t) {p=t;} void operation () {p->operation ();} ~factory () {}};int main (int argc, char** argv) {      //create a base class pointer to a tomato scrambled egg object   Product *pa = new ProductA ();  Create a base class pointer to the object of a scrambled cookie  Product *PB = new PRODUCTB ();   Create a factory pointer   Factory *PFA1 = new Factory (PA);    Factory *PFA2 = new Factory (PB);      Pfa1->operation ();    Pfa2->operation ();    Delete PA;  Delete PB;  Delete pFa1;      Delete Pfa2;return 0;}


The above code comment is very clear, create a base class pointer to a tomato scrambled egg object, create a base class pointer to the object of a fried pancake, create a factory pointer, and then call on it.

Demo Result:

In fact, this simple factory model is certainly not good ah, if you want to eat more food, that still do not put their wife exhausted, in order to alleviate the pressure of the wife, we can take the wife, this time abstract out a big wife factory, tube other wife Factorya,factoryb. Let a wife do a dish, factorya wife do producta tomato scrambled eggs, factoryb do productb fried cake silk, this nothing, wayward, money more ...

Seriously, the extension plant is responsible for the different functions of its different factories. Come on, professional. The meaning of the factory method pattern is to define a factory interface that creates product objects, deferring the actual creation to subclasses. The Core factory class is no longer responsible for product creation, so that the core class becomes an abstract factory role and is responsible only for the interfaces that a specific factory subclass must implement, and the benefit of further abstraction is that the factory method pattern allows the system to introduce new products without modifying the specific factory roles.

The code is implemented as follows:

/* This is the previous simple factory model evolved from Factory mode */#include <iostream>using namespace std;//What to do class product{public:virtual void op Eration () =0;~product () {}}; Class Producta:public product{public:void operation () {cout<< "Scrambled eggs with tomatoes" <<endl;} ~producta () {}};class productb:public product{public:void operation () {cout<< "Fried cake silk" <<endl;} ~PRODUCTB () {}};class productc:public product{public:void operation () {cout<< "mouth fish" &LT;&LT;ENDL;} ~PRODUCTC () {}};class productd:public product{public:void operation () {cout<< "6 yuan Spicy soup" <<endl; ~PRODUCTD () {}};class producte:public product{public:void operation () {cout<< "Chisun noodle" &LT;&LT;ENDL;} ~producte () {}};class productf:public product{public:void operation () {cout<< "meat pinch bun" &LT;&LT;ENDL;}        ~PRODUCTF () {}};//factory wife class factory{protected:product *p; public:virtual void dofun () = 0; };class factorya:public Factory{public:factorya (Product *t) {p=t;} void Dofun () {p->operation ();}}; Class Factoryb:public Factory{public:factoryb (Product *t) {p=t;} void Dofun () {p->operation ();}};  int main (int argc, char** argv) {//Create a base class pointer to a tomato scrambled egg object Product *pa = new ProductA ();       Create a base class pointer to the object of a scrambled cookie Product *PB = new PRODUCTB ();   Creates a base-class pointer to an object of a Chisun noodle Product *pe = new Producte ();   Create a base class pointer to a meat bun object Product *PF = new PRODUCTF ();    Create a factory pointer Factory *pfa1 = new Factorya (PA);       Factory *PFA2 = new Factorya (PB);    Factory *pfa3 = new Factoryb (PE);      Factory *pfa4 = new Factoryb (PF);  cout<< "Output factory Factorya wife Cooking" <<endl;    Pfa1->dofun ();    Pfa2->dofun ();  cout<< "Output factory factoryb wife Cooking" <<endl;    Pfa3->dofun ();      Pfa4->dofun ();  Delete PA;  Delete PB;  Delete PE;  Delete PF;      Delete pFa1;  Delete pFa2;  Delete pFa3; Delete Pfa4;return 0;}


Very clear, the code has comments, want to eat what, what to do, the results show:

In fact, this look is not good, or the question mentioned earlier, with the increase in the food category, the increase in the wife, it is more and more complex, if you add a dish, you will come to a wife, which violates the open and closed principle (mentioned before the blog), this is not good ah. So we re-abstract the type of dish. For example, I put Shaanxi's food as a class producta, Sichuan's food as a PRODUCTB, Shandong's food for a PRODUCTC, in the final analysis, for all things, for simplicity are abstract in abstraction. No more whining, too:

Code Demo:

/* This is the previous Factory mode evolution to abstract Factory mode */#include <iostream>using namespace std;class product{public:virtual void Operati On () =0;~product () {}}; Class Productsichuan:public product{public:virtual void operation () = 0;}; Class Producta:public productsichuan{public:void operation () {cout<< "Sichuan tomato Scrambled egg" &LT;&LT;ENDL; ~producta () {}};class productb:public productsichuan{public:void operation () {cout<< "Sichuan Fried cake Silk" <<endl;} ~PRODUCTB () {}};class productc:public productsichuan{public:void operation () {cout<< "Sichuan mouth fish" &LT;&LT;ENDL;} ~PRODUCTC () {}};class productshaanxi:public product{public:virtual void operation () = 0;}; Class Productd:public productshaanxi{public:void operation () {cout<< "Shaanxi 6 yuan Spicy soup" <<endl;} ~PRODUCTD () {}};class producte:public productshaanxi{public:void operation () {cout<< "Xian qi noodle" &LT;&LT;ENDL;} ~producte () {}};class productf:public productshaanxi{public:void operation () {cout<< "Shaanxi Biangbiang Noodle" << Endl;} ~PRODUCTF () {}};//, Shandong's Delicious drop class ProductshandOng:public product{public:virtual void operation () = 0;}; Class Productg:public productshandong{public:void operation () {cout<< "Shandong Pancake" &LT;&LT;ENDL;} ~PRODUCTG () {}};class producth:public productshandong{public:void operation () {cout<< "Shandong roast Duck" &LT;&LT;ENDL;} ~producth () {}};class producti:public productshandong{public:void operation () {cout<< "Shandong Hairy Crabs" &LT;&LT;ENDL;}        ~producti () {}};//factory wife class factory{protected:product *p; public:virtual void dofun () = 0; };class factorya:public Factory{public:factorya (Product *t) {p=t;} void Dofun () {p->operation ();}}; Class Factoryb:public Factory{public:factoryb (Product *t) {p=t;} void Dofun () {p->operation ();}}; Class Factoryc:public Factory{public:factoryc (Product *t) {p=t;} void Dofun () {p->operation ();}};  int main (int argc, char** argv) {//Create a base class pointer to Sichuan tomato scrambled egg An object of Product *pa = new ProductA ();       Create a base class pointer to a Sichuan fried pastry object Product *pb = new PRODUCTB ();   Create a base class pointer to a Shaanxi Chisun Noodle object Product *pc = new PRODUCTD (); Create a base class pointer to a Shaanxi spicyHot object Product *pd = new Producte ();   Create a base class pointer to a Shandong pancake object Product *pe = new PRODUCTG ();    Create a factory pointer Factory *pfa1 = new Factorya (PA);       Factory *PFA2 = new Factorya (PB);    Factory *pfa3 = new Factoryb (PC);    Factory *pfa4 = new Factoryb (PD);       Factory *pfa5 = new Factoryc (PE);  cout<< "Output factory Factorya wife Cooking" <<endl;    Pfa1->dofun ();    Pfa2->dofun ();  cout<< "Output factory factoryb wife Cooking" <<endl;    Pfa3->dofun ();  Pfa4->dofun ();    cout<< "Output factory factoryc wife Cooking" <<endl;   Pfa5->dofun ();  Delete PA;  Delete PB;  Delete PC;  Delete PD;  Delete PE;      Delete pFa1;  Delete pFa2;  Delete pFa3;  Delete pFa4; Delete Pfa5;return 0;}

Results Demo:

This time suddenly remembered a bit has not said, is in the UML diagram, factory has the product data member, thus realizes the class and the class communication.

I dish rookie one, if there is anything wrong, welcome to point out, grateful,


Simple Factory mode Factory mode abstract Factory mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.