Mike's blog

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 23 Stories :: 83 Comments :: 0 Trackbacks

常用链接

留言簿(17)

我参与的团队

搜索

  •  

最新评论

模板方法模式是一种很简单但却很常见的设计模式,它常常在不知不觉中被使用。
概念:定义一个操作中的算法的骨架,而将具体的执行步骤延迟到子类中。模板方法使得子类可以不改变算法的结构而只是重新定义算法的步骤,使得不同步骤产生不同的效果。
说的更简单点,就是执行的框架由基类来定,而框架中的方法及执行顺序由子类来重定义。
UML类图:
 
上图的代码很好实现,就是基类的模板函数中调用了operation1和operation2中的一个或两个,而子类则重载了那两个方法。

下面给出一个常用的简单多线程工作模式,具体的工作由实际的调用者决定。
为方便,将代码都实现在一个文件里:
  1#include <iostream>
  2#include <pthread.h>
  3#include <unistd.h>
  4
  5using namespace std;
  6 
  7/**
  8 * @class BaseThread 
  9 * @brief The Base class for TemplateMethod DesignPattern.
 10 * @author wei.chen (2010-8-7)
 11 */

 12class BaseThread
 13{
 14public:
 15    BaseThread(): m_runThread(0)
 16    {
 17        cout << "BaseThread's constructor" << endl;
 18    }

 19
 20    virtual ~BaseThread()
 21    {
 22        cout << "BaseThread's destructor" << endl;
 23    }

 24
 25    /**
 26     * @fn run 
 27     * @brief Running as the thread's starting.
 28     * @param pObject The Sub-class's object.
 29     * @author wei.chen (2010-8-7)
 30     */

 31    static void* run(void* pObject)
 32    {
 33        cout << "running" << endl;
 34        BaseThread* pThread = static_cast<BaseThread*>(pObject);
 35        pThread->work();
 36        return NULL;
 37    }

 38
 39    /// Template method, no-virtual.
 40    int init()
 41    {
 42        cout << "Initilize" << endl;
 43        return pthread_create(&m_runThread, NULL, run, this);
 44    }

 45
 46protected:
 47
 48    /// Need to be overloaded in Sub-class.
 49    virtual int work()
 50    {
 51        cout << "The real method of working for BaseThread" << endl;
 52        return 0;
 53    }

 54
 55private:
 56    pthread_t m_runThread;
 57}
;
 58 
 59class TestThread1 : public BaseThread
 60{
 61public:
 62    TestThread1()
 63    {
 64        cout << "TestThread1's constructor" << endl;
 65    }

 66
 67    virtual ~TestThread1()
 68    {
 69        cout << "TestThread1's destructor" << endl;
 70    }

 71 
 72protected:
 73    virtual int work()
 74    {
 75        cout << "working for test1" << endl;
 76        return 0;
 77    }

 78}
;
 79
 80class TestThread2 : public BaseThread
 81{
 82public:
 83    TestThread2()
 84    {
 85        cout << "TestThread2's constructor" << endl;
 86    }

 87    virtual ~TestThread2()
 88    {
 89        cout << "TestThread2's destructor" << endl;
 90    }

 91 
 92protected:
 93    virtual int work()
 94    {
 95        cout << "working for test2" << endl;
 96        return 0;
 97    }

 98}
;
 99 
100int main()
101{
102    BaseThread* test1 = new TestThread1();
103    test1->init();
104
105    cout << endl;
106
107    BaseThread* test2 = new TestThread2();
108    test2->init();
109 
110    while (1{
111        pause();
112    }

113
114    return 0;
115}


输出:
BaseThread's constructor
TestThread1's constructor
Initilize...
running...
working for test1

BaseThread's constructor
TestThread2's constructor
Initilize...
running...
working for test2

posted on 2010-08-09 18:09 老狼 阅读(680) 评论(0)  编辑 收藏 引用 所属分类: C/C++

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