架构,改善程序复用性的设计~第四讲 方法的重载真的用不到吗?

在第三讲中我们主要关注了代码重构的思想,从方法重构到类重构再到项目重构,这是一个过程,一种思想上的升华,今天将继续我们“程序复用性设计”的旅程,说一下方法重载的重要性。

细心的朋友一定会非常关注net frameworks的源代码,即使只能看到它内部方法的定义,也足够了,在System.Web.Mvc这个命名空间下定义了很多关于MVC架构的东西,我们就以它为例来说一下方法重载吧!

重载的好处:

1 对方法调用的程序员来说,它是友好的(程序员只关心自己调用的方法签名即可,不用管参数为NULL怎么办这些逻辑)

2 对于代码维护量来说,它是容易的(核心代码只放在参数签名最多的方法中)

3 对于代码扩展来说,它是直接的(只要修改核心方法代码即可,而不用一个方法一个方法的去COPY)

以下是InputExtensions类下的几个方法:

 1        public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name)
 2         {
 3             return htmlHelper.CheckBox(name, null);
 4         }
 5         
 6         public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name, bool isChecked)
 7         {
 8             return htmlHelper.CheckBox(name, isChecked, null);
 9         }
10         
11         public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name, IDictionary<string, object> htmlAttributes)
12         {
13             return CheckBoxHelper(htmlHelper, null, name, null, htmlAttributes);
14         }
15         
16         public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name, object htmlAttributes)
17         {
18             return htmlHelper.CheckBox(name, ((IDictionary<string, object>) HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));
19         }
20         
21         public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name, bool isChecked, IDictionary<string, object> htmlAttributes)
22         {
23             return CheckBoxHelper(htmlHelper, null, name, new bool?(isChecked), htmlAttributes);
24         }
25         
26         public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name, bool isChecked, object htmlAttributes)
27         {
28             return htmlHelper.CheckBox(name, isChecked, ((IDictionary<string, object>) HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)));
29         }
30         

我们看到这是一个在视图上建立复选框的方法,也是HtmlHelper类型的扩展方法,(即通过HtmlHelper的实例对象直接可以访问到这个方法),它提供了六个方法重载,程序员在使用时根据业务需求去调用,而在参数最少的方法中,直接为参数赋一个默认值即可。

我的项目中的方法重载:

以下是一个通过状态枚举类型,产生一个下拉列表框的扩展方法,它有两个重载方法,第一个是产生下拉列表 (public static MvcHtmlString StatusForSelectList(this HtmlHelper html)),第二个是在产生下拉列表时,给它设一个默认值( public static MvcHtmlString StatusForSelectList(this HtmlHelper html, int selectedValue)),这时使用方法重载就更友好,而另一种作法就是直接用一个方法代替,这对于调用方的程序员来说是不友好的。

下面是两个方式的实现:

不友好的方式:

 1         /// <summary>
 2         /// 状态字段下列列表框,要求调用方的程序员为它传送一个默认值,如0,即使写成int? selectValue,也会使程序员产生歧义
 3         /// </summary>
 4         /// <param name="html"></param>
 5         /// <param name="selectedValue">选中的项</param>
 6         /// <returns></returns>
 7         public static MvcHtmlString StatusForSelectList(this HtmlHelper html, int selectedValue)
 8         {
 9             StringBuilder sb = new StringBuilder();
10             sb.Append("<select name='Status' id='Status'>");
//selectedValue等于0 ,然后去进行一个逻辑的实现
11 foreach (Status i in Enum.GetValues(typeof(Status))) 12 if ((int)i == selectedValue) 13 sb.AppendFormat("<option value='{0}' selected='selected'>{1}</option>", (int)i, ((Status)i).GetDescription()); 14 else 15 { 16 if (i == Status.Normal) sb.AppendFormat("<option value='{0}' selected='selected'>{1}</option>", (int)i, ((Status)i).GetDescription()); 17 else 18 sb.AppendFormat("<option value='{0}'>{1}</option>", (int)i, ((Status)i).GetDescription()); 19 } 20 sb.Append("</select>"); 21 return MvcHtmlString.Create(sb.ToString()); 22 }

下面这种方式是我们提倡的,也是微软主推的:

 1         /// <summary>
 2         /// 状态字段下列列表框
 3         /// </summary>
 4         /// <param name="html"></param>
 5         /// <param name="selectedValue">选中的项</param>
 6         /// <returns></returns>
 7         public static MvcHtmlString StatusForSelectList(this HtmlHelper html, int selectedValue)
 8         {
 9             StringBuilder sb = new StringBuilder();
10             sb.Append("<select name='Status' id='Status'>");
11             foreach (Status i in Enum.GetValues(typeof(Status)))
12                 if ((int)i == selectedValue)
13                     sb.AppendFormat("<option value='{0}' selected='selected'>{1}</option>", (int)i, ((Status)i).GetDescription());
14                 else
15                 {
16                     if (i == Status.Normal) sb.AppendFormat("<option value='{0}' selected='selected'>{1}</option>", (int)i, ((Status)i).GetDescription());
17                     else
18                         sb.AppendFormat("<option value='{0}'>{1}</option>", (int)i, ((Status)i).GetDescription());
19                 }
20             sb.Append("</select>");
21             return MvcHtmlString.Create(sb.ToString());
22         }
23 
24         /// <summary>
25         /// 状态字段下列列表框
26         /// </summary>
27         /// <param name="html"></param>
28         /// <returns></returns>
29         public static MvcHtmlString StatusForSelectList(this HtmlHelper html)
30         {
31             return StatusForSelectList(html, 100);
32         }

这使得代码很清晰,职责分明!

原文地址:https://www.cnblogs.com/lori/p/2528058.html