Design Patterns (1) - Factory Pattern C++ Implementation

A few days ago, I learned design patterns (GRASP pattern) and 23 classic patterns of GoF.
Next , I will review 2 design patterns every week, add code, share with you and deepen
my understanding of what I have learned.

Design patterns in my eyes

The role of a design pattern in an engineering project is like an algorithm template in an algorithm competition. An algorithm template is the code representative of a classic question type. If you encounter such a problem in the competition, you can solve it with the core idea of ​​the algorithm template, while the design pattern It is a set of repeated, most known, categorized, summaries of code design experience.
Design patterns use the important features of object-oriented programming languages: encapsulation, inheritance, polymorphism, and truly understanding the essence of design patterns may be a long process, requiring a lot of practical experience.
Factory patterns include simple factories, factory methods and abstract factories The difference, but what really counts as a design pattern is the abstract factory. The simple factory is really just a more natural repackage.
Let's talk about the factory pattern progressively layer by layer.

Simple Factory Pattern

Now suppose that a company has made great achievements in the theory of unmanned vehicles, but due to economic reasons, it has only one production plant, which can produce ordinary four-wheeled cars and four-wheeled off-road vehicles. What type of unmanned vehicles the customer needs must be displayed. tell the production plant. An implementation scheme is given below.

    enum CAR_TYPE 
    {
        CAR_JIAO = 0,
        CAR_YUE,
        CAR_MAX
    };     
    class car
    {    
    public:    
        virtual void print() = 0;  
    };    
    //四轮轿车    
    class car_jiao: public car
    {    
    public:    
        void print() { cout<<"car jiaoche"<<endl; }    
    };    
    //四轮越野
    class car_yue: public car
    {    
    public:    
        void print() { cout<<"car yueye"<<endl; }    
    };    
    //唯一的工厂,可以生产两种型号的车,在内部判断    
    class Factory    
    {    
    public:     
        car* CreateScar(enum CAR_TYPE ctype)    
        {    
            if(ctype == CAR_JIAO) //工厂内部判断    
                return new CreateScar_JIAO(); //生产四轮轿车    
            else if(ctype == CAR_YUE)    
                return new CreateScar_YUE(); //生产四轮越野    
            else    
                return NULL;    
        }    
    };    

The disadvantage of this design is that if the company expands its business and produces four-wheeled sports cars, then the factory class needs to be modified, which violates the open closed principle: software entities (classes, modules, functions) can be extended, but not modified. Thus, the factory method pattern appeared. The so-called factory method pattern refers to defining an interface for creating objects and letting subclasses decide which class to instantiate. Factory Method delays the instantiation of a class to its subclasses.
It’s the same example just now. The self-driving car company is building a factory dedicated to the production of four-wheel off-road vehicles due to its growing reputation, so that for customers, you can go to any type of factory for the type of car you want. , you don't need to tell the factory what type of car you need, because the function of each factory class is fixed

    class car
    {    
    public:    
        virtual void print() = 0;  
    };    
    //四轮轿车    
    class car_jiao: public car    
    {    
    public:    
        void print() { cout<<"car jiache"<<endl; }    
    };    
    //四轮越野车    
    class car_yue: public car    
    {    
    public:    
        void print() { cout<<"car yueye"<<endl; }    
    };    
    class Factory    
    {    
    public:    
        virtual car* Createcar() = 0;  
    };    
    //生产四轮轿车的工厂    
    class Factory_jiao: public Factory    
    {    
    public:    
        Createcar_jiao* Createcar() { return new car_jiao; }    
    };    
    //生产四轮越野车的工厂    
    class Factory_yue: public Factory    
    {    
    public:    
        car_yue* Createcar() { return new car_yue; }    
    };    

Of course, the factory method is also flawed. Every time a product is added, an object factory needs to be added. In this way, even if the company has money, it cannot open a factory unlimitedly to produce these cars to meet sustainable demand. This abstract factory The pattern is a good solution to this problem.
Assuming that the company's theory is getting stronger and stronger, and then it has developed five-wheel, six-wheel, sedan, and off-road vehicles,
then Factory_jiao can produce four-wheel, five-wheel, six-wheel cars, and Factory_yue can
produce four-wheel, five-wheel, six-wheeled off-road vehicle

    //四轮
    class car_four     
    {    
    public:    
        virtual void print() = 0;  
    };    
    class car_Fourjiao: public car_four     
    {    
    public:    
        void print() { cout<<"car si lun jiao che"<<endl; }    
    };    
    class car_Fouryue:public car_four
    {    
    public:    
        void print() { cout<<"car si lun yue ye"<<endl; }    
    };    
    //五轮
    class car_five      
    {    
    public:    
        virtual void Show() = 0;  
    };    
    class car_fivejiao: public car_five      
    {    
    public:    
        void print() { cout<<"car wu lun jiao che"<<endl; }    

    };    
    class car_fiveyue: public car_five      
    {    
    public:    
        void print() { cout<<"car wu lun yue ye"<<endl; }    
    };    
    //工厂    
    class Factory      
    {    
    public:    
        virtual car_four* CreatFourcar() = 0;  
        virtual car_five* CreatFivecar() = 0;
    };    
    //轿车工厂 ,专门用来生产四轮,五轮的轿车    
    class Factory_jiao :public Factory      
    {    
    public:    
        car_four* CreateFourCar() { return new car_fourjiao();}   
        car_five* CreateFiveCar() { return new car_fivejiao();}    
    };    
    //越野车工厂 ,专门用来生产四轮,五轮的越野车 
    class Factory_yue :public Factory      
    {    
    public:    
        car_four* CreateFourCar() { return new car_fouryue();}   
        car_five* CreateFiveCar() { return new car_fiveyue();}    
    };      

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324886206&siteId=291194637