The design patterns in the software field provide developers with an effective way to use expert design experience. Objects are used in the design model.Programming LanguageImportant features: encapsulation, inheritance, polymorphism, true understanding of the essence of the design model is a long process that requires a lot of practical experience. I recently read a book on design patterns. I wrote a small example in C ++ for each pattern to help me better understand it. Refer to "big talk Design Patterns" and "design patterns: Reusable basics of object-oriented software. This article introduces the implementation of the factory model.
The factory mode is a creation mode, which can be roughly divided into three types: simple factory mode, factory method mode, and abstract factory mode. It sounds like a factory model. Next, we will introduce the simple factory model one by one. Its main feature is to make judgments in the factory class to create corresponding products. When a new product is added, You need to modify the factory class. A little abstract. Let's take an example. There is a manufacturer that produces processor cores. It has only one factory and can produce two types of processor cores. What kind of processor cores the customer needs must explicitly inform the production plant. An implementation scheme is provided below.
[CPP] View plaincopyprint?
-
- EnumCtype {Corea, coreb };
-
- ClassSinglecore
- {
-
- Public:
-
- Virtual VoidShow () = 0;
-
- };
-
- // Single-core
-
- ClassSinglecorea:PublicSinglecore
-
- {
-
- Public:
- VoidShow () {cout <"Singlecore"<Endl ;}
-
- };
-
- // Single-core B
-
- ClassSinglecoreb:PublicSinglecore
-
- {
-
- Public:
- VoidShow () {cout <"Singlecore B"<Endl ;}
-
- };
-
- // The only factory that can produce two types of processor cores, which can be determined internally
-
- ClassFactory
-
- {
-
- Public:
-
- Singlecore * createsinglecore (EnumCtype)
- {
-
- If(Ctype = Corea)// Factory internal judgment
-
- Return NewSinglecorea ();// Production core
-
- Else If(Ctype = coreb)
- Return NewSinglecoreb ();// Production core B
-
- Else
-
- ReturnNULL;
-
- }
-
- };
The main disadvantage of this design has been mentioned before, that is, to add a new core type, you need to modify the factory class. This violates the open and closed principle: software entities (classes, modules, and functions) can be expanded, but cannot be modified. As a result, the factory method model emerged. The so-called factory method mode refers to defining an interface for creating objects, so that the subclass decides which class to instantiate. Factory method delays the instantiation of a class to its subclass.
It sounds very abstract. I will explain it in the example just now. The processor core producer made a lot of money, so he decided to set up another factory dedicated to produce the single-core number B, and the original factory dedicated to producing the single-core number. In this case, the customer needs to find the factory. For example, if the-type core is required, the customer needs Factory A; otherwise, the customer needs factory B, you no longer need to tell the factory what type of processor core is required. An implementation scheme is provided below.
[CPP] View plaincopyprint?
-
- ClassSinglecore
-
- {
-
- Public:
- Virtual VoidShow () = 0;
-
- };
-
- // Single-core
-
- ClassSinglecorea:PublicSinglecore
-
- {
-
- Public:
- VoidShow () {cout <"Singlecore"<Endl ;}
-
- };
-
- // Single-core B
-
- ClassSinglecoreb:PublicSinglecore
-
- {
-
- Public:
- VoidShow () {cout <"Singlecore B"<Endl ;}
-
- };
-
- ClassFactory
-
- {
-
- Public:
-
- VirtualSinglecore * createsinglecore () = 0;
-
- };
-
- // A-core production plant
- ClassFactorya:PublicFactory
-
- {
-
- Public:
-
- Singlecorea * createsinglecore (){Return NewSinglecorea ;}
-
- };
-
- // B-core production plant
- ClassFactoryb:PublicFactory
-
- {
-
- Public:
-
- Singlecoreb * createsinglecore (){Return NewSinglecoreb ;}
-
- };
The factory method model also has disadvantages. Each time a product is added, a factory with an object needs to be added. If the company is developing rapidly and has launched many new processor cores, it is necessary to set up a new factory. In the implementation of C ++, the factory classes must be defined one by one. Obviously, the factory method mode requires more class definitions than the simple factory mode.
Now that we have a simple factory model and a factory method model, why do we need an abstract factory model? What does it do? For example, the company's technological advances can not only produce single-core processors, but also multi-core processors. At present, the simple factory model and the factory method model are beyond reach. The abstract factory model was launched. It is defined to provide an interface for creating a series of related or mutually dependent objects without specifying their specific classes. In this case, the company opened two factories, one dedicated to the production of A-type single-core processor, and the other dedicated to the production of A-type single-core processor, the following shows the implementationCode.
[CPP] View plaincopyprint?
-
- // Single-core
-
- ClassSinglecore
- {
-
- Public:
-
- Virtual VoidShow () = 0;
-
- };
-
- ClassSinglecorea:PublicSinglecore
-
- {
-
- Public:
- VoidShow () {cout <"Single Core"<Endl ;}
-
- };
-
- ClassSinglecoreb:PublicSinglecore
-
- {
-
- Public:
-
- VoidShow () {cout <"Single Core B"<Endl ;}
- };
-
- // Multi-core
-
- ClassMulticore
-
- {
-
- Public:
-
- Virtual VoidShow () = 0;
-
- };
-
- ClassMulticorea:PublicMulticore
- {
-
- Public:
-
- VoidShow () {cout <"Multi core"<Endl ;}
-
-
- };
-
- ClassMulticoreb:PublicMulticore
-
- {
-
- Public:
- VoidShow () {cout <"Multi core B"<Endl ;}
-
- };
-
- // Factory
-
- ClassCorefactory
-
- {
-
- Public:
-
- VirtualSinglecore * createsinglecore () = 0;
- VirtualMulticore * createmulticore () = 0;
-
- };
-
- // Factory A, which is specially used to produce a-type Processor
-
- ClassFactorya:PublicCorefactory
-
- {
-
- Public:
- Singlecore * createsinglecore (){Return NewSinglecorea ();}
-
- Multicore * createmulticore (){Return NewMulticorea ();}
-
- };
-
- // Factory B, which is specially used to produce the processor type B
-
- ClassFactoryb:PublicCorefactory
-
- {
- Public:
-
- Singlecore * createsinglecore (){Return NewSinglecoreb ();}
-
- Multicore * createmulticore (){Return NewMulticoreb ();}
-
- };
So far, the factory model has been introduced. The Rational Rose 2003 software is used to provide UML diagrams of three factory models to deepen the impression.
UML diagram of simple factory mode:
UML diagram of the factory method:
Abstract The UML diagram of the factory model:
I have a blogArticleCopyright, reprinted please indicate the sourceHttp://blog.csdn.net/wuzhekai1985