模版方法模式(c++)
#include<iostream> using namespace std; class DrinkTemplate { public: virtual void BoilWater() = 0;//煮开水 virtual void Brew() = 0;//冲泡 virtual void PourInCup() = 0;//倒入杯中 virtual void AddSomeThing() = 0;//加料 //模板方法 void Make() { this->BoilWater(); this->Brew(); PourInCup(); AddSomeThing(); } }; //制作茶水 class Tea :public DrinkTemplate { public: virtual void BoilWater() { cout << "煮开水" << endl; } void Brew() { cout << "冲泡茶叶" << endl; } void PourInCup() { cout << "茶水导入杯中" << endl; } void AddSomeThing() { cout << "加入柠檬" << endl; } }; int main() { DrinkTemplate* dt=new Tea; dt->Make(); delete dt; dt = nullptr; cin.get(); return 0; }