Abstract Factory mode _c language for C + + design Patterns

Source: Internet
Author: User

Problem description

I talked about the C + + design pattern--the factory method model, we may think that later products will become more and more, the establishment of the factory will be more and more, the factory has grown, the factory has become messy and difficult to manage; Because the factory method pattern creates objects that are inherited from product, the factory method pattern Each factory can only create a single kind of product, when it is necessary to produce a new product (do not inherit from product), the discovery of factory approach is powerless.

For example: A display circuit board manufacturer, its display circuit board types are not liquid crystal and liquid crystal; At this time, the manufacturer built two factories, factory A is responsible for the production of LCD circuit board, Factory B is responsible for the production of LCD circuit board; The factory has been running like this. One day, the general manager found that the direct production of the rest of the display is also very lucrative, so the general manager decided to set up two more plants C and d;c responsible for the production of the remaining parts of the LCD, D responsible for the production of the rest of the liquid crystal display. At this point, the man beside the staff said, manager, this is not good, we can directly in the factory A to add a production of the remaining parts of the non-LCD display line, in the factory B to add a production of the rest of the LCD display line, so you can not increase the plant, only to expand the existing plant, At the same time also facilitate the management of the factory, but also the production of LCD circuit board technicians on the rest of the LCD display of the production of the role of guidance, the production of liquid crystal display circuit board is the same. The general manager found this to be a good idea.

Back to the process of software development, the factory A and B are previously said C + + design mode-the factory method model; The general manager set up the factory C and D again, is duplicate C + + design mode--factory method mode, just produce different products. The disadvantage of this is that, as the staff said, increased management costs and human costs. In the process of object-oriented development, the object management and maintenance is very important, the more objects you have, the more difficult it is to manage and maintain; If the number of factories is too large, then the cost of management and maintenance will increase greatly; Although the production of different products, but can be a subtle relationship between the two, as the staff said, Some technical experience of technicians can be used, which is equivalent to the different objects in the same class, where some resources can be shared. Then, adding an assembly line and expanding the plant is, of course, the best idea.

The actual problem has been solved, so how do you use the design pattern to simulate the actual problem? That is the abstract factory model that follows.

UML Class Diagram

The abstract factory model that we are talking about now is the extension and extension of the factory method pattern, but the abstract factory model is more general and representative, it has the advantage of the factory method, and also increases the ability to solve the practical problems.

As shown in the illustration, the abstract factory pattern is like the superposition of two factory method patterns. An abstract factory creates a series of related objects in which the implementation is actually the factory method pattern used. In the factory factory each method, is like is a production line, but the production line actually needs to produce what kind of product, this is decided by the Factory1 and the Factory2, thus delayed the concrete subclass the instantiation, simultaneously centralized the production line management, saved the resource waste.

Applicable occasions

The factory method model is suitable for the single occasion of the product type structure, provides an interface for a class of products, whereas an abstract factory approach applies to situations where the product is structured in a way that is primarily used to create a set of related products (with multiple categories), providing an interface for creation; When you have multiple abstract roles, Abstract factories can be useful.

Code implementation

* * * * * * * FILENAME:ABSTRACTFACTORYPATTERNDEMO * * * * * * * * * * * author:jelly
 
nformation * * #include <iostream> using namespace std;
 
Product A class ProductA {public:virtual void show () = 0;};
  Class Producta1:public ProductA {public:void Show () {cout<< "I ' m ProductA1" <<endl;
 
}
};
  Class Producta2:public ProductA {public:void Show () {cout<< "I ' m ProductA2" <<endl;
 
}
};
 
Product B class ProductB {public:virtual void show () = 0;};
  Class Productb1:public PRODUCTB {public:void Show () {cout<< "I ' m ProductB1" <<endl;
 
}
};
  Class Productb2:public PRODUCTB {public:void Show () {cout<< "I ' m ProductB2" <<endl;
 
}
};
  Factory class Factory {public:virtual producta *createproducta () = 0;
Virtual PRODUCTB *CREATEPRODUCTB () = 0;
 
}; Class Factory1:public Factory {public:producta *createproducta () {returN New ProductA1 ();
  } PRODUCTB *CREATEPRODUCTB () {return new ProductB1 ();
 
}
};
  Class Factory2:public Factory {producta *createproducta () {return new ProductA2 ();
  } PRODUCTB *CREATEPRODUCTB () {return new ProductB2 ();
 
}
};
  int main (int argc, char *argv[]) {Factory *factoryobj1 = new Factory1 ();
  ProductA *productobja1 = Factoryobj1->createproducta ();
 
  PRODUCTB *productobjb1 = FACTORYOBJ1-&GT;CREATEPRODUCTB ();
  Productobja1->show ();
 
  Productobjb1->show ();
  Factory *factoryobj2 = new Factory2 ();
  ProductA *productobja2 = Factoryobj2->createproducta ();
 
  PRODUCTB *productobjb2 = FACTORYOBJ2-&GT;CREATEPRODUCTB ();
  Productobja2->show ();
 
  Productobjb2->show ();
    if (factoryObj1!= NULL) {delete factoryObj1;
  FactoryObj1 = NULL;
    } if (productObjA1!= NULL) {delete productObjA1;
  Productobja1= NULL;
    } if (productObjB1!= NULL) {delete productObjB1;
  productObjB1 = NULL; }
 
  if (factoryObj2!= NULL) {delete factoryObj2;
  FactoryObj2 = NULL;
    } if (productObjA2!= NULL) {delete productObjA2;
  productObjA2 = NULL;
    } if (productObjB2!= NULL) {delete productObjB2;
  productObjB2 = NULL; }
}

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.