1. 程式人生 > >利用職責鏈模式分解If else

利用職責鏈模式分解If else

tails 耦合 amp AI RR 閱讀 避免 tel write

你肯定遇見過這樣類似的代碼:

                if (arg < 10)
                {

                }
                else if (arg >= 10 && arg < 20)
                {

                }
                else if (arg >= 20 && arg < 30)
                {

                }
            //    ......
else { }

過多的if else在代碼的閱讀和拓展時,會變得相當的困難.

所以運用職責鏈模式進行分解

職責鏈模式(Chain of Responsibility):使多個對象都有機會處理請求,從而避免請求的發送者和接受者之間的耦合關系。將這個對象練成一條鏈,並沿著這條鏈傳遞該請求,直到有一個對象處理它為止。

代碼如下:

先建立一個抽象類:

public abstract class AbsCondition<T>
    {
        protected abstract
string Flag { get; } protected AbsCondition<T> condition; protected Predicate<T> conditionFunc; public void AddCondtion(AbsCondition<T> condition) { this.condition = condition; } public void HandleCondition(T obj) {
if (conditionFunc(obj)) { Console.WriteLine($"{Flag},傳入參數{obj}"); } else { condition?.HandleCondition(obj); } } }

再把需要處理的條件封裝成對象:

public class Condition1<T> : AbsCondition<T>
    {
        public Condition1(Predicate<T> conditionFunc)
        {
            this.conditionFunc = conditionFunc;
        }

        protected override string Flag
        {
            get
            {
                return "第一個條件";
            }
        }
    }

public class Condition2<T> : AbsCondition<T>
    {
        public Condition2(Predicate<T> conditionFunc)
        {
            this.conditionFunc = conditionFunc;
        }

        protected override string Flag
        {
            get
            {
                return "第二個條件";
            }
        }
    }

public class Condition3<T> : AbsCondition<T>
    {
        public Condition3(Predicate<T> conditionFunc)
        {
            this.conditionFunc = conditionFunc;
        }

        protected override string Flag
        {
            get
            {
                return "第三個條件";
            }
        }
    }

public class Condition4<T> :AbsCondition<T>
    {
        public Condition4(Predicate<T> conditionFunc)
        {
            this.conditionFunc = conditionFunc;
        }

        protected override string Flag
        {
            get
            {
                return "第四個條件";
            }
        }
    }

調用:

static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("輸入數字:");
                var arg = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("=".PadRight(50,=));
                Condition1<int> c1 = new Condition1<int>(x => x < 10);
                Condition2<int> c2 = new Condition2<int>(x => x >= 10 && x < 20);
                Condition3<int> c3 = new Condition3<int>(x => x >= 20 && x < 30);
                Condition4<int> c4 = new Condition4<int>(x => x >= 30);
                c1.AddCondtion(c2);
                c2.AddCondtion(c3);
                c3.AddCondtion(c4);
                c1.HandleCondition(arg);
            }
            catch
            {
                Console.WriteLine("發生錯誤,請重新輸入");
            }
            finally
            {
                Main(args);
            }
        }

拓展至 http://blog.csdn.net/laner0515/article/details/7383872"

利用職責鏈模式分解If else