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

            僅列出標題  
            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            色综合久久中文色婷婷| 一本久久a久久精品亚洲| 国产激情久久久久影院| 久久激情五月丁香伊人| 中文字幕无码免费久久| 亚洲欧美精品伊人久久| 天天综合久久一二三区| 精品国产91久久久久久久 | 亚洲精品高清国产一线久久| 久久久亚洲欧洲日产国码aⅴ| 久久精品国产99国产电影网 | 亚洲欧美日韩中文久久| 久久青青草原国产精品免费| 久久亚洲AV成人无码| 久久国产影院| 国产999精品久久久久久| 久久综合精品国产二区无码| 亚洲精品无码专区久久同性男| 久久亚洲精品中文字幕三区| 亚洲国产另类久久久精品黑人| 久久天天躁狠狠躁夜夜2020老熟妇| 精品国产乱码久久久久久郑州公司 | 狠狠色丁香婷综合久久| 77777亚洲午夜久久多人| 亚洲综合久久夜AV | 亚洲精品NV久久久久久久久久| 国产精品热久久无码av| 国产精品VIDEOSSEX久久发布| 国产一区二区精品久久| 狠狠色噜噜狠狠狠狠狠色综合久久 | 久久久久久免费一区二区三区| 久久综合狠狠色综合伊人| 久久久精品久久久久影院| 韩国三级大全久久网站| 777米奇久久最新地址| 亚洲精品无码久久久久去q | 久久国产免费观看精品3| MM131亚洲国产美女久久| 亚洲精品国产美女久久久| 精品久久久久久无码专区不卡| 久久久久99精品成人片试看|