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