c++设计模式:模板方法模式(Template Method Pattern)

定义:模板方法模式在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中,模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。

钩子的定义:钩子是一种被声明在抽象类中的方法,但只有空的或者默认的实现。钩子的存在,可以让子类有能力对算法的不同点进行挂钩。究竟需不需要挂钩,则由子类自行决定。

场景:我们有一个咖啡厅,有一个专门的配方来调制咖啡喝茶,在调制的过程中的某些方法其实是比较类似的,步骤如下:

1、把水煮沸

2、用热水泡咖啡或茶

3、把饮料倒进杯子

4、在饮料内加入适当的调料,咖啡要加糖和牛奶,而茶需要加柠檬。在调料添加的时候,我们需要询问顾客是否需要添加调料,此时就会用到钩子。

类图:

c++设计模式:模板方法模式(Template Method Pattern)

 

c++代码如下:

#include <iostream>
using namespace std;

class CaffeineBeverage
{
public:
void prepareRecipe();
private:
virtual void brew() = 0;
virtual void addCondiments() = 0;
void boilWater();
void pourInCup();
virtual bool customerWantsCondiments(); // 钩子
};

class Coffee : public CaffeineBeverage
{
private:
void brew();
void addCondiments();
bool customerWantsCondiments();
};

class Tea : public CaffeineBeverage
{
private:
void brew();
void addCondiments();
bool customerWantsCondiments();
};

void CaffeineBeverage::prepareRecipe()
{
boilWater();
brew();
pourInCup();
if (customerWantsCondiments())
{
addCondiments();
}
}

void CaffeineBeverage::boilWater()
{
printf("把水煮沸n");
}

void CaffeineBeverage::pourInCup()
{
printf("倒入杯子中n");
}

bool CaffeineBeverage::customerWantsCondiments()
{
return true;
}

void Coffee::brew()
{
printf("用沸水冲泡咖啡粉n");
}

void Coffee::addCondiments()
{
printf("加糖和牛奶n");
}

bool Coffee::customerWantsCondiments()
{
char zAnswer;
printf("您的咖啡中是否需要牛奶和糖:");
fflush(stdin);
zAnswer = getchar();
zAnswer = toupper(zAnswer);
if ('Y'==zAnswer)
{
return true;
}
else if ('N'==zAnswer)
{
return false;
}
else
{
printf("输入错误,默认不加调料n");
return false;
}
}

void Tea::brew()
{
printf("用沸水浸泡茶叶n");
}
void Tea::addCondiments()
{
printf("加柠檬n");
}

bool Tea::customerWantsCondiments()
{
char zAnswer;
printf("您的茶中是否需要柠檬:");
fflush(stdin);
zAnswer = getchar();
zAnswer = toupper(zAnswer);
if ('Y'==zAnswer)
{
return true;
}
else if ('N'==zAnswer)
{
return false;
}
else
{
printf("输入错误,默认不加调料n");
return false;
}
}

int main()
{
Coffee coffee;
Tea tea;
coffee.prepareRecipe();
tea.prepareRecipe();
return 0;
}

运行结果:

把水煮沸
用沸水冲泡咖啡粉
倒入杯子中
您的咖啡中是否需要牛奶和糖:y
加糖和牛奶
把水煮沸
用沸水浸泡茶叶
倒入杯子中
您的茶中是否需要柠檬:n

 

解说:模板方法模式是一个很常见的模式,到处都是。在实际应用中,模板方法有许多的实现,而它们看起来并不一定和书上所讲的设计一致。这个设计常见,是因为对创建框架来说,这个模式简直棒极了,由框架控制如何做事情,而由使用框架的人来指定框架算法中的每个步骤的细节。

 

参考图书:《Head First 设计模式》

原文链接: https://www.cnblogs.com/osyun/archive/2012/02/09/2343999.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++设计模式:模板方法模式(Template Method Pattern)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/41209

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 下午6:06
下一篇 2023年2月8日 下午6:06

相关推荐