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

            積木

            No sub title

              C++博客 :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              140 Posts :: 1 Stories :: 11 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            轉(zhuǎn)載自:http://patmusing.blog.163.com/blog/static/1358349602010150224904/

            也稱為Policy模式。

            定義一系列算法,把他們一個(gè)個(gè)封裝起來(lái),并且使他們可以相互替換。該模式使得算法可以獨(dú)立于使用它的客戶而變化。

            “Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.”- GoF

            在軟件構(gòu)建過(guò)程中,某些對(duì)象使用的算法可能多種多樣,經(jīng)常改變,如果將這些算法都編碼到某個(gè)對(duì)象中,將會(huì)使該對(duì)象變得異常復(fù)雜;而且有時(shí)候支持不使用的算法也是一個(gè)性能負(fù)擔(dān)。Strategy設(shè)計(jì)模式就是在運(yùn)行時(shí)根據(jù)需要透明地更改對(duì)象的算法,將算法和對(duì)象本身解耦。

            我們?cè)诰幊讨校?jīng)常碰到這樣的情況:用不同的辦法去做同一件事情,比如,

            1. 將文件保存為不同的格式;

            2. 用不同的壓縮算法壓縮一個(gè)文本文件;

            3. 用不同的壓縮算法壓縮視頻文件

            4. 將同樣的數(shù)據(jù),用不同的圖形顯示出來(lái):折線圖、柱狀圖或者餅圖;

            5. 將用某種語(yǔ)言寫(xiě)的一個(gè)句子,翻譯成為幾種其他的語(yǔ)言。

            客戶程序告訴驅(qū)動(dòng)模塊(不妨稱為Context),它將使用哪個(gè)算法(即所謂的strategypolicy),并請(qǐng)求Context執(zhí)行該算法。

            Strategy模式UML類圖:

            24. C++實(shí)現(xiàn)Behavioral - Strategy模式 - 玄機(jī)逸士 - 玄機(jī)逸士博客
            v

            角色:

            Strategy

            - 給所有支持的算法聲明一個(gè)通用接口,Context使用該接口調(diào)用由ConcreteStrategy定義的算法。

            ConcreteStrategy

            - 按照Strategy給出的接口實(shí)現(xiàn)具體的算法。

            Context

            - 包含一個(gè)Strategy的引用;

            - 可以由ConcreteStrategy對(duì)象對(duì)其進(jìn)行配置。

            業(yè)務(wù)示例:

            將阿拉伯?dāng)?shù)字“520”分別翻譯成中文、英語(yǔ)和俄語(yǔ)。

            下面是具體C++代碼:

            // Strategy.h

            #include <iostream>

            #include <string>

            #include <memory>

            using namespace std;

            // Strategy抽象類,用做借口

            class Strategy

            {

            public:

            virtual string substitute(string str) = 0;

            public:

            virtual ~Strategy()

            {

            cout << "in the destructor of Strategy..." << endl;

            }

            };

            // 中文Strategy

            class ChineseStrategy : public Strategy

            {

            public:

            string substitute(string str)

            {

            size_t index = str.find("520");

            string tempstr = str.replace(index, 3, "我愛(ài)你");

            return tempstr;

            }

            public:

            ~ChineseStrategy()

            {

            cout << "in the destructor of ChineseStrategy..." << endl;

            }

            };

            // 英語(yǔ)Strategy

            class EnglishStrategy : public Strategy

            {

            public:

            string substitute(string str)

            {

            size_t index = str.find("520");

            string tempstr = str.replace(index, 3, "I love you");

            return tempstr;

            }

            public:

            ~EnglishStrategy()

            {

            cout << "in the destructor of EnglishStrategy..." << endl;

            }

            };

            // 俄語(yǔ)Strategy

            class RussianStrategy : public Strategy

            {

            public:

            string substitute(string str)

            {

            size_t index = str.find("520");

            string tempstr = str.replace(index, 3, "Я люблю тебя");

            return tempstr;

            }

            public:

            ~RussianStrategy()

            {

            cout << "in the destructor of RussiaStrategy..." << endl;

            }

            };

            // Context

            class Translator

            {

            private:

            auto_ptr<Strategy> strategy;

            public:

            ~Translator()

            {

            cout << "in the destructor of Translator..." << endl;

            }

            public:

            void set_strategy(auto_ptr<Strategy> strategy)

            {

            this->strategy = strategy;

            }

            string translate(string str)

            {

            if(0 == strategy.get()) return "";

            return strategy->substitute(str);

            }

            };

            // Strategy.cpp

            #include "Strategy.h"

            int main(int argc, char** argv)

            {

            string str("321520");

            Translator* translator = new Translator;

            // 未指定strategy時(shí)

            cout << "No strategy: " << translator->translate(str) << endl;

            cout << "--------------------------" << endl;

            // 翻譯成中文

            auto_ptr<Strategy> s1(new ChineseStrategy());

            translator->set_strategy(s1);

            cout << "Chinese strategy: " << translator->translate(str) << endl;

            cout << "--------------------------" << endl;

            // 翻譯成英語(yǔ)

            auto_ptr<Strategy> s2(new EnglishStrategy());

            translator->set_strategy(s2);

            cout << "English strategy: " << translator->translate(str) << endl;

            cout << "--------------------------" << endl;

            // 翻譯成俄語(yǔ)

            auto_ptr<Strategy> s3(new RussianStrategy());

            translator->set_strategy(s3);

            cout << "Russian strategy: " << translator->translate(str) << endl;

            cout << "--------------------------" << endl;

            delete translator;

            return 0;

            }

            運(yùn)行結(jié)果:

            No strategy:

            --------------------------

            Chinese strategy: 321我愛(ài)你

            --------------------------

            in the destructor of ChineseStrategy...

            in the destructor of Strategy...

            English strategy: 321I love you

            --------------------------

            in the destructor of EnglishStrategy...

            in the destructor of Strategy...

            Russian strategy: 321Я люблю тебя

            --------------------------

            in the destructor of Translator...

            in the destructor of RussiaStrategy...

            in the destructor of Strategy...

            結(jié)果符合預(yù)期。

            1. 從上面程序可以很容易看出,可以在運(yùn)行時(shí)改變translator對(duì)象的行為;

            2. 算法如果改變了,客戶端程序不需要做任何改動(dòng),比如在EnglishStrategy的實(shí)現(xiàn)中將“520翻譯成“Five hundred and twenty”,客戶端代碼無(wú)需任何改變;

            3. 當(dāng)需要增加新的算法時(shí),比如要將“520”翻譯成日語(yǔ)的“わたし愛(ài)してるあなた”,只需要增加一個(gè)具體的Strategy類,比如JapaneseStrategy類,并重寫(xiě)Strategy中聲明的純虛函數(shù)即可。這完全符合OCP

            上述代碼的UML類圖:

            24. C++實(shí)現(xiàn)Behavioral - Strategy模式 - 玄機(jī)逸士 - 玄機(jī)逸士博客

            Strategy模式和State粗看起來(lái)非常相似,但他們的意圖完全不同。他們主要的區(qū)別在于:

            1. Strategy一次只能選擇一個(gè)strategy(即算法),而State模式中,不同的狀態(tài)有可能同時(shí)被激活;

            2. Strategy封裝的是算法,State封裝的是狀態(tài);

            3. Strategy所封裝的算法(每個(gè)算法對(duì)應(yīng)一個(gè)類),所做的事情相差無(wú)幾,而State所封裝的狀態(tài)(每個(gè)狀態(tài)對(duì)應(yīng)一個(gè)類),往往頗不相同;

            4. State模式中的狀態(tài)遷移的概念,在Strategy中根本不存在。

            posted on 2013-03-08 15:39 Jacc.Kim 閱讀(209) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 設(shè)計(jì)模式
            久久久WWW成人| 91久久精品国产成人久久| 无码人妻久久一区二区三区蜜桃| 国产成人精品久久亚洲高清不卡 | 91久久精品国产91性色也| 青青热久久国产久精品 | 久久人人爽人人爽人人片AV东京热| 久久夜色精品国产亚洲av| 久久精品国产亚洲av麻豆蜜芽 | 久久亚洲高清综合| 久久综合亚洲色HEZYO社区| 久久永久免费人妻精品下载| 久久国产精品久久精品国产| 性高朝久久久久久久久久| 99久久久精品| 日本欧美久久久久免费播放网| 久久国产高潮流白浆免费观看| 久久国产乱子伦精品免费午夜| 伊人久久大香线蕉av不卡| 99久久精品费精品国产| 久久婷婷五月综合国产尤物app| 久久嫩草影院免费看夜色| 99久久无码一区人妻| 亚洲国产精品无码久久久不卡| 久久精品成人一区二区三区| 久久久久久毛片免费播放| 亚洲精品久久久www| 久久九九久精品国产| 久久久91精品国产一区二区三区 | 久久青青草原国产精品免费| 性做久久久久久久久久久| 久久久久国产一级毛片高清版| 无码超乳爆乳中文字幕久久| 国产成人精品综合久久久久| 久久天天躁夜夜躁狠狠躁2022| 日产久久强奸免费的看| 久久久久无码精品| 亚洲乱码日产精品a级毛片久久| 一级女性全黄久久生活片免费| 久久国产香蕉视频| 合区精品久久久中文字幕一区|