轉(zhuǎn)載自:
http://patmusing.blog.163.com/blog/static/135834960201002321448133/
假如我們需要為游戲開(kāi)發(fā)坦克,除了各種不同型號(hào)的坦克(T50、T75和T90)外,我們還希望在不同場(chǎng)合中為其增加以下一種或多種功能,比如:
- 紅外夜視功能
- 水陸兩棲功能
- 衛(wèi)星定位功能
等等,通常在不使用設(shè)計(jì)模式的情況下,大致的實(shí)現(xiàn)思路如下:
1. 先定義一個(gè)抽象坦克類(lèi)
class Tank
{
public:
virtual void shot() = 0;
virtual void run() = 0;
public:
virtual ~Tank()
{
cout << "in the destructor of Tank..." << endl;
}
};
2. 各種型號(hào)的坦克均繼承抽象坦克類(lèi)
class T50 : public Tank
{
// ...
};
class T75 : public Tank
{
// ...
};
class T90 : public Tank
{
// ...
};
3. 定義各種新增功能的接口
class IInfrared // 紅外功能抽象類(lèi),用作接口
{
// ...
};
class IAmphibian // 兩棲功能抽象類(lèi),用作接口
{
// ...
};
class IGPS // 定位功能抽象類(lèi),用作接口
{
// ...
};
4. 定義各種具有新增功能的型號(hào)
以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個(gè)類(lèi)。同樣道理T75和T90也會(huì)各自派生出7個(gè)類(lèi),那么這樣的派生的子類(lèi)就會(huì)多達(dá)21個(gè),以后每增加一個(gè)型號(hào)的坦克,都會(huì)增加類(lèi)似的7個(gè)類(lèi)。這種設(shè)計(jì)思路是靜態(tài)的(即通過(guò)繼承的方式來(lái)獲得功能的擴(kuò)充),即所有可能需要用到的類(lèi),必須在代碼編譯之前準(zhǔn)備好,因此,上面的21個(gè)類(lèi)我們都必須編寫(xiě)。
上面所描述的問(wèn)題的根源在于我們“過(guò)度地使用了繼承來(lái)擴(kuò)展對(duì)象的功能”,由于繼承為類(lèi)型引入的靜態(tài)特質(zhì)(即如前所言,必須在編譯前將所有類(lèi)的代碼必須準(zhǔn)備好),使得這種擴(kuò)展方式缺乏靈活性;并且隨著子類(lèi)(各種不同型號(hào)的坦克)和擴(kuò)展功能(紅外、兩棲和定位)的增多,各種子類(lèi)和擴(kuò)展功能的組合會(huì)導(dǎo)致子類(lèi)的數(shù)量以幾何級(jí)數(shù)的方式增長(zhǎng),以至于代碼難以維護(hù)。
Decorator設(shè)計(jì)模式,就是使“對(duì)象功能的擴(kuò)展”能夠根據(jù)需要來(lái)動(dòng)態(tài)地實(shí)現(xiàn),同時(shí)可以避免“擴(kuò)展功能的增多”導(dǎo)致子類(lèi)數(shù)量急劇增多,從而使得任何“功能擴(kuò)展變化”所產(chǎn)生的負(fù)面影響降為最低。
下面為Decorator的UML類(lèi)圖:

具體實(shí)現(xiàn)代碼:
// Decorator.h
#include <string>
#include <iostream>
#include <memory>
using namespace std;
// 抽象類(lèi):Tank
class Tank
{
public:
virtual void shot() = 0;
virtual void run() = 0;
public:
virtual ~Tank()
{
cout << "in the destructor of Tank..." << endl;
}
};
// 具體坦克類(lèi):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;
}
};
// 具體坦克類(lèi):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;
}
};
// 具體坦克類(lèi):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;
}
};
//抽象類(lèi):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) // 具體的坦克類(lèi)如T5或具體的裝飾類(lèi)
{ // 如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類(lèi)中的構(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();
// 下面的語(yǔ)句模擬增加紅外功能。如果不想給run增加紅外功能,則下面語(yǔ)句可以省去
set_Infrared("+ Infrared ");
cout << get_Infrared() << endl;
}
void shot()
{
tank->shot();
// 下面的語(yǔ)句模擬增加紅外功能
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();
// 下面的語(yǔ)句模擬增加兩棲功能
set_Amphibian("+ Amphibian ");
cout << get_Amphibian() << endl;
}
void shot()
{
tank->shot();
// 下面的語(yǔ)句模擬增加兩棲功能
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();
// 下面的語(yǔ)句模擬增加定位功能
set_GPS("+ GPS ");
cout << get_GPS() << endl;
}
void shot()
{
tank->shot();
// 下面的語(yǔ)句模擬增加定位功能
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è)計(jì)模式要點(diǎn):
- 通過(guò)采用組合,而非繼承的手法,實(shí)現(xiàn)了在運(yùn)行時(shí)動(dòng)態(tài)地?cái)U(kuò)展對(duì)象功能的能力,而且可以根據(jù)需要擴(kuò)展多個(gè)功能。避免了單獨(dú)使用繼承帶來(lái)的“靈活性差”和“多子類(lèi)衍生問(wèn)題”。
- Component類(lèi)在Decorator設(shè)計(jì)模式中充當(dāng)抽象接口的角色,不應(yīng)該去實(shí)現(xiàn)具體的行為。而且Decorator類(lèi)對(duì)于Component類(lèi)應(yīng)該透明,即Component類(lèi)無(wú)需知道Decorator類(lèi),Decorator類(lèi)是從外部來(lái)擴(kuò)展Component類(lèi)的功能。
- Decorator類(lèi)在接口上表現(xiàn)為“is-a”Component的繼承關(guān)系,即Decorator類(lèi)繼承了Component類(lèi)所具有的接口;但在實(shí)現(xiàn)上又同時(shí)表現(xiàn)為“has-a”Component的組合關(guān)系,即Decorator類(lèi)有使用了另外一個(gè)Component類(lèi)。因此,我們可以使用一個(gè)或者多個(gè)Decorator對(duì)象來(lái)“裝飾”一個(gè)Component對(duì)象,并且經(jīng)過(guò)裝飾后的對(duì)象仍然是一個(gè)Component對(duì)象。
- Decorator設(shè)計(jì)模式并非解決“多子類(lèi)衍生的多繼承”問(wèn)題,但它事實(shí)上大幅度減少了子類(lèi)的數(shù)量,如上例中21個(gè)子類(lèi)全部可以無(wú)需存在(當(dāng)然增加了一個(gè)Decorator抽象類(lèi))。不過(guò),其應(yīng)用的要點(diǎn)在于解決“主題類(lèi)在多個(gè)方向上的擴(kuò)展功能”,而這正是“裝飾”的含義。
下面的UML類(lèi)圖,是在原圖的基礎(chǔ)上增加幾個(gè)注解,用來(lái)說(shuō)明上面的示例代碼中的類(lèi)和原圖中的各類(lèi)之間的對(duì)應(yīng)關(guān)系,見(jiàn)帶有顏色部分的注釋。
Decorator模式可以被認(rèn)為是Composite的簡(jiǎn)單版本,他們之間的不同是:
1. Composite中有多個(gè)Component對(duì)象,而Decorator中有且僅有一個(gè)Component對(duì)象;
2. Decorator用于增加函數(shù)的功能性,而Composite用于傳遞函數(shù)調(diào)用。