Decorator裝 飾模式是一種結(jié)構(gòu)型模式,它主要是解決:“過(guò)度地使用了繼承來(lái)擴(kuò)展對(duì)象的功能”,由于繼承為類(lèi)型引入的靜態(tài)特質(zhì),使得這種擴(kuò)展方式缺乏靈活性;并且隨著子 類(lèi)的增多(擴(kuò)展功能的增多),各種子類(lèi)的組合(擴(kuò)展功能的組合)會(huì)導(dǎo)致更多子類(lèi)的膨脹(多繼承)。繼承為類(lèi)型引入的靜態(tài)特質(zhì)的意思是說(shuō)以繼承的方式使某一 類(lèi)型要獲得功能是在編譯時(shí)。所謂靜態(tài),是指在編譯時(shí);動(dòng)態(tài),是指在運(yùn)行時(shí)。
GoF《設(shè)計(jì)模式》中說(shuō)道:動(dòng)態(tài)的給一個(gè)對(duì)象添加一些額外的職責(zé)。就增加功能而言,Decorator模式比生成子類(lèi)更為靈活。
下面來(lái)看看Decorator模式的結(jié)構(gòu):
看這個(gè)結(jié)構(gòu)好像不是很明白,下面我根據(jù)代碼講解一下這個(gè)結(jié)構(gòu)。我想了一個(gè)場(chǎng)景:我們現(xiàn)在用的手機(jī)功能很多,我就用Decorator模式實(shí)現(xiàn)一下對(duì)某個(gè)手機(jī)的GSP和藍(lán)牙功能擴(kuò)展。
首先,我們需要一個(gè)手機(jī)的接口或者是抽象類(lèi),我這里就用抽象類(lèi)來(lái)實(shí)現(xiàn),代碼如下:
public abstract class AbstractCellPhone
{
public abstract string CallNumber();
public abstract string SendMessage();
}
AbstractCellPhone也就是結(jié)構(gòu)圖中的Component,然后,我再來(lái)實(shí)現(xiàn)Nokia和Moto的手機(jī)類(lèi),這類(lèi)要繼承AbstractCellPhone,也就是圖中ConcreteComponent類(lèi)要繼承Component,實(shí)現(xiàn)代碼如下:
public class NokiaPhone : AbstractCellPhone
{
public override string CallNumber()
{
return "NokiaPhone call sombody";
}
public override string SendMessage()
{
return "NokiaPhone send a message to somebody";
}
}
public class MotoPhone : AbstractCellPhone
{
public override string CallNumber()
{
return "MotoPhone call sombody";
}
public override string SendMessage()
{
return "MotoPhone send a message to somebody";
}
}
接下來(lái)我需要一個(gè)Decorator接口或者抽象類(lèi),實(shí)現(xiàn)代碼如下:
public abstract class Decorator:AbstractCellPhone
{
AbstractCellPhone _phone;
public Decorator(AbstractCellPhone phone)
{
_phone = phone;
}
public override string CallNumber()
{
return _phone.CallNumber();
}
public override string SendMessage()
{
return _phone.SendMessage();
}
}
正如結(jié)構(gòu)圖中,這個(gè)Decorator即繼承了AbstractCellPhone,又包含了一個(gè)私有的AbstractCellPhone的對(duì)象。這樣做的意義是:Decorator類(lèi)又使用了另外一個(gè)Component類(lèi)。我們可以使用一個(gè)或多個(gè)Decorator對(duì)象來(lái)“裝飾”一個(gè)Component對(duì)象,且裝飾后的對(duì)象仍然是一個(gè)Component對(duì)象。在下來(lái),我要實(shí)現(xiàn)GSP和藍(lán)牙的功能擴(kuò)展,它們要繼承自Decorator,代碼如下:
public class DecoratorGPS : Decorator
{
public DecoratorGPS(AbstractCellPhone phone)
: base(phone)
{ }
public override string CallNumber()
{
return base.CallNumber() + " with GPS";
}
public override string SendMessage()
{
return base.SendMessage() + " with GPS";
}
}
public class DecoratorBlueTooth : Decorator
{
public DecoratorBlueTooth(AbstractCellPhone phone)
: base(phone)
{ }
public override string CallNumber()
{
return base.CallNumber() + " with BlueTooth";
}
public override string SendMessage()
{
return base.SendMessage() + " with BlueTooth";
}
}
最后,用客戶端程序驗(yàn)證一下:
static void
{
AbstractCellPhone phone = new NokiaPhone();
Console.WriteLine(phone.CallNumber());
Console.WriteLine(phone.SendMessage());
DecoratorGPS gps = new DecoratorGPS(phone); //add GSP
Console.WriteLine(gps.CallNumber());
Console.WriteLine(gps.SendMessage());
DecoratorBlueTooth bluetooth = new DecoratorBlueTooth(gps); //add GSP and bluetooth
Console.WriteLine(bluetooth.CallNumber());
Console.WriteLine(bluetooth.SendMessage());
Console.Read();
}
執(zhí)行結(jié)果:
NokiaPhone call sombody
NokiaPhone send a message to somebody
NokiaPhone call sombody with GPS
NokiaPhone send a message to somebody with GPS
NokiaPhone call sombody with GPS with BlueTooth
NokiaPhone send a message to somebody with GPS with BlueTooth
從執(zhí)行的結(jié)果不難看出擴(kuò)展功能已被添加。最后再說(shuō)說(shuō)Decorator裝飾模式的幾點(diǎn)要點(diǎn):
1、通過(guò)采用組合、而非繼承的手法,Decorator模式實(shí)現(xiàn)了在運(yùn)行時(shí)動(dòng)態(tài)的擴(kuò)展對(duì)象功能的能力,而且可以根據(jù)需要擴(kuò)展多個(gè)功能。避免了單獨(dú)使用繼承帶來(lái)的“靈活性差”和“多子類(lèi)衍生問(wèn)題”。
2、Component類(lèi)在Decorator模式中充當(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)的功能。
3、Decorator類(lèi)在接口上表現(xiàn)為is-a Component的繼承關(guān)系,即Decorator類(lèi)繼承了Component類(lèi)所具有的接口。但在實(shí)現(xiàn)上又表現(xiàn)為has-a Component的組合關(guān)系,即Decorator類(lèi)又使用了另外一個(gè)Component類(lèi)。我們可以使用一個(gè)或多個(gè)Decorator對(duì)象來(lái)“裝飾”一個(gè)Component對(duì)象,且裝飾后的對(duì)象仍然是一個(gè)Component對(duì)象。(在這里我想談一下我的理解:當(dāng)我們實(shí)例化一個(gè)Component對(duì)象后,要給這個(gè)對(duì)象擴(kuò)展功能,這時(shí)我們把這個(gè)Component對(duì)象當(dāng)作參數(shù)傳給Decorator的子類(lèi)的構(gòu)造函數(shù)——也就是擴(kuò)展方法的功能類(lèi)。對(duì)于引用類(lèi)型傳參時(shí),實(shí)際上只是傳遞對(duì)象的地址,這樣,在功能擴(kuò)展是,操作的應(yīng)該是同一個(gè)對(duì)象)
4、Decorator模式并非解決“多子類(lèi)衍生的多繼承”問(wèn)題,Decorator模式應(yīng)用的要點(diǎn)在于解決“主體類(lèi)在多個(gè)方向上的擴(kuò)展功能”——是為“裝飾”的含義。Decorator是在運(yùn)行時(shí)對(duì)功能進(jìn)行組合。



