设计模式 -(5)装饰模式(结构型)
生活随笔
收集整理的這篇文章主要介紹了
设计模式 -(5)装饰模式(结构型)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
(Decorator)
裝飾模式以對客戶端透明的方式擴展對象的功能,是繼承關系的一個替代方案。
裝飾模式以對客戶透明的方式動態地給一個對象附加上更多的責任,裝飾者模式相比生成子類可以更靈活地增加功能。
裝飾模式可以在不增加子類的情況下,將對象的功能加以擴展。
裝飾模式是把客戶端的調用委派到被裝飾類。裝飾模式的關鍵在于擴展完全透明的。 裝飾模式是在不必改變原類文件和使用繼承的情況下,動態的擴展一個對象的功能。它是通過創建一個包裝對象,也就是來包裹真實對象。
裝飾模式的特點:
裝飾對象和真實對象有相同的接口,這樣客戶端對象就可以以和真實對象相同的方式和裝飾對象交互。
裝飾對象包含一個真實對象的引用。
裝飾對象接收所有來自客戶端的請求。它把這些請求轉發給真實的對象。
裝飾對象可以轉發這些請求以前或以后增加一些附加功能。這樣就確保了在運行時,不用修改給定對象的接口就可以在外部增加附加的功能。在面向對象的設計中,通常是通過繼承來實現對給定類的功能拓展。
時序圖
IComponent.cs
interface IComponent { void DoSomething(); }
ConcreteComponent.cs
class ConcreteComponent : IComponent { public void DoSomething() { Console.WriteLine("功能A"); } }
Decretor.cs
class Decretor : IComponent { private IComponent _component;
public Decretor(IComponent Component) { this._component = Component; }
public virtual void DoSomething() { this._component.DoSomething(); } }
ConcreteDecorator1.cs
class ConcreteDecorator1 : Decretor { public ConcreteDecorator1(IComponent component):base(component){}
public override void DoSomething() { base.DoSomething(); this.DoAnotherThing(); } private void DoAnotherThing() { Console.WriteLine("功能B"); } }
ConcreteDecorator2.cs
class ConcreteDecorator2 : Decretor { public ConcreteDecorator2(IComponent component) : base(component) { }
public override void DoSomething() { base.DoSomething(); this.DoAnotherThing(); }
private void DoAnotherThing() { Console.WriteLine("功能C"); } }
Client.cs
class Client { static void Main(string[] args) { Decretor decretor = new ConcreteDecorator1(new ConcreteDecorator2(new ConcreteComponent())); decretor.DoSomething(); Console.Read(); } }
例子2:
來自為知筆記(Wiz)
裝飾模式的角色:
抽象構件角色(Component):給出一個抽象接口,以規范準備接收附加責任的對象。具體構件角色(Concrete Component):定義一個將要接收附加責任的類。裝飾角色(Decorator):持有一個構件(Component)對象的引用,并定義一個與抽象構件接口一致的接口。具體裝飾角色(Concrete Decorator):負責給構件兌現“貼上”附加的責任。裝飾模式的特點:
時序圖
IComponent.cs
interface IComponent { void DoSomething(); }
ConcreteComponent.cs
class ConcreteComponent : IComponent { public void DoSomething() { Console.WriteLine("功能A"); } }
Decretor.cs
class Decretor : IComponent { private IComponent _component;
public Decretor(IComponent Component) { this._component = Component; }
public virtual void DoSomething() { this._component.DoSomething(); } }
ConcreteDecorator1.cs
class ConcreteDecorator1 : Decretor { public ConcreteDecorator1(IComponent component):base(component){}
public override void DoSomething() { base.DoSomething(); this.DoAnotherThing(); } private void DoAnotherThing() { Console.WriteLine("功能B"); } }
ConcreteDecorator2.cs
class ConcreteDecorator2 : Decretor { public ConcreteDecorator2(IComponent component) : base(component) { }
public override void DoSomething() { base.DoSomething(); this.DoAnotherThing(); }
private void DoAnotherThing() { Console.WriteLine("功能C"); } }
Client.cs
class Client { static void Main(string[] args) { Decretor decretor = new ConcreteDecorator1(new ConcreteDecorator2(new ConcreteComponent())); decretor.DoSomething(); Console.Read(); } }
例子2:
來自為知筆記(Wiz)
轉載于:https://www.cnblogs.com/tangge/p/5900322.html
總結
以上是生活随笔為你收集整理的设计模式 -(5)装饰模式(结构型)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用户和组相关配置文件
- 下一篇: [ASP.NET Core] Stati