• <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 小閔 閱讀(1387) | 評論 (1)編輯 收藏

            僅列出標題  
            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            導航

            統計

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            无遮挡粉嫩小泬久久久久久久 | 国产精品99久久精品| 精品免费tv久久久久久久| 久久99热国产这有精品| 久久精品国产99国产精品| 日韩欧美亚洲综合久久| 成人妇女免费播放久久久| 很黄很污的网站久久mimi色| 狠狠色丁香婷婷久久综合| 久久精品国产半推半就| 色婷婷狠狠久久综合五月| 久久无码人妻一区二区三区| 久久国产成人午夜aⅴ影院| 久久久噜噜噜久久熟女AA片| 久久综合成人网| 国产91色综合久久免费| 免费无码国产欧美久久18| 91麻精品国产91久久久久| 亚洲中文字幕无码久久2020| 国产激情久久久久影院小草 | 久久综合九色综合久99| 亚洲精品无码久久久久sm| 久久夜色撩人精品国产小说| 女人香蕉久久**毛片精品| 亚洲第一极品精品无码久久| 久久国产免费直播| 中文精品久久久久国产网址| 久久精品欧美日韩精品| 久久精品国产亚洲AV蜜臀色欲 | 久久久久九九精品影院| 99久久国产免费福利| 久久人人爽人人爽人人AV东京热| 伊人久久大香线蕉成人| 无码精品久久一区二区三区| 国产91久久综合| 成人国内精品久久久久影院VR| 久久天天躁狠狠躁夜夜网站| 久久这里只有精品18| 久久精品无码午夜福利理论片| 亚洲AV无码一区东京热久久| 久久综合九色综合网站|