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

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

            導航

            統計

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            91精品观看91久久久久久| 亚洲色欲久久久综合网东京热| 伊人久久无码中文字幕| 亚洲国产视频久久| 亚洲精品国产综合久久一线| 久久精品亚洲精品国产欧美| 久久国产乱子伦精品免费午夜| 91麻豆精品国产91久久久久久| 久久久青草久久久青草| 日本久久久精品中文字幕| 国产AⅤ精品一区二区三区久久| 99久久精品久久久久久清纯| 国产99久久九九精品无码| 国产999精品久久久久久| 久久综合久久性久99毛片| 欧美激情精品久久久久久久| 久久精品视频网| 精品久久久久久久久久久久久久久| 国产成人久久精品二区三区| 久久午夜无码鲁丝片午夜精品| 久久久久一本毛久久久| 色综合久久中文字幕综合网| 2019久久久高清456| 久久久无码精品亚洲日韩按摩 | 热99re久久国超精品首页| 狠狠久久综合伊人不卡| 久久夜色精品国产| 中文字幕久久精品无码| 久久91亚洲人成电影网站| 久久精品无码一区二区三区免费| 久久精品国产精品亚洲精品| 国产精品无码久久综合| 欧美成a人片免费看久久| 狼狼综合久久久久综合网| 国产A级毛片久久久精品毛片| 久久精品久久久久观看99水蜜桃| 精品国产VA久久久久久久冰| 久久综合久久综合亚洲| 久久精品一区二区| 亚洲国产精品无码久久久不卡| 久久se精品一区二区影院|