• <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++博客 :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              140 Posts :: 1 Stories :: 11 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            轉(zhuǎn)載自:http://patmusing.blog.163.com/blog/static/1358349602010150231168/

            在一個(gè)方法中定義一個(gè)算法的框架,并將該算法的某些步驟,延遲到子類實(shí)現(xiàn)。Template Method使得子類可以重新定義一個(gè)算法中的某些特定的步驟,而無(wú)需改變整個(gè)算法的結(jié)構(gòu)。

            “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提供一個(gè)方法,以允許子類重寫(xiě)該方法的一部分,而無(wú)需重寫(xiě)整個(gè)子類。

            比如,對(duì)于某一項(xiàng)任務(wù),如果它有一個(gè)復(fù)雜的成員函數(shù),并且該成員函數(shù)可以分成幾個(gè)步驟,同時(shí)這幾個(gè)步驟構(gòu)成成員函數(shù)的整體結(jié)構(gòu)式穩(wěn)定的,但各個(gè)子步驟卻有很多改變的需求,這樣的情形下就特別適合使用Template Method。Template Method設(shè)計(jì)模式就是在確定穩(wěn)定的成員函數(shù)組成結(jié)構(gòu)的前提下,應(yīng)對(duì)各個(gè)子步驟的變化。

            Template Method模式之UML類圖:

            25. C++實(shí)現(xiàn)Behavioral - Template Method模式 - 玄機(jī)逸士 - 玄機(jī)逸士博客

            業(yè)務(wù)示例:

            測(cè)試各種不同的小汽車。

            //TemplateMethod.h

            #include <iostream>

            using namespace std;

            class TestVehicle

            {

            public:

            void test() // 測(cè)試。這就是Template Method。它一共由6個(gè)步驟按照一定的時(shí)間

            { // 順序組成,但各個(gè)步驟的實(shí)現(xiàn)被延遲到TestVehicle的子類

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

            start_up(); // 啟動(dòng)

            blow_horn(); // 按喇叭

            run(); // 行駛

            turn(); // 轉(zhuǎn)彎

            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;

            };

            // 測(cè)試帕沙特

            class TestPassat : public TestVehicle

            {

            public:

            ~TestPassat()

            {

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

            }

            protected:

            void start_up()

            {

            cout << "--- Passat:\tstart up ---" << endl; // 模擬啟動(dòng)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轉(zhuǎn)彎

            }

            void brake()

            {

            cout << "--- Passat:\tbrake ---" << endl; // 模擬Passat剎車

            }

            void stop()

            {

            cout << "--- Passat:\tstop ---" << endl; // 模擬Passat停車

            }

            };

            // 測(cè)試捷達(dá)

            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轉(zhuǎn)彎

            }

            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)

            {

            // 測(cè)試帕沙特

            TestVehicle *tvPassat = new TestPassat();

            tvPassat->test();

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

            // 測(cè)試捷達(dá)

            TestVehicle *tvJetta = new TestJetta();

            tvJetta->test();

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

            delete tvPassat;

            delete tvJetta;

            return 0;

            }

            運(yùn)行結(jié)果:

            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++實(shí)現(xiàn)Behavioral - Template Method模式 - 玄機(jī)逸士 - 玄機(jī)逸士博客

            Template Method模式應(yīng)該是GoF給出的23個(gè)模式中相對(duì)簡(jiǎn)單的一個(gè)。

            posted on 2013-03-08 15:47 Jacc.Kim 閱讀(225) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 設(shè)計(jì)模式
            精品无码久久久久久尤物| 国产激情久久久久影院老熟女| 国产成人精品综合久久久久| 久久亚洲国产最新网站| 一本一道久久综合狠狠老| 999久久久免费国产精品播放| 伊人久久大香线蕉精品不卡| 久久午夜伦鲁片免费无码| 久久婷婷五月综合成人D啪| 蜜臀av性久久久久蜜臀aⅴ麻豆 | 亚洲婷婷国产精品电影人久久| 日韩精品久久久久久久电影蜜臀| 国产午夜精品久久久久九九电影| 久久久久久精品无码人妻| 中文字幕成人精品久久不卡| 久久久久久综合网天天| 久久久精品久久久久久| 免费精品99久久国产综合精品| 伊人色综合久久天天网| 久久se精品一区二区影院| 国产精品久久久天天影视| 久久狠狠爱亚洲综合影院| 少妇被又大又粗又爽毛片久久黑人| 久久99精品久久久久婷婷| 超级97碰碰碰碰久久久久最新| 久久精品这里只有精99品| 久久天堂电影网| 久久97精品久久久久久久不卡 | 久久99热只有频精品8| 人人妻久久人人澡人人爽人人精品| 久久久久久A亚洲欧洲AV冫| 精品久久久久久中文字幕| 99久久精品国内| 99久久99久久| 色综合久久88色综合天天 | 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 狠狠色丁香久久综合五月| 欧美一区二区精品久久| 99国产精品久久| 国产ww久久久久久久久久| 久久久久国色AV免费看图片|