The design patterns in the software field provide developers with an effective way to use expert design experience. The design patterns use the important features of object-oriented programming languages: encapsulation, inheritance, and polymorphism. It may be a long process to truly comprehend the essence of the design patterns, which 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]
Enum CTYPE {COREA, COREB };
Class SingleCore
{
Public:
Virtual void Show () = 0;
};
// Single-core
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, which can be determined internally
Class Factory
{
Public:
SingleCore * CreateSingleCore (enum CTYPE ctype)
{
If (ctype = COREA) // factory internal judgment
Return new SingleCoreA (); // production core
Else if (ctype = COREB)
Return new SingleCoreB (); // production core B
Else
Return NULL;
}
};
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]
Class SingleCore
{
Public:
Virtual void Show () = 0;
};
// Single-core
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;
};
// A-core production plant
Class FactoryA: public Factory
{
Public:
SingleCoreA * CreateSingleCore () {return new SingleCoreA ;}
};
// B-core production plant
Class FactoryB: public Factory
{
Public:
SingleCoreB * CreateSingleCore () {return new SingleCoreB ;}
};
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. Specifically, this company opened two factories, one dedicated to producing a-Type Single-core processor, and the other dedicated to producing a-Type Single-core processor, the following is the implementation code.
[Cpp]
// Single-core
Class SingleCore
{
Public:
Virtual void Show () = 0;
};
Class SingleCoreA: public SingleCore
{
Public:
Void Show () {cout <"Single Core A" <endl ;}
};
Class SingleCoreB: public SingleCore
{
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, which is specially used to produce A-type Processor
Class FactoryA: public CoreFactory
{
Public:
SingleCore * CreateSingleCore () {return new SingleCoreA ();}
MultiCore * CreateMultiCore () {return new MultiCoreA ();}
};
// Factory B, which is specially used to produce the processor type B
Class FactoryB: public CoreFactory
{
Public:
SingleCore * CreateSingleCore () {return new SingleCoreB ();}
MultiCore * CreateMultiCore () {return new MultiCoreB ();}
};
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: