• <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/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)的(即通過繼承的方式來獲得功能的擴充),即所有可能需要用到的類,必須在代碼編譯之前準(zhǔn)備好,因此,上面的21個類我們都必須編寫。

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

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

            下面為DecoratorUML類圖:

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

            具體實現(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;

            }

            運行結(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)了在運行時動態(tài)地擴展對象功能的能力,而且可以根據(jù)需要擴展多個功能。避免了單獨使用繼承帶來的“靈活性差”和“多子類衍生問題”。

            - Component類在Decorator設(shè)計模式中充當(dāng)抽象接口的角色,不應(yīng)該去實現(xiàn)具體的行為。而且Decorator類對于Component類應(yīng)該透明,即Component類無需知道Decorator類,Decorator類是從外部來擴展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)用的要點在于解決“主題類在多個方向上的擴展功能”,而這正是“裝飾”的含義。

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

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

            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è)計模式
            亚洲欧洲久久久精品| 91精品久久久久久无码| 精品久久久久久国产三级| 成人精品一区二区久久| 久久黄色视频| 色天使久久综合网天天| 精品久久久久久中文字幕大豆网| 久久久久久伊人高潮影院| 久久精品国产亚洲麻豆| 久久久久av无码免费网| 亚洲中文久久精品无码ww16 | 亚洲国产成人精品久久久国产成人一区二区三区综 | 久久91精品国产91久久小草| 国产精品午夜久久| 久久se这里只有精品| 久久久久国产一区二区三区| 久久久久无码精品| 久久久老熟女一区二区三区| 午夜不卡久久精品无码免费| 久久大香萑太香蕉av| 无码人妻精品一区二区三区久久久 | 999久久久国产精品| 久久精品国产亚洲AV香蕉| 久久亚洲中文字幕精品一区四| 久久久精品人妻一区二区三区蜜桃 | 久久久久久亚洲精品无码| av无码久久久久久不卡网站| 久久精品国产亚洲网站| 久久国产精品成人影院| 99国产欧美精品久久久蜜芽| 99久久精品免费看国产一区二区三区 | 婷婷久久综合九色综合九七| 97精品依人久久久大香线蕉97| 性高湖久久久久久久久AAAAA| 久久精品国产色蜜蜜麻豆| 久久亚洲精精品中文字幕| 国内精品久久久久久麻豆| 成人国内精品久久久久一区 | 久久国产精品成人影院| 亚洲国产精品嫩草影院久久| 精品亚洲综合久久中文字幕|