• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Mike's blog

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              0 Posts :: 23 Stories :: 83 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(17)

            我參與的團隊

            搜索

            •  

            最新評論

            模板方法模式是一種很簡單但卻很常見的設計模式,它常常在不知不覺中被使用。
            概念:定義一個操作中的算法的骨架,而將具體的執(zhí)行步驟延遲到子類中。模板方法使得子類可以不改變算法的結構而只是重新定義算法的步驟,使得不同步驟產生不同的效果。
            說的更簡單點,就是執(zhí)行的框架由基類來定,而框架中的方法及執(zhí)行順序由子類來重定義。
            UML類圖:
             
            上圖的代碼很好實現(xiàn),就是基類的模板函數(shù)中調用了operation1和operation2中的一個或兩個,而子類則重載了那兩個方法。

            下面給出一個常用的簡單多線程工作模式,具體的工作由實際的調用者決定。
            為方便,將代碼都實現(xiàn)在一個文件里:
              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 老狼 閱讀(747) 評論(0)  編輯 收藏 引用 所屬分類: C/C++
            亚洲精品国产字幕久久不卡| 久久国产色AV免费看| 少妇久久久久久被弄到高潮| 亚洲欧洲久久av| 午夜天堂精品久久久久| 99久久精品免费看国产| 久久精品国产亚洲AV香蕉| 久久亚洲国产午夜精品理论片| 午夜精品久久久内射近拍高清 | 久久人人爽人人爽人人AV东京热| 69SEX久久精品国产麻豆| 亚洲午夜久久久| 国产高潮久久免费观看| 人妻无码αv中文字幕久久琪琪布| 久久精品国产国产精品四凭| 国内精品久久久久伊人av| 日本加勒比久久精品| 欧美伊香蕉久久综合类网站| 浪潮AV色综合久久天堂| 久久精品国产男包| 欧美性大战久久久久久| 国产一区二区精品久久凹凸 | 久久久综合香蕉尹人综合网| a高清免费毛片久久| 成人国内精品久久久久一区| 无码专区久久综合久中文字幕| 亚洲а∨天堂久久精品9966| 久久成人精品| 青青热久久国产久精品| 久久乐国产精品亚洲综合| 久久精品国产精品亚洲人人| 久久精品无码一区二区app| 久久本道综合久久伊人| 久久久久久国产精品美女| 久久亚洲天堂| 久久午夜无码鲁丝片秋霞| 久久精品中文无码资源站| 无码人妻久久久一区二区三区| 亚洲AV日韩AV永久无码久久| 无码人妻久久久一区二区三区 | 情人伊人久久综合亚洲|