設計模式之——責任鏈模式

跳轉到==>設計模式彙總

責任鏈模式(屬於行爲型模式)
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);
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章