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

            我參與的團(tuán)隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

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


            假如我們需要為游戲開發(fā)坦克,除了各種不同型號的坦克
            (T50T75T90)外,我們還希望在不同場合中為其增加以下一種或多種功能,比如:

            - 紅外夜視功能

            - 水陸兩棲功能

            - 衛(wèi)星定位功能

            等等,通常在不使用設(shè)計模式的情況下,大致的實現(xiàn)思路如下:

            1. 先定義一個抽象坦克類

            class Tank

            {

            public:

            virtual void shot() = 0;

            virtual void run() = 0;

            public:

            virtual ~Tank()

            {

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

            }

            };

            2. 各種型號的坦克均繼承抽象坦克類

            class T50 : public Tank

            {

            // ...

            };

            class T75 : public Tank

            {

            // ...

            };

            class T90 : public Tank

            {

            // ...

            };

            3. 定義各種新增功能的接口

            class IInfrared // 紅外功能抽象類,用作接口

            {

            // ...

            };

            class IAmphibian // 兩棲功能抽象類,用作接口

            {

            // ...

            };

            class IGPS // 定位功能抽象類,用作接口

            {

            // ...

            };

            4. 定義各種具有新增功能的型號

            T50為例,定義如下:

            class T50Infrared : public T50, public IInfrared // 有紅外功能的T50

            {

            // ...

            };

            class T50Amphibian : public T50, public IAmphibian // 有兩棲功能的T50

            {

            // ...

            };

            class T50GPS : public T50, public IGPS // 有定位功能的T50

            {

            // ...

            };

            class T50InfraredAmphibian : public T50, public IInfrared, public IAmphibian // 有紅外、兩棲功能的T50

            {

            // ...

            };

            class T50InfraredGPS : public T50, public IInfrared, public IGPS // 有紅外、定位功能的T50

            {

            // ...

            };

            class T50AmphibianGPS : public T50, public Amphibian, public IGPS // 有兩棲、定位功能的T50

            {

            // ...

            };

            class T50InfraredAmphibianGPS : public T50, public IInfrared, public IAmphibian, public IGPS // 有紅外、兩棲、定位功能的T50

            {

            // ...

            };

            一共有7個類。同樣道理T75T90也會各自派生出7個類,那么這樣的派生的子類就會多達(dá)21個,以后每增加一個型號的坦克,都會增加類似的7個類。這種設(shè)計思路是靜態(tài)的(即通過繼承的方式來獲得功能的擴(kuò)充),即所有可能需要用到的類,必須在代碼編譯之前準(zhǔn)備好,因此,上面的21個類我們都必須編寫。

            上面所描述的問題的根源在于我們過度地使用了繼承來擴(kuò)展對象的功能,由于繼承為類型引入的靜態(tài)特質(zhì)(即如前所言,必須在編譯前將所有類的代碼必須準(zhǔn)備好),使得這種擴(kuò)展方式缺乏靈活性;并且隨著子類(各種不同型號的坦克)和擴(kuò)展功能(紅外、兩棲和定位)的增多,各種子類和擴(kuò)展功能的組合會導(dǎo)致子類的數(shù)量以幾何級數(shù)的方式增長,以至于代碼難以維護(hù)。

            Decorator設(shè)計模式,就是使對象功能的擴(kuò)展能夠根據(jù)需要來動態(tài)地實現(xiàn),同時可以避免擴(kuò)展功能的增多導(dǎo)致子類數(shù)量急劇增多,從而使得任何功能擴(kuò)展變化所產(chǎn)生的負(fù)面影響降為最低。

            下面為DecoratorUML類圖:

            11. C++實現(xiàn)Structural - Decorator模式 - 玄機(jī)逸士 - 玄機(jī)逸士博客

            具體實現(xiàn)代碼:

            // Decorator.h

            #include <string>

            #include <iostream>

            #include <memory>

            using namespace std;

            // 抽象類:Tank

            class Tank

            {

            public:

            virtual void shot() = 0;

            virtual void run() = 0;

            public:

            virtual ~Tank()

            {

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

            }

            };

            // 具體坦克類:T50

            class T50 : public Tank

            {

            public:

            void shot()

            {

            cout << "Tank Type T50 : shot() : " << endl;

            }

            void run()

            {

            cout << "Tank Type T50 : run() : " << endl;

            }

            public:

            virtual ~T50()

            {

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

            }

            };

            // 具體坦克類:T75

            class T75 : public Tank

            {

            public:

            void shot()

            {

            cout << "Tank Type T75 : shot() : " << endl;

            }

            void run()

            {

            cout << "Tank Type T75 : run() : " << endl;

            }

            public:

            virtual ~T75()

            {

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

            }

            };

            // 具體坦克類:T90

            class T90 : public Tank

            {

            public:

            void shot()

            {

            cout << "Tank Type T90 : shot() : " << endl;

            }

            void run()

            {

            cout << "Tank Type T90 : run() : " << endl;

            }

            public:

            virtual ~T90()

            {

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

            }

            };

            //抽象類:Decorator

            class Decorator : public Tank // "is - a"關(guān)系

            {

            protected:

            auto_ptr<Tank> tank; // "has - a"關(guān)系

            public:

            Decorator(auto_ptr<Tank> tank) : tank(tank) // 具體的坦克類如T5或具體的裝飾類

            { // InfraredDecorator均可作為參數(shù)

            } // 傳入

            virtual ~Decorator()

            {

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

            }

            public:

            void shot()

            {

            tank->shot();

            }

            void run()

            {

            tank->run();

            }

            };

            class InfraredDecorator : public Decorator

            {

            private:

            string infrared; // 這就是所謂的addedAtrribute

            public:

            InfraredDecorator(auto_ptr<Tank> tank) : Decorator(tank) // 調(diào)用Decorator類中的構(gòu)造方法

            {

            }

            virtual ~InfraredDecorator()

            {

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

            }

            public:

            void set_Infrared(const string& infrared) // 這就所謂的addedOperation

            {

            this->infrared = infrared;

            }

            string get_Infrared() const

            {

            return infrared;

            }

            public:

            void run()

            {

            tank->run();

            // 下面的語句模擬增加紅外功能。如果不想給run增加紅外功能,則下面語句可以省去

            set_Infrared("+ Infrared ");

            cout << get_Infrared() << endl;

            }

            void shot()

            {

            tank->shot();

            // 下面的語句模擬增加紅外功能

            set_Infrared("+ Infrared ");

            cout << get_Infrared() << endl;

            }

            };

            class AmphibianDecorator : public Decorator

            {

            private:

            string amphibian; // 這就是所謂的addedAtrribute

            public:

            AmphibianDecorator(auto_ptr<Tank> tank) : Decorator(tank)

            {

            }

            ~AmphibianDecorator()

            {

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

            }

            public:

            void set_Amphibian(const string& amphibian) // 這就所謂的addedOperation

            {

            this->amphibian = amphibian;

            }

            string get_Amphibian() const

            {

            return amphibian;

            }

            public:

            void run()

            {

            tank->run();

            // 下面的語句模擬增加兩棲功能

            set_Amphibian("+ Amphibian ");

            cout << get_Amphibian() << endl;

            }

            void shot()

            {

            tank->shot();

            // 下面的語句模擬增加兩棲功能

            set_Amphibian("+ Amphibian ");

            cout << get_Amphibian() << endl;

            }

            };

            class GPSDecorator : public Decorator

            {

            private:

            string gps; // 這就是所謂的addedAtrribute

            public:

            GPSDecorator(auto_ptr<Tank> tank) : Decorator(tank)

            {

            }

            ~GPSDecorator()

            {

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

            }

            public:

            void set_GPS(const string& gps) // 這就所謂的addedOperation

            {

            this->gps = gps;

            }

            string get_GPS() const

            {

            return gps;

            }

            public:

            void run()

            {

            tank->run();

            // 下面的語句模擬增加定位功能

            set_GPS("+ GPS ");

            cout << get_GPS() << endl;

            }

            void shot()

            {

            tank->shot();

            // 下面的語句模擬增加定位功能

            set_GPS("+ GPS ");

            cout << get_GPS() << endl;

            }

            };

            // Decorator.cpp

            #include "Decorator.h"

            int main(int argc, char **argv)

            {

            // T50增加紅外功能

            auto_ptr<Tank> tank1(new T50);

            auto_ptr<Tank> pid1(new InfraredDecorator(tank1));

            pid1->shot();

            cout << endl;

            pid1->run();

            cout << "\n-------------------------------\n" << endl;

            // T75增加紅外、兩棲功能

            auto_ptr<Tank> tank2(new T75);

            auto_ptr<Tank> pid2(new InfraredDecorator(tank2));

            auto_ptr<Tank> pad2(new AmphibianDecorator(pid2));

            pad2->shot();

            cout << endl;

            pad2->run();

            cout << "\n-------------------------------\n" << endl;

            // T75增加紅外、兩棲、定位功能

            auto_ptr<Tank> tank3(new T90);

            auto_ptr<Tank> pid3(new InfraredDecorator(tank3));

            auto_ptr<Tank> pad3(new AmphibianDecorator(pid3));

            auto_ptr<Tank> pgd3(new GPSDecorator(pad3));

            pgd3->shot();

            cout << endl;

            pgd3->run();

            cout << "\n-------------------------------\n" << endl;

            }

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

            Tank Type T50 : shot() :

            + Infrared

            Tank Type T50 : run() :

            + Infrared

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

            Tank Type T75 : shot() :

            + Infrared

            + Amphibian

            Tank Type T75 : run() :

            + Infrared

            + Amphibian

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

            Tank Type T90 : shot() :

            + Infrared

            + Amphibian

            + GPS

            Tank Type T90 : run() :

            + Infrared

            + Amphibian

            + GPS

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

            in the destructor of GPSDecorator...

            in the destructor of Decorator...

            in the destructor of AmphibianDecorator...

            in the destructor of Decorator...

            in the destructor of InfraredDecorator...

            in the destructor of Decorator...

            in the destructor of T90...

            in the destructor of Tank...

            in the destructor of Tank...

            in the destructor of Tank...

            in the destructor of Tank...

            in the destructor of AmphibianDecorator...

            in the destructor of Decorator...

            in the destructor of InfraredDecorator...

            in the destructor of Decorator...

            in the destructor of T75...

            in the destructor of Tank...

            in the destructor of Tank...

            in the destructor of Tank...

            in the destructor of InfraredDecorator...

            in the destructor of Decorator...

            in the destructor of T50...

            in the destructor of Tank...

            in the destructor of Tank...

            Decorator設(shè)計模式要點:

            - 通過采用組合,而非繼承的手法,實現(xiàn)了在運(yùn)行時動態(tài)地擴(kuò)展對象功能的能力,而且可以根據(jù)需要擴(kuò)展多個功能。避免了單獨使用繼承帶來的“靈活性差”和“多子類衍生問題”。

            - Component類在Decorator設(shè)計模式中充當(dāng)抽象接口的角色,不應(yīng)該去實現(xiàn)具體的行為。而且Decorator類對于Component類應(yīng)該透明,即Component類無需知道Decorator類,Decorator類是從外部來擴(kuò)展Component類的功能。

            - Decorator類在接口上表現(xiàn)為“is-aComponent的繼承關(guān)系,即Decorator類繼承了Component類所具有的接口;但在實現(xiàn)上又同時表現(xiàn)為“has-aComponent的組合關(guān)系,即Decorator類有使用了另外一個Component類。因此,我們可以使用一個或者多個Decorator對象來“裝飾”一個Component對象,并且經(jīng)過裝飾后的對象仍然是一個Component對象。

            - Decorator設(shè)計模式并非解決“多子類衍生的多繼承”問題,但它事實上大幅度減少了子類的數(shù)量,如上例中21個子類全部可以無需存在(當(dāng)然增加了一個Decorator抽象類)。不過,其應(yīng)用的要點在于解決“主題類在多個方向上的擴(kuò)展功能”,而這正是“裝飾”的含義。

            下面的UML類圖,是在原圖的基礎(chǔ)上增加幾個注解,用來說明上面的示例代碼中的類和原圖中的各類之間的對應(yīng)關(guān)系,見帶有顏色部分的注釋。

            11. C++實現(xiàn)Structural - Decorator模式 - 玄機(jī)逸士 - 玄機(jī)逸士博客

            Decorator模式可以被認(rèn)為是Composite的簡單版本,他們之間的不同是:

            1. Composite中有多個Component對象,而Decorator中有且僅有一個Component對象;

            2. Decorator用于增加函數(shù)的功能性,而Composite用于傳遞函數(shù)調(diào)用。




            posted on 2013-03-07 22:15 Jacc.Kim 閱讀(258) 評論(0)  編輯 收藏 引用 所屬分類: 設(shè)計模式
            国产亚洲美女精品久久久2020| 久久精品无码一区二区无码| 久久精品免费一区二区三区| 久久综合九色综合欧美狠狠| 久久九色综合九色99伊人| 亚洲午夜无码AV毛片久久| 欧洲国产伦久久久久久久| 久久精品无码免费不卡| 超级碰碰碰碰97久久久久| 人妻无码精品久久亚瑟影视| 久久精品一本到99热免费| 国产精品日韩欧美久久综合| 久久高清一级毛片| 性欧美大战久久久久久久久| 久久亚洲精品视频| 亚洲国产精品18久久久久久| 成人国内精品久久久久影院VR | 久久精品国产免费| 中文字幕无码久久精品青草| 久久久久亚洲AV无码麻豆| 日韩AV毛片精品久久久| 精品国产青草久久久久福利| 久久精品中文闷骚内射| 久久久久国产精品麻豆AR影院 | 久久狠狠高潮亚洲精品 | 亚洲中文字幕久久精品无码APP | 亚洲精品美女久久777777| 久久久久99精品成人片| 久久se精品一区精品二区| 久久精品国产久精国产思思| 午夜精品久久久久久久| 亚洲精品无码久久久久sm| 人妻久久久一区二区三区| 久久国产乱子精品免费女| 亚洲成色www久久网站夜月 | 97久久精品人人澡人人爽| 国产精品免费看久久久| 91精品国产色综合久久| 久久99国产精品久久久| 91久久九九无码成人网站| 国产视频久久|