解决多集成,多子类,扩展等 ---装饰模式

    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里面的装饰功能

 

 

架构设计模式。拜读下有空

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值