第 6 章 —— 装饰模式
6.6 裝扮模式總結(jié)
裝飾模式把每個(gè)要裝飾的功能放在單獨(dú)的類中,并讓這個(gè)類包裝它所要裝飾的對象,因此,當(dāng)需要執(zhí)行特殊行為時(shí),客戶端代碼就可以在運(yùn)行時(shí)根據(jù)需要有選擇地、按順序地使用裝飾功能包裝對象了。
裝扮模式是為已有功能動(dòng)態(tài)地添加更多功能的一種方式。
裝飾模式的優(yōu)點(diǎn):
有效地把類的核心職責(zé)和裝飾功能區(qū)分開了(將裝飾功能從類中移除,簡化了原有的類)。而且可以去除相關(guān)類中重復(fù)的裝飾邏輯(具體的裝飾類都繼承了抽象裝飾類)。
需要注意的問題:
裝飾模式的順序很重要,比如加密數(shù)據(jù)和過濾詞匯都可以是數(shù)據(jù)持久化(存儲(chǔ))以前的裝飾功能,但若先加密了數(shù)據(jù)再用過濾功能就會(huì)出問題了,最理想的情況,是保證裝飾類之間彼此獨(dú)立,這樣它們就可以以任意的順序進(jìn)行組合了。
6.4 裝飾模式
裝飾模式,動(dòng)態(tài)地給一個(gè)對象添加一些額外的職責(zé),就增加功能來說,裝飾模式比生成子類更為靈活。
基本的實(shí)現(xiàn)代碼:
Component 類:
abstract class Component{public abstract void Operation();}Component 定義了一個(gè)對象接口(抽象類的抽象方法),可以給這些對象動(dòng)態(tài)地添加職責(zé)。
ConcreteComponent 類:
class ConcreteComponent : Component{public override void Operation(){Console.WriteLine("具體對象的操作");}}ConcreteComponent定義了一個(gè)具體的對象,也可以給這個(gè)對象添加一些職責(zé)。
Decorator 類:(抽象的裝飾類,需要繼承定義的對象接口)
abstract class Decorator : Component{protected Component component;public void SetComponent(Component component) //設(shè)置Component {this.component = component;}public override void Operation() //重寫Operation(),實(shí)際執(zhí)行的是Component的Operation() {if(component != null){component.Operation();}}}Decorator,抽象裝飾類,繼承了?Component,從類外面來擴(kuò)展?Component 類的功能,但對于?Component 來說,是無需知道?Decorator 的存在的。
具體的裝飾類:
class ConcreteDecoratorA : Decorator{private string addedState; //本類的獨(dú)有功能,以區(qū)別于ConcreteDecoratorBpublic override void Operation(){base.Operation(); //首先運(yùn)行原Component的Operation(),再執(zhí)行本類的功能,如addedState,相當(dāng)于對原Component進(jìn)行了裝飾addedState = "New State";Console.WriteLine("具體裝飾對象A的操作");}}class ConcreteDecoratorB : Decorator{public override void Operation(){base.Operation(); //首先運(yùn)行原Component的Operation(),再執(zhí)行本類的功能,AddBehavitor(),相當(dāng)于對原Component進(jìn)行了裝飾 AddBehavitor();Console.WriteLine("具體裝飾對象A的操作");}private void AddBehavitor() //本類獨(dú)有的方法,以區(qū)別于ConcreteDecoratorA {}}客戶端代碼:
static void Main(string[] args){ConcreteComponent c = new ConcreteComponent();ConcreteDecoratorA d1 = new ConcreteDecoratorA();ConcreteDecoratorB d2 = new ConcreteDecoratorB();d1.SetComponent(c);d2.SetComponent(d1);d2.Operation(); //裝飾的方法是:首先用ConcreteComponent實(shí)例化對象c,然后用ConcreteDecoratorA的實(shí)例化對象d1來包裝c,再用ConcreteDecoratorB的對象d2包裝d1,最終執(zhí)行d2的Operation() Console.Read();}6.5 裝飾模式的實(shí)際應(yīng)用
“Person” 類(ConcreteComponent)
class Person{public Person(){ }private string name;public Person(string name){this.name = name;}public virtual void Show(){Console.WriteLine("裝扮的{0}", name);}}服飾類(Decorator)
class Finery : Person{protected Person component;//打扮public void Decorate(Person component){this.component = component;}public override void Show(){if(component != null){component.Show();}}}具體服飾類(ConcreteDecorator)
class TShirts : Finery{public override void Show(){Console.WriteLine("大T恤 ");base.Show();}}class BigTrouser : Finery{public override void Show(){Console.WriteLine("垮褲 ");base.Show();}}客戶端代碼
static void Main(string[] args){Person xc = new Person("小菜");Console.WriteLine("\n第一種裝扮:");BigTrouser kk = new BigTrouser(); //垮褲TShirts dtx = new TShirts(); //大T恤 kk.Decorate(xc); //裝扮過程 dtx.Decorate(kk);dtx.Show();Console.Read();}?
轉(zhuǎn)載于:https://www.cnblogs.com/zhangchaoran/p/8538062.html
總結(jié)
以上是生活随笔為你收集整理的第 6 章 —— 装饰模式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 可视化爬虫资料
- 下一篇: 大道至简伪代码(第一个博客)