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

            倚樓聽風雨,淡看江湖路

            朝花夕拾

            設計模式-策略模式

            // 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 on 2011-08-15 14:27 小閔 閱讀(1387) 評論(1)  編輯 收藏 引用 所屬分類: 設計模式

            評論

            # re: 設計模式-策略模式 2011-08-15 16:43 他她女鞋

            這個倒不是很了解。  回復  更多評論   

            <2011年8月>
            31123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            導航

            統計

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            国产亚州精品女人久久久久久 | 婷婷综合久久狠狠色99h| 丰满少妇高潮惨叫久久久| 久久国产香蕉视频| 办公室久久精品| 国产精品午夜久久| 99热精品久久只有精品| 91精品国产综合久久久久久| 久久99久久成人免费播放| 久久国产亚洲精品无码| 精品久久久久久久国产潘金莲| 人人狠狠综合久久亚洲88| 久久久久99精品成人片试看| 久久精品国产99久久久香蕉| 久久棈精品久久久久久噜噜| 青青热久久国产久精品 | 久久综合九色欧美综合狠狠| 久久久久成人精品无码中文字幕| 久久久久亚洲av成人网人人软件| 国产99久久久久久免费看| 国产99精品久久| 国产亚洲精午夜久久久久久| 久久99精品国产自在现线小黄鸭| 久久久久久久精品妇女99| 亚洲女久久久噜噜噜熟女| 久久亚洲国产成人影院| 国产69精品久久久久观看软件| 久久嫩草影院免费看夜色| 香蕉久久夜色精品国产尤物| 亚洲午夜精品久久久久久人妖| 久久精品国产精品国产精品污| 日本免费一区二区久久人人澡| 久久综合久久综合久久| 久久久久女教师免费一区| 久久天天日天天操综合伊人av| 伊人色综合久久天天人守人婷| 中文成人无码精品久久久不卡 | 国产精品免费久久| 亚洲国产精品久久久天堂| 国产精品对白刺激久久久| 99久久精品免费|