文章出處
文章列表
索引
- 包裝器(Wrapper)
動態地給一個對象添加一些額外的職責。
就增加功能來說,Decorator 模式相比生成子類更為靈活。
Attach additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to subclassing for extending functionality.
Component
- 定義一個對象接口,可以給這些對象動態地添加職責。
ConcreteComponent
- 定義一個對象,可以給這個對象添加一些職責。
Decorator
- 維持一個指向 Component 對象的指針,并定義一個與 Component 接口一致的接口。
ConcreteDecorator
- 向組件添加職責。
在以下情況下可以使用 Decorator 模式:
- 在不影響其他對象的情況下,以動態、透明的方式給單個對象添加職責。
- 處理那些可以撤銷的職責。
- 當不能采用生成子類的方法進行擴充時。
- Decorator 是一個透明的包裝,其與 Component 還是有些差別的。
- 采用 Decorator 模式進行系統設計往往會產生許多看上去類似的小對象。導致很難學習系統,排錯也很困難。
- 比靜態繼承更靈活。
- 避免在層次結構高層的類有太多的特征。
- Decorator 模式不同于 Adapter 模式,因為 Decorator 僅改變對象的職責而不改變它的接口,而 Adapter 將給對象一個全新的接口。
- 可以將 Decorator 視為一個退化的、僅有一個組件的 Composite。然而,Decorator 僅給對象添加額外的職責,它的目的不在于對象聚集。
- 用一個 Decorator 可以改變對象的外表,而 Strategy 模式使得你可以改變對象的內核。這是改變對象的兩種途徑。
- 當 Component 類原本就很龐大時,使用 Decorator 模式的代價太高,Strategy 模式相對更好一些。
實現方式(一):Decorator 對象的接口必須與它所裝飾的 Component 的接口保持一致。
所有的 ConcreteDecorator 類必須有一個公共的父類。
使用 Decorator 模式僅從外部改變組件,因此組件無需對它的裝飾有任何了解,也就是說,這些裝飾對該組件是透明的。
1 namespace DecoratorPattern.Implementation1 2 { 3 public abstract class Component 4 { 5 public abstract void Operation(); 6 } 7 8 public class ConcreteComponent : Component 9 { 10 public override void Operation() 11 { 12 // do something 13 } 14 } 15 16 public abstract class Decorator : Component 17 { 18 private Component _component; 19 20 public Decorator(Component component) 21 { 22 _component = component; 23 } 24 25 public override void Operation() 26 { 27 _component.Operation(); 28 } 29 } 30 31 public class ConcreteDecorator : Decorator 32 { 33 public ConcreteDecorator(Component component) 34 : base(component) 35 { 36 } 37 38 public override void Operation() 39 { 40 base.Operation(); 41 AddedBehavior(); 42 } 43 44 private void AddedBehavior() 45 { 46 // do some other things 47 } 48 } 49 50 public class Client 51 { 52 public void TestCase1() 53 { 54 Component component1 = new ConcreteComponent(); 55 Component component2 = new ConcreteDecorator(component1); 56 57 component2.Operation(); 58 } 59 } 60 }
當僅需要添加一個職責是,沒有必要定義抽象的 Decorator 類。
這時可以把 Decorator 向 Component 轉發請求的職責合并到 ConcreteDecorator 中。
1 namespace DecoratorPattern.Implementation2 2 { 3 public abstract class Component 4 { 5 public abstract void Operation(); 6 } 7 8 public class ConcreteComponent : Component 9 { 10 public override void Operation() 11 { 12 // do something 13 } 14 } 15 16 public class ConcreteDecorator : Component 17 { 18 private Component _component; 19 20 public ConcreteDecorator(Component component) 21 { 22 _component = component; 23 } 24 25 public override void Operation() 26 { 27 _component.Operation(); 28 AddedBehavior(); 29 } 30 31 private void AddedBehavior() 32 { 33 // do some other things 34 } 35 } 36 37 public class Client 38 { 39 public void TestCase1() 40 { 41 Component component1 = new ConcreteComponent(); 42 Component component2 = new ConcreteDecorator(component1); 43 44 component2.Operation(); 45 } 46 } 47 }
《設計模式之美》為 Dennis Gao 發布于博客園的系列文章,任何未經作者本人同意的人為或爬蟲轉載均為耍流氓。
文章列表
全站熱搜