• <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>
            posts - 183,  comments - 10,  trackbacks - 0
            來自于《大話設計模式》
            模板方法模式:定義一個操作中的算法的骨架,而將一些步驟延遲到子類中。模板方法使得子類可以不改變一個算法的結構即可重定義該算法的某些特定步驟。

            盡量將公共操作上移到基類中,這樣便于修改,代碼復用性更強。

            UML 類圖:


            代碼實現 C++:
             1 #include <iostream>
             2 #include <string>
             3 using namespace std;
             4 
             5 class TestPaper
             6 {
             7 public:
             8     virtual void TestQuestion1()
             9     {
            10         cout << "TestQuestion1" << endl;
            11         cout << "Answer: " << Answer1() << endl;
            12     }
            13     virtual void TestQuestion2()
            14     {
            15         cout << "TestQuestion2" << endl;
            16         cout << "Answer: " << Answer2() << endl;
            17     }
            18     virtual void TestQuestion3()
            19     {
            20         cout << "TestQuestion3" << endl;
            21         cout << "answer: " << Answer3() << endl;
            22     }
            23     virtual const string Answer1() = 0
            24     {
            25         return "";
            26     }
            27     virtual const string Answer2() = 0
            28     {
            29         return "";
            30     }
            31     virtual const string Answer3() = 0
            32     {
            33         return "";
            34     }
            35 };
            36 
            37 class TestPaperA : public TestPaper
            38 {
            39 public:
            40     virtual const string Answer1()
            41     {
            42         return "a";
            43     }
            44     virtual const string Answer2()
            45     {
            46         return "b";
            47     }
            48     virtual const string Answer3()
            49     {
            50         return "c";
            51     }
            52 };
            53 
            54 class TestPaperB : public TestPaper
            55 {
            56 public:
            57     virtual const string Answer1()
            58     {
            59         return "c";
            60     }
            61     virtual const string Answer2()
            62     {
            63         return "b";
            64     }
            65     virtual const string Answer3()
            66     {
            67         return "a";
            68     }
            69 };
            70 
            71 int main()
            72 {
            73     TestPaperA a;
            74     a.TestQuestion1();
            75     a.TestQuestion2();
            76     a.TestQuestion3();
            77 
            78     TestPaperB b;
            79     b.TestQuestion1();
            80     b.TestQuestion2();
            81     b.TestQuestion3();
            82 
            83     TestPaper* p;
            84     p = new TestPaperA;
            85     p->TestQuestion1();
            86     p->TestQuestion2();
            87     p->TestQuestion3();
            88     delete p;
            89 
            90     p = new TestPaperB;
            91     p->TestQuestion1();
            92     p->TestQuestion2();
            93     p->TestQuestion3();
            94     delete p;
            95 
            96     return 0;
            97 }
            posted on 2011-04-25 16:41 unixfy 閱讀(176) 評論(0)  編輯 收藏 引用
            色婷婷噜噜久久国产精品12p| 精品国产乱码久久久久久郑州公司 | 国产亚洲综合久久系列| 热re99久久精品国产99热| 综合久久给合久久狠狠狠97色| 久久午夜夜伦鲁鲁片免费无码影视| 久久综合九色综合网站| 久久亚洲AV无码西西人体| 97久久精品无码一区二区天美| 免费一级欧美大片久久网| 精品久久久噜噜噜久久久 | 国产精品免费久久久久久久久| 免费精品久久天干天干| 久久99热这里只有精品国产| 无码国内精品久久人妻蜜桃| 欧美日韩中文字幕久久久不卡| 日韩精品久久无码人妻中文字幕| 久久精品国产欧美日韩| 成人免费网站久久久| 99久久精品免费看国产一区二区三区| 国产—久久香蕉国产线看观看| 久久婷婷五月综合色奶水99啪| 亚洲国产精品嫩草影院久久| 国产精品gz久久久| 中文字幕成人精品久久不卡| 国产亚洲美女精品久久久久狼| 久久丫精品国产亚洲av| 97久久国产综合精品女不卡 | 无码人妻久久一区二区三区| 欧美精品国产综合久久| 伊人久久亚洲综合影院| 亚洲精品无码专区久久同性男| 久久影院久久香蕉国产线看观看| 久久国产视频99电影| 久久精品国产99国产精品| 国内精品久久久久久麻豆| 久久精品一区二区| 久久精品国产一区二区三区 | 久久发布国产伦子伦精品 | 久久久久人妻精品一区三寸蜜桃| 精品久久综合1区2区3区激情|