轉載自:
http://patmusing.blog.163.com/blog/static/135834960201002321448133/
假如我們需要為游戲開發坦克,除了各種不同型號的坦克(T50、T75和T90)外,我們還希望在不同場合中為其增加以下一種或多種功能,比如:
- 紅外夜視功能
- 水陸兩棲功能
- 衛星定位功能
等等,通常在不使用設計模式的情況下,大致的實現思路如下:
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個類。同樣道理T75和T90也會各自派生出7個類,那么這樣的派生的子類就會多達21個,以后每增加一個型號的坦克,都會增加類似的7個類。這種設計思路是靜態的(即通過繼承的方式來獲得功能的擴充),即所有可能需要用到的類,必須在代碼編譯之前準備好,因此,上面的21個類我們都必須編寫。
上面所描述的問題的根源在于我們“過度地使用了繼承來擴展對象的功能”,由于繼承為類型引入的靜態特質(即如前所言,必須在編譯前將所有類的代碼必須準備好),使得這種擴展方式缺乏靈活性;并且隨著子類(各種不同型號的坦克)和擴展功能(紅外、兩棲和定位)的增多,各種子類和擴展功能的組合會導致子類的數量以幾何級數的方式增長,以至于代碼難以維護。
Decorator設計模式,就是使“對象功能的擴展”能夠根據需要來動態地實現,同時可以避免“擴展功能的增多”導致子類數量急劇增多,從而使得任何“功能擴展變化”所產生的負面影響降為最低。
下面為Decorator的UML類圖:

具體實現代碼:
// 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"關系
{
protected:
auto_ptr<Tank> tank; // "has - a"關系
public:
Decorator(auto_ptr<Tank> tank) : tank(tank) // 具體的坦克類如T5或具體的裝飾類
{ // 如InfraredDecorator均可作為參數
} // 傳入
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) // 調用Decorator類中的構造方法
{
}
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;
}
運行結果:
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設計模式要點:
- 通過采用組合,而非繼承的手法,實現了在運行時動態地擴展對象功能的能力,而且可以根據需要擴展多個功能。避免了單獨使用繼承帶來的“靈活性差”和“多子類衍生問題”。
- Component類在Decorator設計模式中充當抽象接口的角色,不應該去實現具體的行為。而且Decorator類對于Component類應該透明,即Component類無需知道Decorator類,Decorator類是從外部來擴展Component類的功能。
- Decorator類在接口上表現為“is-a”Component的繼承關系,即Decorator類繼承了Component類所具有的接口;但在實現上又同時表現為“has-a”Component的組合關系,即Decorator類有使用了另外一個Component類。因此,我們可以使用一個或者多個Decorator對象來“裝飾”一個Component對象,并且經過裝飾后的對象仍然是一個Component對象。
- Decorator設計模式并非解決“多子類衍生的多繼承”問題,但它事實上大幅度減少了子類的數量,如上例中21個子類全部可以無需存在(當然增加了一個Decorator抽象類)。不過,其應用的要點在于解決“主題類在多個方向上的擴展功能”,而這正是“裝飾”的含義。
下面的UML類圖,是在原圖的基礎上增加幾個注解,用來說明上面的示例代碼中的類和原圖中的各類之間的對應關系,見帶有顏色部分的注釋。
Decorator模式可以被認為是Composite的簡單版本,他們之間的不同是:
1. Composite中有多個Component對象,而Decorator中有且僅有一個Component對象;
2. Decorator用于增加函數的功能性,而Composite用于傳遞函數調用。