• <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++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              140 Posts :: 1 Stories :: 11 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

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


            假設我們需要開發(fā)一個坦克模擬系統(tǒng)用于模擬坦克在各種作戰(zhàn)環(huán)境中的行為,其中坦克系統(tǒng)由引擎、車輪、控制器和火炮等各子系統(tǒng)構(gòu)成。

            12. C++實現(xiàn)Structural - Faccedil;ade模式 - 玄機逸士 - 玄機逸士博客

            A方案的問題在于組件的客戶和組件中各種復雜的子系統(tǒng)有了過多的耦合,隨著外部客戶程序和個子系統(tǒng)的演化,這種過多的耦合面臨很多變化的挑戰(zhàn)。Façade設計模式則簡化外部客戶程序和系統(tǒng)間的交互接口,將外部客戶程序的演化和內(nèi)部子系統(tǒng)的變化之間的依賴相互解耦。

            Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. (為子系統(tǒng)中的一組接口提供一個一致的界面,Façade設計模式定義了一個高層接口,這個接口使得這一子系統(tǒng)更加容易使用) - GoF

            示例代碼:

            // Facade.h

            #include <iostream>

            #include <vector>

            using namespace std;

            class Engine // 發(fā)動機子系統(tǒng)

            {

            private:

            bool engineStatus;

            public:

            Engine()

            {

            engineStatus = false;

            }

            public:

            void turn_on() // 啟動

            {

            engineStatus = true;

            cout << "this is in method turn_on() ..." << endl;

            }

            void turn_off() // 關(guān)閉

            {

            engineStatus = false;

            cout << "this is in method turn_off() ..." << endl;

            }

            bool get_engine_status() const // 獲取發(fā)動機狀態(tài)

            {

            return engineStatus;

            }

            };

            class WheelPedrail // 履帶子系統(tǒng)

            {

            public:

            void rotate() // 轉(zhuǎn)動

            {

            cout << "this in method rotate() ..." << endl;

            }

            };

            class Controller // 控制子系統(tǒng)

            {

            public:

            void forward() // 前進

            {

            cout << "this is in method forward() ..." << endl;

            }

            void backward() // 后退

            {

            cout << "this is in method backward() ..." << endl;

            }

            void turnright() // 右轉(zhuǎn)

            {

            cout << "this is in method turnright() ..." << endl;

            }

            void turnleft() // 左轉(zhuǎn)

            {

            cout << "this is in method turnleft() ..." << endl;

            }

            };

            class Artillery // 火炮子系統(tǒng)

            {

            public:

            void fire() // 開炮

            {

            cout << "this is in method fire() ..." << endl;

            }

            void shell_load() // 裝填炮彈

            {

            cout << "this is in method shell_load() ..." << endl;

            }

            void aim_at() // 瞄準

            {

            cout << "this is in method aim_at() ..." << endl;

            }

            };

            class TankFacade

            {

            private:

            vector<Engine> engine;

            vector<WheelPedrail> wheel_pedrail;

            Controller controller;

            Artillery artillery;

            public:

            TankFacade()

            {

            vector<Engine> eng(4); // 4個發(fā)動機

            engine = eng;

            for(int i = 0; i < 12; i++) // 12個輪子

            {

            WheelPedrail wp;

            wheel_pedrail.push_back(wp);

            }

            }

            void start()

            {

            if(!engine[0].get_engine_status()) engine[0].turn_on();

            if(!engine[1].get_engine_status()) engine[1].turn_on();

            if(!engine[2].get_engine_status()) engine[2].turn_on();

            if(!engine[3].get_engine_status()) engine[3].turn_on();

            }

            void stop()

            {

            if(engine[0].get_engine_status()) engine[0].turn_off();

            if(engine[1].get_engine_status()) engine[1].turn_off();

            if(engine[2].get_engine_status()) engine[2].turn_off();

            if(engine[3].get_engine_status()) engine[3].turn_off();

            }

            void run()

            {

            start();

            for(int i = 0; i < 12; i++) // 12個輪子

            {

            wheel_pedrail[i].rotate();

            }

            controller.forward();

            }

            void fire()

            {

            start();

            artillery.aim_at();

            artillery.shell_load();

            artillery.fire();

            }

            // ...

            };

            // 測試代碼:Facade.cpp

            #include "Facade.h"

            int main(int argc, char **argv)

            {

            TankFacade *tank = new TankFacade;

            tank->run();

            tank->fire();

            return 0;

            }

            程序運行結(jié)果:

            this is in method turn_on() ...

            this is in method turn_on() ...

            this is in method turn_on() ...

            this is in method turn_on() ...

            this in method rotate() ...

            this in method rotate() ...

            this in method rotate() ...

            this in method rotate() ...

            this in method rotate() ...

            this in method rotate() ...

            this in method rotate() ...

            this in method rotate() ...

            this in method rotate() ...

            this in method rotate() ...

            this in method rotate() ...

            this in method rotate() ...

            this is in method forward() ...

            this is in method aim_at() ...

            this is in method shell_load() ...

            this is in method fire() ...

            Façade設計模式的幾個要點:

            - 從客戶程序的角度來看,Façade設計模式不僅簡化了整個組件系統(tǒng)的接口,同時對于組件內(nèi)部與外部客戶程序來說,從某種程度上也達到了一個“解耦”的效果 --- 內(nèi)部子系統(tǒng)的任何變化不會影響到Façade接口的變化。

            - Façade設計模式更注重從架構(gòu)的層次去看整個系統(tǒng),而不是從類的層次。Façade設計模式更多的時候是一種系統(tǒng)架構(gòu)設計模式。

            Façade設計模式、Adapter設計模式、Bridge設計模式與Decorator設計模式之間的區(qū)別:

            - Façade設計模式注重簡化接口;

            - Adapter設計模式注重轉(zhuǎn)換接口;

            - Bridge設計模式注重分離接口(抽象)與其實現(xiàn);

            - Decorator設計模式注重在穩(wěn)定接口的前提下為對象擴展功能。



            posted on 2013-03-07 23:20 Jacc.Kim 閱讀(260) 評論(0)  編輯 收藏 引用 所屬分類: 設計模式
            A级毛片无码久久精品免费| 色天使久久综合网天天| 久久精品一区二区三区AV| 国产成人综合久久精品尤物| 久久精品国产亚洲AV麻豆网站| 欧美日韩精品久久久久| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 久久福利青草精品资源站免费| 久久99精品国产麻豆宅宅| 一本色道久久88精品综合| 性欧美大战久久久久久久久| 亚洲国产精品18久久久久久| 久久久无码人妻精品无码| 国产精品99久久久久久人| 九九99精品久久久久久| 999久久久国产精品| 青青热久久国产久精品| 97精品依人久久久大香线蕉97| 婷婷久久久亚洲欧洲日产国码AV| 日韩久久久久久中文人妻| 99久久精品毛片免费播放| 精品一久久香蕉国产线看播放| 日产久久强奸免费的看| 97精品依人久久久大香线蕉97 | 久久99久国产麻精品66| 国产V亚洲V天堂无码久久久| 国产精品女同一区二区久久| 无码人妻少妇久久中文字幕| 天天爽天天狠久久久综合麻豆| 精品一区二区久久| 久久久久久久综合狠狠综合| 91精品国产91久久综合| 香港aa三级久久三级老师2021国产三级精品三级在 | 精品国际久久久久999波多野| 91精品国产91久久| 亚洲欧美成人综合久久久| 99久久精品免费看国产一区二区三区| 久久久国产亚洲精品| 青青青伊人色综合久久| 亚洲中文久久精品无码ww16| 内射无码专区久久亚洲|