public abstract class Decorator : Tank { private Tank _tank; public Decorator(Tank tank) { _tank = tank; } public override void Shot() { _tank.Shot(); } public override void Run() { _tank.Run(); } }
public class ConcreteDecoratorA : Decorator { public ConcreteDecoratorA(Tank tank):base(tank){} public override void Shot() { //扩展红外功能 base.Shot(); } public override void Run() { base.Run(); } }
public class ConcreteDecoratorB : Decorator { public ConcreteDecoratorB(Tank tank):base(tank){} public override void Shot() { //扩展两栖功能 base.Shot(); } public override void Run() { base.Run(); } }
public class ConcreteDecoratorC : Decorator { public ConcreteDecoratorC(Tank tank) : base(tank) { } public override void Shot() { //扩展定位功能 base.Shot(); } public override void Run() { base.Run(); } }
客户端的调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
public class App { static void Main() { Tank tank = new T50(); //拥有红外一种功能 ConcreteDecoratorA da = new ConcreteDecoratorA(tank); //拥有红外和两栖两种功能 ConcreteDecoratorB db = new ConcreteDecoratorB(da); //拥有红外、两栖和定位三种功能 ConcreteDecoratorC dc = new ConcreteDecoratorC(db);