14 Decorator
动态地给一个对象增加一些额外的职责。就增加功能而言,Decorator模式比生成子类更为灵活。
// from: Design Patterns Elements of reusable Object-Oriented Software using System; namespace DesignPatterns_Decorator { public abstract class VisualComponent { public virtual void Draw() { Console.WriteLine("Draw Visual Component."); } } public class TextView : VisualComponent { public override void Draw() { base.Draw(); Console.WriteLine("Draw TextView"); } } public abstract class Decorator : VisualComponent { public Decorator(VisualComponent v) { this.component = v; } private VisualComponent component; public override void Draw() { component.Draw(); } } public class ConcreteDecorator1 : Decorator { public ConcreteDecorator1(VisualComponent v) : base(v) { } public override void Draw() { base.Draw(); Console.WriteLine("ConcreteDecorator1 draw"); } } public class ConcreteDecorator2 : Decorator { public ConcreteDecorator2(VisualComponent v) : base(v) { } public override void Draw() { base.Draw(); Console.WriteLine("ConcreteDecorator2 draw"); } } class Program { static void Main(string[] args) { TextView tv = new TextView(); Decorator de = new ConcreteDecorator1(tv); Decorator de2 = new ConcreteDecorator2(de); Decorator de11 = new ConcreteDecorator1(de2); //Decorator的递归嵌套 de11.Draw(); Console.ReadLine(); } } }
//////////////以下来自视频教程:
public abstract class MyClass
{
public abstract void Fun1();
public abstract void Fun2();
}
/////////////////////////////////
public class Decorator: MyClass
{
MyClass _mc = null;
public Decorator(MyClass mc)
{
this._mc = mc;
}
public override void Fun1()
{
_mc.Fun1();
}
public override void Fun2()
{
_mc.Fun2();
}
}
//////////////////////////////////
public class DecoratorA : Decorator
{
public DecoratorA(MyClass mc) : base(mc) { }
public override void Fun1()
{
//增加一个功能点
Console.WriteLine("I can fly.");
base.Fun1();
}
public override void Fun2()
{
Console.WriteLine("I can fly too.");
base.Fun2();
}
}
//////////////////////////////////
public class DecoratorB: Decorator
{
public DecoratorB(MyClass mc) : base(mc) { }
public override void Fun1()
{
Console.WriteLine("i can run");
base.Fun1();
}
public override void Fun2()
{
Console.WriteLine("i can run too");
base.Fun2();
}
}
//////////////////////////////////
public class DecoratorC: Decorator
{
public DecoratorC(MyClass mc) : base(mc) { }
public override void Fun1()
{
Console.WriteLine("i can swim");
base.Fun1();
}
public override void Fun2()
{
Console.WriteLine("i can swim too");
base.Fun2();
}
}
//////////////////////////////////
public class MyClassConcrete: MyClass
{
public override void Fun1()
{
Console.WriteLine(this.GetType().ToString() +" : Fun1");
}
public override void Fun2()
{
Console.WriteLine(this.GetType().ToString()+" : Fun2");
}
}
//////////////////////////////////
static void Main(string[] args)
{
MyClass mc = new Decorator(new MyClassConcrete());
mc.Fun1();
mc.Fun2();
DecoratorA da = new DecoratorA(mc);
da.Fun1();
da.Fun2();
DecoratorB db = new DecoratorB(da);
db.Fun1();
db.Fun2();
DecoratorC dc = new DecoratorC(db);
dc.Fun1();
dc.Fun2();
}
{
public abstract void Fun1();
public abstract void Fun2();
}
/////////////////////////////////
public class Decorator: MyClass
{
MyClass _mc = null;
public Decorator(MyClass mc)
{
this._mc = mc;
}
public override void Fun1()
{
_mc.Fun1();
}
public override void Fun2()
{
_mc.Fun2();
}
}
//////////////////////////////////
public class DecoratorA : Decorator
{
public DecoratorA(MyClass mc) : base(mc) { }
public override void Fun1()
{
//增加一个功能点
Console.WriteLine("I can fly.");
base.Fun1();
}
public override void Fun2()
{
Console.WriteLine("I can fly too.");
base.Fun2();
}
}
//////////////////////////////////
public class DecoratorB: Decorator
{
public DecoratorB(MyClass mc) : base(mc) { }
public override void Fun1()
{
Console.WriteLine("i can run");
base.Fun1();
}
public override void Fun2()
{
Console.WriteLine("i can run too");
base.Fun2();
}
}
//////////////////////////////////
public class DecoratorC: Decorator
{
public DecoratorC(MyClass mc) : base(mc) { }
public override void Fun1()
{
Console.WriteLine("i can swim");
base.Fun1();
}
public override void Fun2()
{
Console.WriteLine("i can swim too");
base.Fun2();
}
}
//////////////////////////////////
public class MyClassConcrete: MyClass
{
public override void Fun1()
{
Console.WriteLine(this.GetType().ToString() +" : Fun1");
}
public override void Fun2()
{
Console.WriteLine(this.GetType().ToString()+" : Fun2");
}
}
//////////////////////////////////
static void Main(string[] args)
{
MyClass mc = new Decorator(new MyClassConcrete());
mc.Fun1();
mc.Fun2();
DecoratorA da = new DecoratorA(mc);
da.Fun1();
da.Fun2();
DecoratorB db = new DecoratorB(da);
db.Fun1();
db.Fun2();
DecoratorC dc = new DecoratorC(db);
dc.Fun1();
dc.Fun2();
}