[C ++] design pattern template model

Behavioral patterns

14) template model

This paper reference:

https://www.cnblogs.com/qq-361807535/p/6854191.html

Template model and appearance somewhat similar mode, the process is encapsulated. But both the implementation and the focus is not the same.
The appearance of various types of multi-mode implementation is cooperating together to complete one thing, so we use a function to encapsulate these operations, (this function is in a class).
Template pattern implementation is more a function of a combination of a class of complete thing. Definition of a skeleton algorithm operation delay some steps to subclasses, subclasses such that the template method may not change the structure of certain steps of the algorithm of the algorithm to redefine.

To cook, for example, whether Chinese or Western food, cooking steps are, a ready food, two pot, the three dishes

class cooking{
public:
    cooking(){}
    virtual void prepareMaterial() = 0; //准备原材料
    virtual void cook() = 0; //做饭
    virtual void washDishes() = 0; //洗碗
};

class ChinesecCooking : public cooking{
public:
    ChinesecCooking():cooking(){}
    void prepareMaterial(){
        cout<<"准备米饭,菜和肉,以及各种调料"<<endl;
    }
    void cook(){
        cout<<"中餐:炒、煎、炸、蒸、焖"<<endl;
    }
    void washDishes(){
        cout<<"洗碗筷"<<endl;
    }
};

class WesternCooking : public cooking{
public:
    WesternCooking():cooking(){}
    void prepareMaterial(){
        cout<<"准备土豆,面包、西兰花和pasta"<<endl;
    }
     void cook(){
         cout<<"西餐:水煮水煮水煮"<<endl;
     }
    
    void washDishes(){
        cout<<"洗盘子和刀叉"<<endl;
    }
};

int main(){
    ChinesecCooking chinacook = ChinesecCooking();
    chinacook.cook();
    
    WesternCooking westcook = WesternCooking();
    westcook.cook();
    
}

The final result is:

Guess you like

Origin www.cnblogs.com/corineru/p/12031817.html