Franky's LIFE

          Anything... ...

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            53 隨筆 :: 87 文章 :: 6 評(píng)論 :: 0 Trackbacks

          Decorator 裝 飾模式是一種結(jié)構(gòu)型模式,它主要是解決:“過度地使用了繼承來擴(kuò)展對(duì)象的功能”,由于繼承為類型引入的靜態(tài)特質(zhì),使得這種擴(kuò)展方式缺乏靈活性;并且隨著子 類的增多(擴(kuò)展功能的增多),各種子類的組合(擴(kuò)展功能的組合)會(huì)導(dǎo)致更多子類的膨脹(多繼承)。繼承為類型引入的靜態(tài)特質(zhì)的意思是說以繼承的方式使某一 類型要獲得功能是在編譯時(shí)。所謂靜態(tài),是指在編譯時(shí);動(dòng)態(tài),是指在運(yùn)行時(shí)。

          GoF 《設(shè)計(jì)模式》中說道:動(dòng)態(tài)的給一個(gè)對(duì)象添加一些額外的職責(zé)。就增加功能而言, Decorator 模式比生成子類更為靈活。

          下面來看看 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ī)的接口或者是抽象類,我這里就用抽象類來實(shí)現(xiàn),代碼如下:

          public abstract class AbstractCellPhone

          ??? {

          ??????? public abstract string CallNumber();

          ??????? public abstract string SendMessage();

          }

          AbstractCellPhone 也就是結(jié)構(gòu)圖中的Component,然后,我再來實(shí)現(xiàn)NokiaMoto的手機(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的對(duì)象。這樣做的意義是: Decorator 類又使用了另外一個(gè) Component 類。我們可以使用一個(gè)或多個(gè) Decorator 對(duì)象來“裝飾”一個(gè) Component 對(duì)象,且裝飾后的對(duì)象仍然是一個(gè) Component 對(duì)象。 在下來,我要實(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 Main(string[] args)

          ??????? {

          ???????????? AbstractCellPhone phone = newNokiaPhone();

          ??????????? Console.WriteLine(phone.CallNumber());

          ??????????? Console.WriteLine(phone.SendMessage());

          ??????????? DecoratorGPS gps = newDecoratorGPS(phone);???? //add GSP

          ??????????? Console.WriteLine(gps.CallNumber());

          ??????????? Console.WriteLine(gps.SendMessage());

          ??????????? DecoratorBlueTooth bluetooth = newDecoratorBlueTooth(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í)動(dòng)態(tài)的擴(kuò)展對(duì)象功能的能力,而且可以根據(jù)需要擴(kuò)展多個(gè)功能。避免了單獨(dú)使用繼承帶來的“靈活性差”和“多子類衍生問題”。

          2 Component 類在 Decorator 模式中充當(dāng)抽象接口的角色,不應(yīng)該去實(shí)現(xiàn)具體的行為。而且 Decorator 類對(duì)于 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 對(duì)象來“裝飾”一個(gè) Component 對(duì)象,且裝飾后的對(duì)象仍然是一個(gè) Component 對(duì)象。(在這里我想談一下我的理解:當(dāng)我們實(shí)例化一個(gè) Component 對(duì)象后,要給這個(gè)對(duì)象擴(kuò)展功能,這時(shí)我們把這個(gè) Component 對(duì)象當(dāng)作參數(shù)傳給 Decorator 的子類的構(gòu)造函數(shù)——也就是擴(kuò)展方法的功能類。對(duì)于引用類型傳參時(shí),實(shí)際上只是傳遞對(duì)象的地址,這樣,在功能擴(kuò)展是,操作的應(yīng)該是同一個(gè)對(duì)象)

          4 Decorator 模式并非解決“多子類衍生的多繼承”問題, Decorator 模式應(yīng)用的要點(diǎn)在于解決“主體類在多個(gè)方向上的擴(kuò)展功能”——是為“裝飾”的含義。 Decorator 是在運(yùn)行時(shí)對(duì)功能進(jìn)行組合。
          posted on 2007-03-19 20:56 Franky 閱讀(125) 評(píng)論(0)  編輯  收藏 所屬分類: IT知識(shí)
          主站蜘蛛池模板: 长丰县| 广河县| 翁源县| 图木舒克市| 即墨市| 石城县| 哈巴河县| 赣州市| 竹山县| 新化县| 乌鲁木齐县| SHOW| 靖远县| 甘德县| 台安县| 吴江市| 临颍县| 博爱县| 磐石市| 新巴尔虎左旗| 哈尔滨市| 霸州市| 天门市| 浠水县| 屏东县| 安岳县| 顺平县| 湖南省| 定襄县| 德兴市| 阿克| 新安县| 苍溪县| 桃园市| 翁牛特旗| 上饶县| 上饶市| 桃江县| 鹤壁市| 禄丰县| 蕉岭县|