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

            常用鏈接

            留言簿(1)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            轉載自: http://patmusing.blog.163.com/blog/static/135834960201002310460918/


            假設我們需要開發一個坦克模擬系統用于模擬坦克在各種作戰環境中的行為,其中坦克系統由引擎、車輪、控制器和火炮等各子系統構成。

            12. C++實現Structural - Faccedil;ade模式 - 玄機逸士 - 玄機逸士博客

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

            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. (為子系統中的一組接口提供一個一致的界面,Façade設計模式定義了一個高層接口,這個接口使得這一子系統更加容易使用) - GoF

            示例代碼:

            // Facade.h

            #include <iostream>

            #include <vector>

            using namespace std;

            class Engine // 發動機子系統

            {

            private:

            bool engineStatus;

            public:

            Engine()

            {

            engineStatus = false;

            }

            public:

            void turn_on() // 啟動

            {

            engineStatus = true;

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

            }

            void turn_off() // 關閉

            {

            engineStatus = false;

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

            }

            bool get_engine_status() const // 獲取發動機狀態

            {

            return engineStatus;

            }

            };

            class WheelPedrail // 履帶子系統

            {

            public:

            void rotate() // 轉動

            {

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

            }

            };

            class Controller // 控制子系統

            {

            public:

            void forward() // 前進

            {

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

            }

            void backward() // 后退

            {

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

            }

            void turnright() // 右轉

            {

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

            }

            void turnleft() // 左轉

            {

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

            }

            };

            class Artillery // 火炮子系統

            {

            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個發動機

            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;

            }

            程序運行結果:

            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設計模式不僅簡化了整個組件系統的接口,同時對于組件內部與外部客戶程序來說,從某種程度上也達到了一個“解耦”的效果 --- 內部子系統的任何變化不會影響到Façade接口的變化。

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

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

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

            - Adapter設計模式注重轉換接口;

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

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



            posted on 2013-03-07 23:20 Jacc.Kim 閱讀(263) 評論(0)  編輯 收藏 引用 所屬分類: 設計模式
            欧美午夜A∨大片久久 | 99久久综合国产精品二区| 国产精品对白刺激久久久| 久久亚洲国产欧洲精品一| 久久亚洲2019中文字幕| 无码人妻精品一区二区三区久久久| 人妻精品久久无码专区精东影业| 亚洲国产天堂久久综合网站| 久久这里都是精品| 国产午夜久久影院| 天天影视色香欲综合久久| 99久久免费国产精品热| 久久这里只有精品视频99| 2021久久精品国产99国产精品| 久久这里只有精品视频99| 91精品国产高清久久久久久io| 久久天天躁狠狠躁夜夜2020 | 亚洲国产精品综合久久一线| 国产美女亚洲精品久久久综合 | 欧美麻豆久久久久久中文| 欧美激情精品久久久久| 久久久久久久精品妇女99| 午夜视频久久久久一区| 国产精品亚洲综合专区片高清久久久| 亚洲色婷婷综合久久| 97视频久久久| 青青草原综合久久大伊人| 久久久久人妻一区精品果冻| 中文字幕久久欲求不满| 久久精品国产影库免费看| 久久精品国产亚洲av影院| 无码专区久久综合久中文字幕| 中文国产成人精品久久亚洲精品AⅤ无码精品| 久久国产高清字幕中文| 777久久精品一区二区三区无码 | 久久国产一片免费观看| 91亚洲国产成人久久精品| 伊人久久大香线蕉精品| 91精品久久久久久无码| 99久久国产热无码精品免费久久久久| 久久国产免费观看精品|