Design mode--(1) Factory mode

Source: Internet
Author: User

Factory mode

  The factory model belongs to the creation model, which can be divided into three types, simple Factory mode, factory method mode and abstract Factory mode.

Simple Factory mode

Simple factory model, its main feature is the need to make judgments in the factory class, so as to create the corresponding products. When adding new products, you need to modify the factory class.

For example: There is a manufacturer of processor cores, it has only one factory, capable of producing two types of processor cores. What kind of processor core the customer needs, be sure to show to the production plant. The following is an implementation scenario.

// simple Factory mode 
#include <iostream>using namespacestd;
enum CTYPE {product_a, PRODUCT_b}; //Abstract Productsclassabstractproduct{ Public: Virtual voidGetproductsname () =0;};//Specific Product 1classConcreteProduct1: Publicabstractproduct{ Public: voidGetproductsname () {cout<<"ConcreteProduct1"<<Endl;}};//Specific Product 2classConcreteProduct2: Publicabstractproduct{ Public: voidGetproductsname () {cout<<"ConcreteProduct2"<<Endl;}};//Specific factory (no abstract factory),can produce two kinds of products, in-house judgment classconcretefactory{ Public: Abstractproduct* Getproductobject ( enum CTYPE CTYPE) { if(CType==product_a) { return NewConcreteProduct1 (); } Else if(CType== product_b) { return NewConcreteProduct2 (); } Else { return 0; } }};intMain () {concretefactory F; Abstractproduct* P1 = F.getproductobject (product_a); Abstractproduct* P2 = f.getproductobject (Product_b); P1-Getproductsname (); P2-Getproductsname (); DeleteP1; DeleteP2; return 0;}

The main disadvantage of this design is the need to modify the factory class when adding new products. This violates the open closure principle: Software entities (classes, modules, functions) can be extended, but cannot be modified. So, the factory method pattern appeared.

Factory method Mode

The factory method pattern refers to defining an interface for creating objects, so that subclasses decide which class to instantiate. The Factory method defers the instantiation of a class to its subclasses.

The producer of the processor's core made a lot of money and decided to set up another factory dedicated to the B-type single-core, and the original factory was designed to produce a single core of type A. At this point, the customer to do is to find a good factory, such as to a model of the nuclear, to find a factory to, or to find the B factory, no longer need to tell the factory specifically what type of processor core. Here is an implementation scenario.

#include <iostream>using namespacestd;//General Factory mode//Abstract Productsclassabstractproduct{ Public:    Virtual voidGetproductname () =0;};//Specific Product 1classConcreteProduct1: Publicabstractproduct{ Public:    voidGetproductname () {cout<<"ConcreteProduct1"<<Endl;}};//Specific Product 2classConcreteProduct2: Publicabstractproduct{ Public:    voidGetproductname () {cout<<"ConcreteProduct2"<<Endl;}};//Abstract Factoryclassabstractfactory{ Public:    Virtualabstractproduct* getproduct () =0;};//Specific Factory 1classConcreteFactory1: Publicabstractfactory{ Public: Abstractproduct*getproduct () {return NewConcreteProduct1 (); }};//Specific Factory 2classConcreteFactory2: Publicabstractfactory{ Public: Abstractproduct*getproduct () {return NewConcreteProduct2 (); }};intMain () {abstractfactory* F1 =NewConcreteFactory1 (); Abstractfactory* F2 =NewConcreteFactory2 (); F1->getproduct ()Getproductname (); F2->getproduct ()Getproductname (); DeleteF1, F2; return 0;}

Factory method models also have drawbacks, and each additional product requires an additional plant for the object. If the company develops rapidly and launches a lot of new products, it will have to open a new factory. In the C + + implementation, it is to define a factory class. Obviously, the factory method pattern requires more class definitions than the simple factory model.

Abstract Factory mode

Abstract Factory mode, in order to provide an interface for creating a series of related or interdependent objects, you need to specify their specific classes.

The company's technology continues to evolve, not only to produce single-core processors, but also to produce multicore processors. Both the simple Factory mode and the factory method mode are now beyond. The abstract factory model has appeared. The company also opened two factories, one dedicated to the production of a single core multi-core processor, while another factory dedicated to the production of B-type single-core multi-core processors, the implementation of the code below.

#include <iostream>using namespacestd;//Abstract Factory mode//Abstract Product Aclassabstractproducta{ Public:    Virtual voidGetproductname () =0;};//Specific product A1classConcreteProductA1: Publicabstractproducta{ Public:    voidGetproductname () {cout<<"ConcreteProductA1"<<Endl;}};//Specific product A2classConcreteProductA2: Publicabstractproducta{ Public:    voidGetproductname () {cout<<"ConcreteProductA2"<<Endl;}};//Abstract Product Bclassabstractproductb{ Public:    Virtual voidGetproductname () =0;};//Specific product B1classConcreteProductB1: Publicabstractproductb{ Public:    voidGetproductname () {cout<<"ConcreteProductB1"<<Endl;}};//Specific product B2classConcreteProductB2: Publicabstractproductb{ Public:    voidGetproductname () {cout<<"ConcreteProductB2"<<Endl;}};//Abstract Factoryclassabstractfactory{ Public:    Virtualabstractproducta* getproducta () =0; Virtualabstractproductb* GETPRODUCTB () =0;};//Specific Factory 1classConcreteFactory1: Publicabstractfactory{abstractproducta*getproducta () {return NewConcreteProductA1; } ABSTRACTPRODUCTB*GETPRODUCTB () {return NewConcreteProductB1; }}; //Specific Factory 2classConcreteFactory2: Publicabstractfactory{abstractproducta*getproducta () {return NewConcreteProductA2; } ABSTRACTPRODUCTB*GETPRODUCTB () {return NewConcreteProductB2; }};intMain () {abstractfactory*F1 =NewConcreteFactory1 (); Abstractfactory*F2 =NewConcreteFactory2 (); F1->getproducta ()Getproductname (); F1-&GT;GETPRODUCTB ()Getproductname (); F2->getproducta ()Getproductname (); F2-&GT;GETPRODUCTB ()Getproductname (); DeleteF1, F2; return 0;}

At this point, three modes are complete.

Design mode--(1) 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.