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

            導航

            統計

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            国产精品一区二区久久精品无码| 欧美熟妇另类久久久久久不卡| 久久电影网2021| 精品免费久久久久国产一区| 欧美成a人片免费看久久| 伊人久久久AV老熟妇色| 亚洲午夜精品久久久久久人妖| 久久久久亚洲AV综合波多野结衣 | 久久99精品久久久久子伦| 久久―日本道色综合久久| 色综合久久夜色精品国产| av无码久久久久不卡免费网站 | 久久中文精品无码中文字幕| 精品国产乱码久久久久软件 | 久久久久免费看成人影片| 欧美精品一区二区久久| 91精品国产91久久| 久久精品国产亚洲AV香蕉| 伊人久久国产免费观看视频| 国产精品久久久久天天影视| 久久WWW免费人成一看片| 麻豆国内精品久久久久久| 久久综合中文字幕| 国产91色综合久久免费分享| 99久久精品国产一区二区 | 伊人久久大香线蕉av不卡| 久久精品国产精品亜洲毛片| 国产综合久久久久| 久久久久久精品成人免费图片| 久久国产综合精品五月天| 亚洲成色999久久网站| 国产V综合V亚洲欧美久久| 久久er99热精品一区二区| 亚洲国产精品无码久久一线| 99精品国产综合久久久久五月天| 久久午夜福利电影| 久久亚洲国产精品五月天婷| 香蕉久久永久视频| 国内精品久久国产| 久久综合国产乱子伦精品免费| 色偷偷久久一区二区三区|