模板方法模式

模式定义:

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

      模板就是一个方法。更具体的说,这个方法将算法定义成一组步骤,其中的任何步骤都可以是抽象的,由子类实现。这可以确保算法的结果保持不变,同时由子类提供部分实现。

模式结构:

举例:

      泡咖啡和泡茶步骤与基本相同,定义咖啡和茶的类如下:

class Coffee
{
public:
	void prepareRecipe()
	{
		boilWater();
		brewCoffeeGrinds();
		pourInCup();
		addSugarAndMilk();
	}

	void boilWater()
	{
		cout << "Boiling water" << endl;
	}

	void brewCoffeeGrinds()
	{
		cout << "Dripping Coffee through filter" << endl;
	}

	void pourCup()
	{
		cout << "Pouring into cup" <<endl;
	}

	void addSugarAndMilk()
	{
		cout << "Adding Sugar and Milk" << endl;
	}
};

class Tea
{
public:
	void prepareRecipe()
	{
		boilWater();
		brewReaBag();
		pourInCup();
		addLemon();
	}

	void boilWater()
	{
		cout << "Boiling water" << endl;
	}

	void brewReaBag()
	{
		cout << "Steeping the tea" << endl;
	}

	void pourCup()
	{
		cout << "Pouring into cup" <<endl;
	}

	void addLemon()
	{
		cout << "Adding Lemon" << endl;
	}
};

      可见有两份冲泡算法中都采用了把水煮沸和把饮料倒入杯子的算法,所以可以把他们放到Coffee和Tea的基类(新定义一个咖啡因类CaffeineBeverage.)中。还有两个算法并没有被放入基类,但可以将他们定义新的方法名称brew()和addCondiments()方法,并在子类中实现。

UML设计:

编程实现及执行结果:

#include <iostream>

using namespace std;

//定义咖啡因基类
class CaffeineBeverage
{
public:
	void prepareRecipe()
	{
		boilWater();
		brew();
		pourInCup();
		addCondiments();
	}

	void boilWater()
	{
		cout << "Boiling water" << endl;
	}

	void pourInCup()
	{
		cout << "Pouring into cup" <<endl;
	}

	virtual void brew(){}

	virtual void addCondiments(){}
};
//定义咖啡类
class Coffee : public CaffeineBeverage
{
public:
	void brew()
	{
		cout << "Dripping Coffee through filter" << endl;
	}

	void addCondiments()
	{
		cout << "Adding Sugar and Milk" << endl;
	}
};
//定义茶类
class Tea : public CaffeineBeverage
{
public:
	void brew()
	{
		cout << "Steeping the tea" << endl;
	}

	void addCondiments()
	{
		cout << "Adding Lemon" << endl;
	}
};
//客户代码
int main()
{
	Coffee coffee;
	cout << "Making coffee..." << endl;
	coffee.prepareRecipe();
	cout << endl << endl;

	Tea tea;
	cout << "Make tea...";
	tea.prepareRecipe();
	return 0;
}

执行结果如下:

Makingcoffee...

Boilingwater

DrippingCoffee through filter

Pouringinto cup

AddingSugar and Milk



Maketea...Boiling water

Steepingthe tea

Pouringinto cup

AddingLemon

请按任意键继续. . .

设计原则的应用:

      好莱坞原则:别调用(打电话)我们,我们会调用你。在好莱坞原则下,我们允许低层组件将自己挂钩到系统上,但是高层组件会决定什么时候和怎么样使用这些低层组件。如在模板方法中:当我们设计模板方法模式时,我们告诉子类,“不要调用我们,我们会调用你”。

文章导航