解决多集成,多子类,扩展等 ---装饰模式
public abstract class Tank { public abstract void Shot(); public abstract void Run(); }
解决主题类再多种方向上的扩展功能
public abstract class Decorator:Tank { private Tank tank; public Decorator(Tank tank) { this.tank = tank; } public override void Shot() { Console.WriteLine( "Tank.Shot" ); tank.Shot(); } public override void Run() { Console.WriteLine( "Tank.Run" ); tank.Run(); } }
public class DecoratorA:Decorator { public DecoratorA( Tank tank ) : base( tank ) { } public override void Shot() { //Do some extension 红外线 Console.WriteLine( "DecoratorA.Shot" ); base.Shot(); } public override void Run() { //Do Some extension Console.WriteLine( "DecoratorA.Run" ); base.Run(); } } public class DecoratorB : Decorator { public DecoratorB( Tank tank ) : base( tank ) { } public override void Shot() { //Do some extension 夜视功能 Console.WriteLine( "DecoratorB.Shot" ); base.Shot(); } public override void Run() { //Do Some extension 夜视功能 Console.WriteLine( "DecoratorB.Run" ); base.Run(); } }
他并不是is a关系,只是把当成接口,主题是has a关系
看调用
class Program { static void Main( string[] args ) { Tank tank = new T50(); DecoratorA da = new DecoratorA( tank ); DecoratorB db = new DecoratorB( da ); db.Shot(); } }
public class T50:Tank { public override void Shot() { Console.WriteLine( "T50.Shot" ); } public override void Run() { Console.WriteLine( "T50.Run" ); } }
在.net里面的装饰功能
架构设计模式。拜读下有空
posted on 2013-04-02 21:01 HOT SUMMER 阅读(244) 评论(0) 编辑 收藏 举报