• <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>

            積木

            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 閱讀(218) 評論(0)  編輯 收藏 引用 所屬分類: 設計模式
            久久综合丝袜日本网| 欧美久久久久久| 青青草原1769久久免费播放| 国产高清国内精品福利99久久| 久久久人妻精品无码一区| 国产aⅴ激情无码久久| 成人资源影音先锋久久资源网| 久久久91人妻无码精品蜜桃HD| 久久国产精品无| 天天久久狠狠色综合| 囯产精品久久久久久久久蜜桃| 66精品综合久久久久久久| 久久久久久久波多野结衣高潮| 99精品久久久久久久婷婷| 香蕉久久av一区二区三区| 色8激情欧美成人久久综合电| 久久国产精品久久| 欧洲成人午夜精品无码区久久| 精品多毛少妇人妻AV免费久久| 久久久久亚洲AV无码网站| 亚洲天堂久久久| 色婷婷久久综合中文久久一本| 久久精品国产亚洲AV嫖农村妇女| 久久综合久久性久99毛片| 亚洲国产成人久久精品动漫| 日产精品久久久一区二区| 久久精品国产欧美日韩99热| 久久久不卡国产精品一区二区| 久久精品国产一区二区三区日韩| 久久精品国产亚洲av水果派| 人妻无码中文久久久久专区| 亚洲精品乱码久久久久久久久久久久 | 久久国产AVJUST麻豆| 热RE99久久精品国产66热| 国产日韩久久免费影院 | 久久久久国产一区二区三区| 91精品国产高清91久久久久久| 久久久亚洲欧洲日产国码aⅴ| 日韩乱码人妻无码中文字幕久久 | 久久亚洲AV成人出白浆无码国产| 人妻精品久久无码专区精东影业|