When learning the C++ factory model, I found a well-understood article, so I reprinted the collection. http://blog.csdn.net/wuzhekai1985 . Thank you for sharing!
Design patterns in the field of software provide an effective way for developers to use expert design experience. The design pattern uses the important characteristics of object-oriented programming language: encapsulation, inheritance and polymorphism. It is a long process to truly understand the essence of the design pattern, which requires a lot of practical experience. Recently, I read the book of design patterns. For each pattern, I wrote a small example with C++ to deepen my understanding. It mainly refers to two books, Dahua Design Patterns and Design Patterns: The Foundation of Reusable Object-Oriented Software. This paper introduces the realization of factory mode.
The factory mode belongs to the creation mode, which can be roughly divided into three categories: simple factory mode, factory method mode and abstract factory mode. Sounds similar. It's all factory mode. Following is an introduction. First of all, the simple factory model is introduced. Its main feature is that it needs to make judgments in the factory class, so as to create corresponding products. When new products are added, factory classes need to be modified. It's a bit abstract, as an example shows. There is a manufacturer that produces processor cores. It only has one factory and can produce two types of processor cores. What kind of processor cores do customers need? Be sure to tell the production plant explicitly. An implementation scheme is given below.
The main disadvantage of this design has also been mentioned before, that is, to add new nuclear types, the factory classes need to be modified. This violates the principle of open and closed: software entities (classes, modules, functions) can be extended, but not modified. Thus, the factory method model emerged. The so-called factory method pattern refers to the definition of an interface for creating objects, allowing subclasses to decide which class to instantiate. Factory Method delays the instantiation of a class to its subclasses.
Sounds abstract, but let's use the example just now. The manufacturer of processor cores made a lot of money, so he decided to open another factory dedicated to the production of model B single-core, and the original factory dedicated to the production of model A single-core. At this time, the customer is to find a good factory, such as type A core, find factory A; otherwise, find factory B, no longer need to tell the factory what type of processor core. An implementation scheme is given below.
The factory method model also has its drawbacks. For each additional product, an object factory needs to be added. If the company develops rapidly and introduces many new processor cores, it will need to open new factories accordingly. In C++ implementation, it is necessary to define one factory class after another. Obviously, the factory method pattern requires more class definitions than the simple factory pattern.
Since there are simple factory model and factory method model, why should there be abstract factory model? What exactly does it do? For example, the company's technology is improving, producing not only single-core processors, but also multi-core processors. Now the simple factory model and the factory method model are far behind. The abstract factory model has come on the scene. It is defined as providing an interface for creating a series of related or interdependent objects without specifying their specific classes. In this way, the company still has two factories, one specializing in the production of model A single-core multi-core processors, and the other specializing in the production of model B single-core multi-core processors. The code for implementation is given below.
At this point, the factory model has been introduced. Using Rational Rose 2003 software, UML diagrams of three factory patterns are given to deepen the impression.
UML diagrams of simple factory patterns:
UML diagrams of factory methods:
UML diagrams of abstract factory patterns: