Design patterns in the field of software provide developers with an effective way to use expert design experience. The design pattern uses the important characteristic of object-oriented programming language: encapsulation, inheritance, polymorphism, really comprehend the essence of design pattern is probably a long process, need to accumulate a lot of practical experience. Recently read the design pattern of the book, for each model, with C + + write a small example, deepen understanding. The main reference is "big talk design mode" and "design pattern: the basis of reusable object-oriented software" two books. This article describes the implementation of the factory Pattern.
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. that sounds almost like a factory model. The following introduction, the first introduction of the 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. A little abstract, for example, i understand. There is a manufacturer of processor cores, which 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:
enumCTYPE {corea, coreb}; classSinglecore { public: Virtual voidShow () =0; }; //Single Core AclassSinglecorea: publicSinglecore { public: voidShow () {cout<<"Singlecore A"<<endl;} }; //Single Core BclassSinglecoreb: publicSinglecore { public: voidShow () {cout<<"Singlecore B"<<endl;} }; //the only factory that can produce two types of processor cores, in-house judgmentclassFactory { public: Singlecore* Createsinglecore (enumCTYPE CTYPE) { if(ctype = = Corea)//Factory internal judgment return NewSinglecorea ();//production of nuclear a Else if(ctype = =Coreb)return NewSinglecoreb ();//production of nuclear b Else returnNULL; } };
The main drawbacks of this design have been mentioned before, that is, to add new kernel types, you need to modify the factory class . This violates the open closure Principle: software entities (classes, modules, Functions) can be extended, but cannot be modified. so, the factory method pattern Appeared. The so-called 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.
It sounds abstract, or it is explained in the example just NOW. 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.
Factory Method Mode:
classSinglecore { public: Virtual voidShow () =0; }; //Single Core AclassSinglecorea: publicSinglecore { public: voidShow () {cout<<"Singlecore A"<<endl;} }; //Single Core BclassSinglecoreb: publicSinglecore { public: voidShow () {cout<<"Singlecore B"<<endl;} }; classFactory { public: Virtualsinglecore* Createsinglecore () =0; }; //production of a nuclear plantclassFactorya: publicFactory { public: Singlecorea* Createsinglecore () {return Newsinglecorea;} }; //Plant producing B-coreclassFactoryb: publicFactory { public: Singlecoreb* Createsinglecore () {return Newsinglecoreb;} };
Factory method models also have drawbacks, and each additional product requires an additional plant for the object . If the company develops rapidly and launches many new processor cores, it will have to open a new Plant. 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.
Given the simple factory model and the factory approach model, why should there be an abstract factory model? How does it work? To cite this example, 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. It is defined to provide an interface that creates a series of related or interdependent objects without specifying their specific classes. In this application, the company also opened two factories, one dedicated to the production of a single core multi-core processor, and another factory dedicated to the production of B-type Single-core multicore processors, The implementation of the Code BELOW.
Abstract Factory Mode:
//Single CoreclassSinglecore { public: Virtual voidShow () =0; }; classSinglecorea: publicSinglecore { public: voidShow () {cout<<"single Core A"<<endl;} }; classSinglecoreb: publicSinglecore { public: voidShow () {cout<<"single Core B"<<endl;} }; //multi-coreclassmulticore { public: Virtual voidShow () =0; }; classMulticorea: publicmulticore { public: voidShow () {cout<<"Multi Core A"<<endl;} }; classMulticoreb: publicmulticore { public: voidShow () {cout<<"Multi Core B"<<endl;} }; //FactoryclassCorefactory { public: Virtualsinglecore* Createsinglecore () =0; Virtualmulticore* Createmulticore () =0; }; //Factory a, designed to produce a type a processorclassFactorya: publicCorefactory { public: Singlecore* Createsinglecore () {return NewSinglecorea ();} Multicore* Createmulticore () {return NewMulticorea ();} }; //Factory b, dedicated to the production of B-type processorsclassFactoryb: publicCorefactory { public: Singlecore* Createsinglecore () {return NewSinglecoreb ();} Multicore* Createmulticore () {return NewMulticoreb ();} };
Design pattern C + + implementation-factory mode