定义一个操作中算法的框架,而将一些步骤延迟到子类中。模板方法模式使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。

class TemplateDrink{
public:
    virtual void boilWater() = 0;
    virtual void drew() = 0;
    virtual void pourCup() = 0;
    virtual void addSomething() = 0;
    
    void make(){
        boilWater();
        drew();
        pourCup();
        addSomething();
    }
};
class Tea : public TemplateDrink{
public:
    void boilWater() override{
        cout << "煮山泉水..." << endl;
    }
    void drew() override{
        cout << "冲龙井茶..." << endl;
    }
    void pourCup() override{
        cout << "倒入茶杯中..." << endl;
    }
    void addSomething() override{
        cout << "加点柠檬..." << endl;
    }
};
class Coffee : public TemplateDrink{
public:
    void boilWater() override{
        cout << "煮矿泉水..." << endl;
    }
    void drew() override{
        cout << "冲雀巢咖啡..." << endl;
    }
    void pourCup() override{
        cout << "倒入咖啡杯中..." << endl;
    }
    void addSomething() override{
        cout << "加点糖和牛奶..." << endl;
    }
};
int main()
{
    Tea myTea;
    myTea.make();
    cout << "-------------------" << endl;
    Coffee myCoffee;
    myCoffee.make();
    return 0;
}