Factory Mode of C++ Design Mould

Keywords: Programming

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.

  1. enum CTYPE {COREA, COREB};     
  2. class SingleCore    
  3. {    
  4. public:    
  5.     virtual void Show() = 0;  
  6. };    
  7. //Mononuclear A  
  8. class SingleCoreA: public SingleCore    
  9. {    
  10. public:    
  11.     void Show() { cout<<"SingleCore A"<<endl; }    
  12. };    
  13. //Mononuclear B  
  14. class SingleCoreB: public SingleCore    
  15. {    
  16. public:    
  17.     void Show() { cout<<"SingleCore B"<<endl; }    
  18. };    
  19. //The only factory that can produce two types of processor cores for internal judgment.  
  20. class Factory    
  21. {    
  22. public:     
  23.     SingleCore* CreateSingleCore(enum CTYPE ctype)    
  24.     {    
  25.         if(ctype == COREA) //Factory internal judgment  
  26.             return new SingleCoreA(); //Producing Nuclear A  
  27.         else if(ctype == COREB)    
  28.             return new SingleCoreB(); //Production of nuclear B  
  29.         else    
  30.             return NULL;    
  31.     }    
  32. };    

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.

  1. class SingleCore    
  2. {    
  3. public:    
  4.     virtual void Show() = 0;  
  5. };    
  6. //Mononuclear A  
  7. class SingleCoreA: public SingleCore    
  8. {    
  9. public:    
  10.     void Show() { cout<<"SingleCore A"<<endl; }    
  11. };    
  12. //Mononuclear B  
  13. class SingleCoreB: public SingleCore    
  14. {    
  15. public:    
  16.     void Show() { cout<<"SingleCore B"<<endl; }    
  17. };    
  18. class Factory    
  19. {    
  20. public:    
  21.     virtual SingleCore* CreateSingleCore() = 0;  
  22. };    
  23. //A Nuclear Plant  
  24. class FactoryA: public Factory    
  25. {    
  26. public:    
  27.     SingleCoreA* CreateSingleCore() { return new SingleCoreA; }    
  28. };    
  29. //A plant that produces B nuclei.  
  30. class FactoryB: public Factory    
  31. {    
  32. public:    
  33.     SingleCoreB* CreateSingleCore() { return new SingleCoreB; }    
  34. };    

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.

  1. //Mononuclear  
  2. class SingleCore     
  3. {    
  4. public:    
  5.     virtual void Show() = 0;  
  6. };    
  7. class SingleCoreA: public SingleCore      
  8. {    
  9. public:    
  10.     void Show() { cout<<"Single Core A"<<endl; }    
  11. };    
  12. class SingleCoreB :public SingleCore    
  13. {    
  14. public:    
  15.     void Show() { cout<<"Single Core B"<<endl; }    
  16. };    
  17. //Multi-core  
  18. class MultiCore      
  19. {    
  20. public:    
  21.     virtual void Show() = 0;  
  22. };    
  23. class MultiCoreA : public MultiCore      
  24. {    
  25. public:    
  26.     void Show() { cout<<"Multi Core A"<<endl; }    
  27.     
  28. };    
  29. class MultiCoreB : public MultiCore      
  30. {    
  31. public:    
  32.     void Show() { cout<<"Multi Core B"<<endl; }    
  33. };    
  34. //Factory  
  35. class CoreFactory      
  36. {    
  37. public:    
  38.     virtual SingleCore* CreateSingleCore() = 0;  
  39.     virtual MultiCore* CreateMultiCore() = 0;  
  40. };    
  41. //Factory A, a processor specially designed for the production of Model A.  
  42. class FactoryA :public CoreFactory    
  43. {    
  44. public:    
  45.     SingleCore* CreateSingleCore() { return new SingleCoreA(); }    
  46.     MultiCore* CreateMultiCore() { return new MultiCoreA(); }    
  47. };    
  48. //Factory B, a processor specially designed for the production of Model B.  
  49. class FactoryB : public CoreFactory    
  50. {    
  51. public:    
  52.     SingleCore* CreateSingleCore() { return new SingleCoreB(); }    
  53.     MultiCore* CreateMultiCore() { return new MultiCoreB(); }    
  54. };   

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:



Posted by TylerL on Thu, 27 Jun 2019 17:33:02 -0700