房屋设施重构例子
昨天晚上看《大话设计模式》重构部分,今天在写一个私有方法时,感觉一直在粘贴复制,很无聊,后来我就想办法重构,重构了3次,终于得到了想要的代码,很简洁。
(1)最初时公司有关58同城的代码是下面这样的:
最初代码
1 #region 填写房屋设施 2 /// <summary> 3 /// 填写房屋设施 4 /// </summary> 5 /// <param name="httpclient"></param> 6 /// <param name="qunfainfo"></param> 7 private void FangWuSheShi(ref HttpClient httpclient, QunFa qunfainfo) 8 { 9 string fwss = qunfainfo.ef_fwss; 10 if (string.IsNullOrEmpty(fwss)) 11 { 12 return; 13 } 14 foreach (string ss in fwss.Split(',')) 15 { 16 if (ss == "床") 17 { 18 httpclient.PostingData.Add("HouseAllocation", "6"); 19 } 20 if (ss == "热水器") 21 { 22 httpclient.PostingData.Add("HouseAllocation", "8"); 23 } 24 if (ss == "洗衣机") 25 { 26 httpclient.PostingData.Add("HouseAllocation", "9"); 27 } 28 if (ss == "空调") 29 { 30 httpclient.PostingData.Add("HouseAllocation", "10"); 31 } 32 if (ss == "冰箱") 33 { 34 httpclient.PostingData.Add("HouseAllocation", "11"); 35 } 36 if (ss == "电视机") 37 { 38 httpclient.PostingData.Add("HouseAllocation", "12"); 39 } 40 } 41 } 42 #endregion
上面的代码效率是很低的,比如foreach (string ss in fwss.Split(',')),每一次都要调用一次Split函数,应该把它用变量保存下来。
还有如果配对成功,应该加个continue,减少判断次数。
(2)下面是Yes515网站的第一个重构:
#region 房屋设施 /// <summary> /// 向httpclient中添加房屋设施参数 /// </summary> /// <param name="httpclient">httpclient对象</param> /// <param name="qunfainfo">房源信息</param> /// <param name="type">1售房,2租房</param> private void FangWuSheShi(ref HttpClient httpclient, QunFa qunfainfo, int type) { //缓存变量 string[] source = qunfainfo.ef_fwss.Split(','); if (source.Count() == 0) { return; } //保存post的参数 List<string> postParam = new List<string>(); for (int i = 0; i < source.Count(); i++) { if (source[i].Contains("")) { postParam.Add(""); continue; } if (source[i].Contains("")) { postParam.Add(""); continue; } if (source[i].Contains("")) { postParam.Add(""); continue; } if (source[i].Contains("")) { postParam.Add(""); continue; } } //售房添加参数 if (type == 1) { for (int i = 0; i < postParam.Count; i++) { httpclient.PostingData.Add("", postParam[i]); } } else { for (int i = 0; i < postParam.Count; i++) { httpclient.PostingData.Add("", postParam[i]); } } } #endregion
if括号体中的代码仅仅是post的参数不同,可以用数组保存下来。如果有很多信息时特别方便。
这次重构添加了continue,增加了类型,发售房和租房都可以使用。
(2)第二次重构
#region 房屋设施 /// <summary> /// 向httpclient中添加房屋设施参数 /// </summary> /// <param name="httpclient">httpclient对象</param> /// <param name="qunfainfo">房源信息</param> /// <param name="type">1售房,2租房</param> private void FangWuSheShi(ref HttpClient httpclient, QunFa qunfainfo, int type) { //缓存变量,保存本地房屋设施 string[] source = qunfainfo.ef_fwss.Split(','); if (source.Count() == 0) { return; } //将要匹配的信息和对应的post参数,比如 ('床'对应'床'或数字'1') string[] des1 = { "床", "家具", "有线电视", "洗衣机", "空调", "冰箱", "热水器", "宽带", "电话", "微波炉", "厨具", "煤气", "暖气", "电梯", "车位" }; string[] des2 = { "床", "家具", "有线电视", "洗衣机", "空调", "冰箱", "热水器", "宽带", "电话", "微波炉", "厨具", "煤气", "暖气", "电梯", "车位" }; for (int i = 0; i < source.Count(); i++) { for (int j = 0; j < des1.Count(); j++) { if(source[i].Contains(des1[j])) { if(type==1) { httpclient.PostingData.Add("",des2[i]); } else { httpclient.PostingData.Add("",des2[i]); } continue; } } } } #endregion
重构之后代码量明显减少,仍有不足。
因为数据库中存放使用字段fwss按照(床,电视,洗衣机)的形式存放的,因此可以省去一层循环。
(3)第三次重构
#region 房屋设施 /// <summary> /// 向httpclient中添加房屋设施参数 /// </summary> /// <param name="httpclient">httpclient对象</param> /// <param name="qunfainfo">房源信息</param> /// <param name="type">1售房,2租房</param> private void FangWuSheShi(ref HttpClient httpclient, QunFa qunfainfo, int type) { //缓存变量,保存本地房屋设施 string sheShi = qunfainfo.ef_fwss; if (string.IsNullOrEmpty(sheShi)) { return; } //将要匹配的信息和对应的post参数,比如 ('床'对应'床'或数字'1') string[] des1 = { "床", "家具", "有线电视", "洗衣机", "空调", "冰箱", "热水器", "宽带", "电话", "微波炉", "厨具", "煤气", "暖气", "电梯", "车位" }; string[] des2 = { "床", "家具", "有线电视", "洗衣机", "空调", "冰箱", "热水器", "宽带", "电话", "微波炉", "厨具", "煤气", "暖气", "电梯", "车位" }; //需要比较的次数 int n = des1.Count(); for (int i = 0; i < n; i++) { if(sheShi.Contains(des1[i])) { if (type == 1) { httpclient.PostingData.Add("esf.peiTao", des2[i]); } else { httpclient.PostingData.Add("zufang.peiTao", des2[i]); } continue; } } } #endregion
如果有很多类似的代码,请考虑重构,把相同的部分提出出来,不同的可以考虑使用多态或者数组实现。