国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

重構:用Command替換條件調度程序

2019-11-14 16:07:55
字體:
來源:轉載
供稿:網友

 

注:該隨筆受啟發于 《重構與模式》  第七章 第7.6小節 用Command替換條件調度程序 。

 

對于Command不做過多解釋,這里我找了兩個例子、供部分園友參閱:Command例子1 Command例子2 。

條件調度程序:我對這個名詞的理解為,它是相對簡單的選擇結構 與 相對獨立的業務邏輯的結合體。

話不是很好理解,下面舉個小例子吧。

 

重構前的代碼:

  /// <summary>        /// 很簡單的選擇分支 一層 if else         /// N個 相對獨立 任務                     /// </summary>        /// <param name="actionName"></param>        public void DoAction(string actionName)        {            if (actionName == "Action1")            {                // 處理 Action1任務                Console.WriteLine("執行任務1");            }            else if (actionName == "Action2")            {                // 處理 Action2任務                Console.WriteLine("執行任務2");            }            else if (actionName == "Action3")            {                // 處理 Action3任務                // 無處理操作            }        }

 

在 《重構與模式》 一文中的重構的做法是:

為每一個動作創建一個Command,把這些Command存儲在一個集合中, 并用獲取及執行Command的代碼替換條件邏輯。

重構步驟我不做詳細描述,看一下重構后的結果吧:

 

    public class class2    {        PRivate Dictionary<string, CommandAbstract> dic;        public class2()        {            this.dic = new Dictionary<string, CommandAbstract>();            this.dic.Add("Action1", new Command1());            this.dic.Add("Action2", new Command2());            this.dic.Add("Action3", new Command3());        }        /// <summary>        /// 應用 Command模式 替換 條件調度程序/// </summary>        /// <param name="actionName"></param>        public void DoAction(string actionName)        {            CommandAbstract command = null;            if (dic.ContainsKey(actionName))            {                command = dic[actionName];            }            if (command != null)            {                command.Execute();            }        }    }    public abstract class CommandAbstract    {        public abstract void Execute();    }    public class Command1 : CommandAbstract    {        public override void Execute()        {            Console.WriteLine("執行任務1");        }    }    public class Command2 : CommandAbstract    {        public override void Execute()        {            Console.WriteLine("執行任務2");        }    }    public class Command3 : CommandAbstract    {        public override void Execute()        {        }    }
  

看著 硬編碼 Dictionary 很不爽,如果經常需要添加新Command, 有可能還需要繼續重構——使其遵循開閉原則。

方案:使用反射代替硬編碼 (簡單的Plugin模式),重構后的結果如下:

    public static class CommandFactory    {        private static Dictionary<string, CommandAbstract> dic;        static CommandFactory()        {            dic = new Dictionary<string, CommandAbstract>();            Type absType = typeof(CommandAbstract);            Assembly assem = absType.Assembly;            foreach (Type t in assem.GetTypes())            {                if (t.IsClass && !t.IsAbstract && t.IsSubclassOf(absType))                {                    CommandAbstract command = Activator.CreateInstance(t) as CommandAbstract;                    if (command != null && !dic.ContainsKey(command.CommandName))                    {                        dic.Add(command.CommandName, command);                    }                }            }        }        public static CommandAbstract GetCommand(string commandName)        {            if (dic.ContainsKey(commandName))            {                return dic[commandName];            }            return null;        }    }    public class class2    {/// <summary>        /// 重構硬編碼/// </summary>        /// <param name="actionName"></param>        public void DoAction(string actionName)        {            CommandAbstract command = CommandFactory.GetCommand(actionName);            if (command != null)            {                command.Execute();            }        }    }    public abstract class CommandAbstract    {        public string CommandName { get; protected set; }        public abstract void Execute();    }    public class Command1 : CommandAbstract    {        public Command1()        {            this.CommandName = "Action1";        }        public override void Execute()        {            Console.WriteLine("執行任務1");        }    }    public class Command2 : CommandAbstract    {        public Command2()        {            this.CommandName = "Action2";        }        public override void Execute()        {            Console.WriteLine("執行任務2");        }    }    public class Command3 : CommandAbstract    {        public Command3()        {            this.CommandName = "Action3";        }        public override void Execute()        {        }    }

 

 

如果 條件表達式 較為復雜呢,那又可以怎樣重構?

提示:責任鏈模式。

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 深圳市| 郓城县| 松阳县| 喀喇沁旗| 武宣县| 交城县| 太白县| 康平县| 通海县| 宝鸡市| 内丘县| 苏尼特右旗| 云霄县| 元谋县| 乌鲁木齐县| 涞水县| 荆州市| 辽中县| 大埔县| 七台河市| 碌曲县| 岱山县| 兴义市| 登封市| 夏津县| 朝阳市| 德昌县| 黔江区| 新宁县| 隆回县| 通河县| 恩施市| 福泉市| 景德镇市| 汝南县| 农安县| 新宁县| 裕民县| 永登县| 建德市| 潞西市|