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.
[CPP]View Plaincopyprint?
- Enum CTYPE {corea, Coreb};
- Class Singlecore
- {
- Public
- virtual void Show () = 0;
- };
- Single Core A
- Class Singlecorea: Public singlecore
- {
- Public
- void Show () {cout<<"Singlecore A" <<endl;}
- };
- Single Core B
- Class Singlecoreb: Public singlecore
- {
- Public
- void Show () {cout<<"Singlecore B" <<endl;}
- };
- The only factory that can produce two types of processor cores, in-house judgment
- Class Factory
- {
- Public
- singlecore* Createsinglecore (enum CTYPE CTYPE)
- {
- if (ctype = = Corea) //Factory internal judgment
- return new Singlecorea (); //production of nuclear a
- Else if (ctype = = Coreb)
- return new Singlecoreb (); //production of nuclear b
- Else
- return NULL;
- }
- };
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.
[CPP]View Plaincopyprint?
- Class Singlecore
- {
- Public
- virtual void Show () = 0;
- };
- Single Core A
- Class Singlecorea: Public singlecore
- {
- Public
- void Show () {cout<<"Singlecore A" <<endl;}
- };
- Single Core B
- Class Singlecoreb: Public singlecore
- {
- Public
- void Show () {cout<<"Singlecore B" <<endl;}
- };
- Class Factory
- {
- Public
- virtual singlecore* createsinglecore () = 0;
- };
- Production of a nuclear plant
- Class Factorya: Public Factory
- {
- Public
- singlecorea* Createsinglecore () { return new Singlecorea;}
- };
- Plant producing B-core
- Class Factoryb: Public Factory
- {
- Public
- singlecoreb* Createsinglecore () { return new Singlecoreb;}
- };
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.
[CPP]View Plaincopyprint?
- Single Core
- Class Singlecore
- {
- Public
- virtual void Show () = 0;
- };
- Class Singlecorea: Public singlecore
- {
- Public
- void Show () {cout<<"single Core A" <<endl;}
- };
- Class Singlecoreb: Publicsinglecore
- {
- Public
- void Show () {cout<<"single Core B" <<endl;}
- };
- Multi-core
- Class multicore
- {
- Public
- virtual void Show () = 0;
- };
- Class Multicorea: Public multicore
- {
- Public
- void Show () {cout<<"Multi Core A" <<endl;}
- };
- Class Multicoreb: Public multicore
- {
- Public
- void Show () {cout<<"Multi Core B" <<endl;}
- };
- Factory
- Class Corefactory
- {
- Public
- virtual singlecore* createsinglecore () = 0;
- virtual multicore* createmulticore () = 0;
- };
- Factory A, designed to produce a type a processor
- Class Factorya: Publiccorefactory
- {
- Public
- singlecore* Createsinglecore () { return new Singlecorea ();}
- multicore* Createmulticore () { return new Multicorea ();}
- };
- Factory B, dedicated to the production of B-type processors
- Class Factoryb: Public corefactory
- {
- Public
- singlecore* Createsinglecore () { return new Singlecoreb ();}
- multicore* Createmulticore () { return new Multicoreb ();}
- };
At this point, the factory model is finished. Use rational Rose 2003 software to give a UML diagram of three factory models to deepen impressions.
UML diagram for simple Factory mode:
UML diagram of the factory method:
UML diagram for abstract Factory mode:
I enjoy the copyright of the blog article, reproduced please indicate the source http://blog.csdn.net/wuzhekai1985
Design pattern C + + implementation (1)-Factory mode (RPM)