C++实现Behavioral - Template Method模式

 

在一个方法中定义一个算法的框架,并将该算法的某些步骤,延迟到子类实现。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类图:

 

业务示例:

测试各种不同的小汽车。

 

//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类图:

 

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

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值