标签:style blog http color io 使用 ar 文件 sp
装饰模式:装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。
(一)装饰模式的特点:
1 using System; 2 3 namespace 装饰模式 4 { 5 abstract class Component 6 { 7 public abstract void Operation(); 8 } 9 class ConcreteComponent:Component 10 { 11 public override void Operation() 12 { 13 Console.WriteLine("需要扩展功能的对象的原有操作。"); 14 } 15 } 16 abstract class Decorator:Component 17 { 18 public Component component; 19 public void SetComponent(Component component) 20 { 21 this.component = component; 22 } 23 public override void Operation() 24 { 25 if (component != null) 26 { 27 component.Operation(); 28 } 29 } 30 } 31 class ConcreteDecoratorA : Decorator 32 { 33 public override void Operation() 34 { 35 base.Operation(); 36 Console.WriteLine("功能A..."); 37 } 38 } 39 class ConcreteDecoratorB : Decorator 40 { 41 public override void Operation() 42 { 43 base.Operation(); 44 Console.WriteLine("功能B..."); 45 } 46 } 47 class Program 48 { 49 static void Main(string[] args) 50 { 51 ConcreteComponent c = new ConcreteComponent(); 52 ConcreteDecoratorA ca = new ConcreteDecoratorA(); 53 ConcreteDecoratorB cb = new ConcreteDecoratorB(); 54 //装饰过程 55 ca.SetComponent(c); 56 cb.SetComponent(ca); 57 58 cb.Operation(); 59 60 Console.ReadKey(); 61 } 62 } 63 }
标签:style blog http color io 使用 ar 文件 sp
原文地址:http://www.cnblogs.com/runonroad/p/4022164.html