青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

積木

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個類,那么這樣的派生的子類就會多達21個,以后每增加一個型號的坦克,都會增加類似的7個類。這種設(shè)計思路是靜態(tài)的(即通過繼承的方式來獲得功能的擴充),即所有可能需要用到的類,必須在代碼編譯之前準備好,因此,上面的21個類我們都必須編寫。

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

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

下面為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è)計模式中充當抽象接口的角色,不應(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個子類全部可以無需存在(當然增加了一個Decorator抽象類)。不過,其應(yīng)用的要點在于解決“主題類在多個方向上的擴展功能”,而這正是“裝飾”的含義。

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

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

Decorator模式可以被認為是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è)計模式
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            久久精品亚洲精品| 亚洲日本成人网| 亚洲免费在线电影| 欧美精品激情| 国内成人精品视频| 国产亚洲精品一区二区| 亚洲欧美日韩电影| 午夜在线电影亚洲一区| 极品少妇一区二区| 欧美国产日韩一区二区在线观看| 美女精品在线| 亚洲午夜一二三区视频| 午夜精品视频在线观看| 影音先锋中文字幕一区二区| 亚洲黄色成人网| 国产精品视频成人| 欧美刺激午夜性久久久久久久| 欧美大片一区| 久久福利影视| 欧美激情bt| 久久久久国产精品一区二区| 免费一级欧美片在线观看| 亚洲国产精品久久久久| 欧美日韩精品一二三区| 久久精品成人一区二区三区| 蜜桃久久精品乱码一区二区| 亚洲一区二区视频| 久久野战av| 亚洲免费在线视频一区 二区| 欧美在线免费视屏| 在线一区免费观看| 久久久久久久综合| 欧美一区成人| 欧美日韩亚洲91| 欧美va天堂va视频va在线| 国产精品电影观看| 亚洲欧洲一区二区三区在线观看| 国产欧美在线看| 一本色道久久综合亚洲91| 一区久久精品| 欧美一区2区三区4区公司二百 | 午夜精品久久久久久久男人的天堂 | 亚洲一区二区在线| 亚洲欧美福利一区二区| 久久久久久国产精品mv| 亚洲欧美激情四射在线日 | 久久精品国产免费看久久精品| 99精品视频免费观看| 久久久久国产精品一区二区| 欧美一区二区三区精品电影| 欧美日韩免费区域视频在线观看| 欧美高清免费| 亚洲国产一区二区三区青草影视| 欧美在线日韩| 久久精品一区蜜桃臀影院| 国产精品久久久久久久app| 亚洲美女网站| 99精品国产在热久久| 亚洲精品视频在线观看网站| 狼狼综合久久久久综合网| 老司机67194精品线观看| 国产日韩欧美自拍| 欧美中文在线字幕| 久久久亚洲国产天美传媒修理工| 国产欧美一区二区在线观看| 亚洲自拍16p| 久久久久久久网站| 伊人成综合网伊人222| 久久三级福利| 欧美激情亚洲国产| 亚洲美女视频在线观看| 欧美日韩久久精品| 99re热精品| 欧美一级黄色录像| 狠狠干成人综合网| 久久婷婷蜜乳一本欲蜜臀| 欧美黄色一区二区| 亚洲视频在线播放| 影音先锋亚洲电影| 亚洲免费成人av电影| 99国产精品自拍| 欧美另类高清视频在线| 亚洲免费电影在线| 欧美在线综合视频| 狠狠88综合久久久久综合网| 久久综合狠狠| 一本久道久久综合婷婷鲸鱼| 午夜一区不卡| 精品不卡在线| 欧美日韩第一区| 亚洲一区二区三区精品在线观看| 久久久久久久激情视频| 亚洲国产日韩欧美在线图片| 欧美日韩一本到| 欧美在线看片| 亚洲精品一区二区在线| 久久精品91| 99re66热这里只有精品4| 国产欧美精品一区| 欧美1区视频| 午夜精品在线| 亚洲国产精品免费| 欧美一区二区三区视频在线| 亚洲激情视频| 国产精品私拍pans大尺度在线| 免费成人在线视频网站| 日韩天堂av| 午夜欧美不卡精品aaaaa| 久久av资源网站| 在线免费一区三区| 欧美视频一区二区在线观看 | 亚洲免费网站| 亚洲国产欧美国产综合一区| 欧美一区二视频| av成人黄色| 亚洲成人自拍视频| 国产欧美一区二区精品婷婷| 欧美精品日韩一区| 久久久午夜精品| 欧美亚洲视频一区二区| 一区二区三区成人精品| 亚洲大片一区二区三区| 久久免费视频网| 性高湖久久久久久久久| 亚洲一区二区三区成人在线视频精品 | 中日韩高清电影网| 一本一本久久a久久精品综合妖精 一本一本久久a久久精品综合麻豆 | 久久国产精品久久w女人spa| 亚洲精品影院在线观看| 在线成人h网| 国语精品一区| 国内精品久久久久影院色 | 亚洲电影毛片| 韩日在线一区| 韩日欧美一区二区| 好男人免费精品视频| 红桃视频一区| 激情成人综合| 鲁大师影院一区二区三区| 欧美一区二区精品| 国产精品视频yy9099| 欧美色大人视频| 欧美日韩在线不卡一区| 欧美日韩国产专区| 欧美日本韩国一区二区三区| 欧美激情亚洲激情| 欧美日韩一卡| 国产精品日韩一区二区| 国产精品美女www爽爽爽视频| 欧美天堂亚洲电影院在线观看| 欧美日韩三级| 国产精品乱人伦中文| 国产农村妇女毛片精品久久麻豆 | 免费不卡中文字幕视频| 欧美成人视屏| 亚洲欧洲视频| 亚洲视频观看| 亚洲欧美国产高清| 国产精品theporn| 欧美另类女人| 国产精品每日更新在线播放网址| 国产精品自拍小视频| 黄色亚洲精品| 亚洲精品国偷自产在线99热| 亚洲视频播放| 久久久九九九九| 欧美韩日一区二区三区| 99re66热这里只有精品4| 欧美一级片在线播放| 欧美成人网在线| 国产精品乱码一区二三区小蝌蚪| 国产一区二区三区免费不卡| 亚洲人成在线观看网站高清| 亚洲欧美国产77777| 欧美成人资源网| 亚洲午夜小视频| 卡一卡二国产精品| 国产精品美女主播在线观看纯欲| 好吊日精品视频| 亚洲视频一区二区在线观看| 久久综合狠狠| 亚洲视频国产视频| 欧美成人亚洲成人| 国产亚洲毛片在线| 国产精品日本精品| 午夜亚洲性色福利视频| 亚洲欧洲一区二区三区| 亚洲欧美精品suv| 麻豆精品一区二区av白丝在线| 亚洲精品在线视频观看| 久久精品国产精品亚洲精品| 欧美视频在线观看一区二区| 亚洲国产精品va在线观看黑人| 亚洲欧美日韩精品久久亚洲区| 亚洲国产欧美国产综合一区| 久久经典综合| 国产麻豆综合| 亚洲欧美日韩综合一区| 亚洲每日更新| 欧美激情欧美激情在线五月|