大话设计模式(四)装饰模式

装饰模式结构图:

 

装饰模式基本代码:

using System; using System.Collections.Generic; using System.Text; namespace 装饰模式 { class Program { 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(); Console.Read(); } } abstract class Component { public abstract void Operation(); } class ConcreteComponent : Component { public override void Operation() { Console.WriteLine("具体对象的操作"); } } abstract class Decorator : Component { protected Component component; public void SetComponent(Component component) { this.component = component; } public override void Operation() { if (component != null) { component.Operation(); } } } class ConcreteDecoratorA : Decorator { private string addedState; public override void Operation() { base.Operation(); addedState = "New State"; Console.WriteLine("具体装饰对象A的操作"); } } class ConcreteDecoratorB : Decorator { public override void Operation() { base.Operation(); AddedBehavior(); Console.WriteLine("具体装饰对象B的操作"); } private void AddedBehavior() { } } }

 

代码结构图:

大话设计模式(四)装饰模式_第1张图片

装饰模式代码:

using System; using System.Collections.Generic; using System.Text; namespace 装饰模式 { class Program { static void Main(string[] args) { Person xc = new Person("小菜"); Console.WriteLine("/n第一种装扮:"); Sneakers pqx = new Sneakers(); BigTrouser kk = new BigTrouser(); TShirts dtx = new TShirts(); pqx.Decorate(xc); kk.Decorate(pqx); dtx.Decorate(kk); dtx.Show(); Console.WriteLine("/n第二种装扮:"); LeatherShoes px = new LeatherShoes(); Tie ld = new Tie(); Suit xz = new Suit(); px.Decorate(xc); ld.Decorate(px); xz.Decorate(ld); xz.Show(); Console.WriteLine("/n第三种装扮:"); Sneakers pqx2 = new Sneakers(); LeatherShoes px2 = new LeatherShoes(); BigTrouser kk2 = new BigTrouser(); Tie ld2 = new Tie(); pqx2.Decorate(xc); px2.Decorate(pqx); kk2.Decorate(px2); ld2.Decorate(kk2); ld2.Show(); Console.Read(); } } class Person { public Person() { } private string name; public Person(string name) { this.name = name; } public virtual void Show() { Console.WriteLine("装扮的{0}", name); } } class Finery : Person { protected Person component; //打扮 public void Decorate(Person component) { this.component = component; } public override void Show() { if (component != null) { component.Show(); } } } class TShirts : Finery { public override void Show() { Console.Write("大T恤 "); base.Show(); } } class BigTrouser : Finery { public override void Show() { Console.Write("垮裤 "); base.Show(); } } class Sneakers : Finery { public override void Show() { Console.Write("破球鞋 "); base.Show(); } } class Suit : Finery { public override void Show() { Console.Write("西装 "); base.Show(); } } class Tie : Finery { public override void Show() { Console.Write("领带 "); base.Show(); } } class LeatherShoes : Finery { public override void Show() { Console.Write("皮鞋 "); base.Show(); } } }

大话设计模式(四)装饰模式_第2张图片

你可能感兴趣的:(设计模式,c,String,Decorator,null,Class)