C + + Design mode-Abstract Factory mode

Source: Internet
Author: User

Problem description

Before referring to the C + + design pattern-the factory method model, we may think that later products will be more and more, the establishment of factories will be more and more, factories have grown, factories have become messy and difficult to manage; Because the factory method pattern creates objects that are inherited from product, Each factory can only create a single kind of product, and when it comes to producing a brand new product (not inheriting from product), the discovery of a factory approach is powerless.

For example: A display circuit board manufacturer, its display circuit board type has non-liquid crystal and liquid crystal; At this time, the manufacturers to build two factories, factory A is responsible for the production of non-liquid crystal display circuit board, factory B is responsible for the production of LCD circuit board; The factory has been running this way. 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 build two factories C and D;c responsible for the production of non-liquid crystal display of the remaining components, D is responsible for the production of the rest of the LCD monitor components. At this time, the person next to the staff said, manager, this is not good, we can directly in the factory A to add a production of non-liquid crystal display of the remaining parts of the production line, in plant B to add a production line of the rest of the LCD display, so that can not increase the plant, only with the existing plant to expand, The general manager found this to be a good idea.

Back in the process of software development, factories A and B are the previously mentioned C + + Design mode-factory method mode, the general manager once again set up factories C and D, is repeating the C + + Design mode-factory method mode, just the production of the product is different. The disadvantage of doing so, as the staff said, increases management costs and labor costs. In the process of object-oriented development, it is very important to object management and maintenance, the more objects, the more difficult 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 have a subtle relationship between the two, as the staff said, Technical experience of technical personnel can be used for reference, which is equivalent to the same class of different objects, can be common between some resources. Then, adding an assembly line to expand the plant, of course, is the best idea.

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

UML Class Diagram

Now the abstract factory model is the extension and extension of the factory method pattern, but the abstract factory model is more general and representative, it has the advantages of the factory method, but also increases the ability to solve practical problems.

, the abstract factory pattern is like an overlay of two factory method patterns. The 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 delays the concrete subclass the instantiation, simultaneously centralized the production line management, saved the resources the waste.

Applicable occasions

The factory method model is suitable for a single type of product structure, providing an interface for a class of products, while the abstract factory approach is suitable for applications where there are many types of products, mainly used to create a set of products (with multiple kinds) related, and to provide the interfaces created for them; When you have multiple abstract roles, Abstract factories can be useful.

Code implementation
/*** filename:abstractfactorypatterndemo** author:jelly young** date:2013/11/19** description:m Ore information, go to http://www.jellythink.com*/#include <iostream>using namespace std;//Product aclass P roducta{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 bclass 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;}};/ /Factoryclass 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 ();p roductobja1->show ();p roductobjb1->show (); Factory *factoryobj2 = new Factory2 (); ProductA *productobja2 = Factoryobj2->createproducta (); PRODUCTB *productobjb2 = FACTORYOBJ2-&GT;CREATEPRODUCTB ();p roductobja2->show ();p roductobjb2->show (); 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;}}

  

C + + Design mode-Abstract Factory mode

Related Article

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.