积木

No sub title

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  140 Posts :: 1 Stories :: 11 Comments :: 0 Trackbacks

常用链接

留言簿(1)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

转载自:http://patmusing.blog.163.com/blog/static/1358349602010150231168/

在一个方法中定义一个算法的框架,并将该算法的某些步骤,延迟到子类实现。Template Method使得子类可以重新定义一个算法中的某些特定的步骤,而无需改变整个算法的结构。

“Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.” - GoF

换言之,Template Method提供一个方法,以允许子类重写该方法的一部分,而无需重写整个子类。

比如,对于某一项任务,如果它有一个复杂的成员函数,并且该成员函数可以分成几个步骤,同时这几个步骤构成成员函数的整体结构式稳定的,但各个子步骤却有很多改变的需求,这样的情形下就特别适合使用Template MethodTemplate Method设计模式就是在确定稳定的成员函数组成结构的前提下,应对各个子步骤的变化。

Template Method模式之UML类图:

25. C++实现Behavioral - Template Method模式 - 玄机逸士 - 玄机逸士博客

业务示例:

测试各种不同的小汽车。

//TemplateMethod.h

#include <iostream>

using namespace std;

class TestVehicle

{

public:

void test() // 测试。这就是Template Method。它一共由6个步骤按照一定的时间

{ // 顺序组成,但各个步骤的实现被延迟到TestVehicle的子类

cout << "Start to test...." << endl; // 模拟固定部分的代码

start_up(); // 启动

blow_horn(); // 按喇叭

run(); // 行驶

turn(); // 转弯

brake(); // 刹车

stop(); // 停车

cout << "Test finished..." << endl; // 模拟固定部分的代码

}

virtual ~TestVehicle()

{

cout << "in the destructor of TestVehicle..." << endl;

}

protected:

virtual void start_up() = 0;

virtual void blow_horn() = 0;

virtual void run() = 0;

virtual void turn() = 0;

virtual void brake() = 0;

virtual void stop() = 0;

};

// 测试帕沙特

class TestPassat : public TestVehicle

{

public:

~TestPassat()

{

cout << "in the destructor of TestPassat..." << endl;

}

protected:

void start_up()

{

cout << "--- Passat:\tstart up ---" << endl; // 模拟启动Passat

}

void blow_horn()

{

cout << "--- Passat:\tblow the horn ---" << endl; // 模拟按Passat的喇叭

}

void run()

{

cout << "--- Passat:\trun ---" << endl; // 模拟Passat行驶

}

void turn()

{

cout << "--- Passat:\ttrun ---" << endl; // 模拟Passat转弯

}

void brake()

{

cout << "--- Passat:\tbrake ---" << endl; // 模拟Passat刹车

}

void stop()

{

cout << "--- Passat:\tstop ---" << endl; // 模拟Passat停车

}

};

// 测试捷达

class TestJetta : public TestVehicle

{

public:

~TestJetta()

{

cout << "in the destructor of TestJetta..." << endl;

}

protected:

void start_up()

{

cout << "--- Jetta:\tstart up ---" << endl; // 模拟按Jetta的喇叭

}

void blow_horn()

{

cout << "--- Jetta:\tblow the horn ---" << endl; // 模拟按Jetta的喇叭

}

void run()

{

cout << "--- Jetta:\trun ---" << endl; // 模拟Jetta行驶

}

void turn()

{

cout << "--- Jetta:\ttrun ---" << endl; // 模拟Jetta转弯

}

void brake()

{

cout << "--- Jetta:\tbrake ---" << endl; // 模拟Jetta刹车

}

void stop()

{

cout << "--- Jetta:\tstop ---" << endl; // 模拟Jetta停车

}

};

// TemplateMethod.cpp

#include "TemplateMethod.h"

int main(int argc, char** argv)

{

// 测试帕沙特

TestVehicle *tvPassat = new TestPassat();

tvPassat->test();

cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;

// 测试捷达

TestVehicle *tvJetta = new TestJetta();

tvJetta->test();

cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;

delete tvPassat;

delete tvJetta;

return 0;

}

运行结果:

Start to test....

--- Passat: start up ---

--- Passat: blow the horn ---

--- Passat: run ---

--- Passat: trun ---

--- Passat: brake ---

--- Passat: stop ---

Test finished...

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Start to test....

--- Jetta: start up ---

--- Jetta: blow the horn ---

--- Jetta: run ---

--- Jetta: trun ---

--- Jetta: brake ---

--- Jetta: stop ---

Test finished...

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

in the destructor of TestPassat...

in the destructor of TestVehicle...

in the destructor of TestJetta...

in the destructor of TestVehicle...

上述程序的UML类图:

25. C++实现Behavioral - Template Method模式 - 玄机逸士 - 玄机逸士博客

Template Method模式应该是GoF给出的23个模式中相对简单的一个。

posted on 2013-03-08 15:47 Jacc.Kim 阅读(207) 评论(0)  编辑 收藏 引用 所属分类: 设计模式

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理