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

            倚樓聽風雨,淡看江湖路

            朝花夕拾

            2011年8月15日

            設計模式-策略模式

            // strategy.cpp : 定義控制臺應用程序的入口點。
            //
            #include "stdafx.h"
            /********************************************************************
            created: 2011/08/15
            created: 15:8:2011   10:37
            file base: strategy
            file ext: cpp
            author: lost boy
            purpose: 設計模式-策略模式
            策略模式是指定義一系列的算法,把它們一個個封裝起來,并且使它們可相互替換。
            本模式使得算法可獨立于使用它的客戶而變化。也就是說這些算法所完成的功能一樣,
            對外的接口一樣,只是各自實現上存在差異。用策略模式來封裝算法,效果比較好。
            *********************************************************************/
            /*
            電腦戰術
            */
            class BotsTactics
            {
            public:
            virtual void DoTactics() = 0;
            };
            /*
            吹風流
            */
            class TacticsBlown : public BotsTactics
            {
            public:
            void DoTactics()
            {
            printf("bot use Blown tactics\n");
            }
            };
            /*
            熊流
            */
            class TacticsBears : public BotsTactics
            {
            public:
            void DoTactics()
            {
            printf("bot use Bears tactics\n");
            }
            };
            /*
            塔流
            */
            class TacticsTowerRush : public BotsTactics
            {
            public:
            void DoTactics()
            {
            printf("bot use TowerRush tactics\n");
            }
            };
            /*
            也是直接通過參數指定,只不過不是傳入指針,而是一個標簽。
            用戶只需要知道標簽
            */
            enum TATICS {BLOWN, BEAR, TR}; //標簽  
            class Bots
            {
            private:
            BotsTactics *pTatics_;
            public:
            Bots(enum TATICS ta)
            {
            if (ta == BLOWN)
            pTatics_ = new TacticsBlown;
            else if(ta == BEAR)
            pTatics_ = new TacticsBears;
            else if (ta == TR)
            pTatics_ = new TacticsTowerRush;
            else
            pTatics_ = NULL;
            }
            ~Bots()
            {
            if (pTatics_)
            {
            delete pTatics_;
            pTatics_ = NULL;
            }
            };
            public:
            void DoTactics()
            {
            pTatics_->DoTactics();
            }
            };
            /*
            利用模板實現。算法通過模板的實參指定。
            */
            template<class ta>
            class templateBots
            {
            private:
            ta ta_;
            public:
            templateBots(){}
            ~templateBots(){}
            public:
            void DoTactics()
            {
            ta_.DoTactics();
            }
            };
            int _tmain(int argc, _TCHAR* argv[])
            {
            Bots bot(BLOWN);
            bot.DoTactics();
            templateBots<TacticsTowerRush> bot1;
            bot1.DoTactics();
            return 0;
            }

            posted @ 2011-08-15 14:27 小閔 閱讀(1384) | 評論 (1)編輯 收藏

            僅列出標題  
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            導航

            統計

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            国产精品免费久久久久影院| 999久久久国产精品| 久久夜色精品国产噜噜亚洲a| 亚洲人成网站999久久久综合| 久久婷婷是五月综合色狠狠| 色8久久人人97超碰香蕉987| 精品999久久久久久中文字幕| 久久综合伊人77777| 久久亚洲精精品中文字幕| 精品综合久久久久久88小说| 亚洲级αV无码毛片久久精品| 国产精品18久久久久久vr| 亚洲精品乱码久久久久久蜜桃 | 国产69精品久久久久99| 国产免费久久精品丫丫| 久久久精品国产sm调教网站| 久久精品国产只有精品66| 麻豆亚洲AV永久无码精品久久| 久久国产成人午夜AV影院| 国产三级久久久精品麻豆三级| 久久嫩草影院免费看夜色| 久久r热这里有精品视频| 日产精品久久久一区二区| 色综合久久久久综合99| 久久国产视屏| 日日狠狠久久偷偷色综合免费 | 亚洲愉拍99热成人精品热久久| 狠狠久久亚洲欧美专区| 国产欧美久久久精品影院| 精品久久久久久无码人妻蜜桃| 国产成人精品久久一区二区三区 | 无码AV中文字幕久久专区| 无码伊人66久久大杳蕉网站谷歌| 情人伊人久久综合亚洲| 激情伊人五月天久久综合| 亚洲午夜久久久久久久久久| 2021久久精品免费观看| 国产一区二区久久久| 久久久久久精品无码人妻| 久久精品免费一区二区| 日韩乱码人妻无码中文字幕久久|