设计模式之——责任链模式

跳转到==>设计模式汇总

责任链模式(属于行为型模式)
1 很好理解,就是一个东西传下来,遇到合适的处理对象,才进行处理
2 重点:有多个处理者,这些处理者继承同一类;每个处理者有它的下一级处理者;当然每个的下一级是串联在一起的

    public class Mobile
    {
        public Mobile nextMobile;
        public Level mobileLevel;
        public Mobile()
        {
            nextMobile = new Honour();
            mobileLevel = Level.None;
        }

        public virtual void Call() { }

        public void CallSomeOne(Level level)
        {
            if (mobileLevel >= level)
            {
                this.Call();
            }
            else
            {
                nextMobile.Call();
            }
        }
    }

    public enum Level
    {
        None = 0,
        Low = 1,
        Middle = 2,
        High = 3
    }
    public class Honour : Mobile
    {
        public Honour()
        {
            nextMobile = new Note();
            mobileLevel = Level.Low;
        }

        public override void Call()
        {
            Console.WriteLine("call someone with honour ");
        }
    }
    public class Meta : Mobile
    {
        public Meta()
        {
            mobileLevel = Level.High;
            nextMobile = new Meta();
        }

        public override void Call()
        {
            Console.WriteLine("Call someone with Meta");
        }
    }
    public class Note : Mobile
    {
        public Note()
        {
            nextMobile = new Meta();
            mobileLevel = Level.Middle;
        }

        public override void Call()
        {
            Console.WriteLine("Call someone with Note");
        }
    }
    public class MobileUser
    {
        public static void CallSomeone()
        {
            Mobile phone = new Mobile();
            phone.CallSomeOne(Level.Middle);
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值