《代碼重構(gòu)怎么做?_》由會(huì)員分享,可在線(xiàn)閱讀,更多相關(guān)《代碼重構(gòu)怎么做?_(11頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、代碼重構(gòu)怎么做?_
這幾天,接手一個(gè)同事的代碼,關(guān)于微信接口開(kāi)發(fā)的,那一堆的 if,看得哥蛋痛了,這個(gè)毛病也是許多新手簡(jiǎn)單犯的,所以特地把這次重構(gòu)寫(xiě)出來(lái)。
下面來(lái)我們看看這個(gè)代碼的問(wèn)題所在,if else 里面的代碼塊規(guī)律,不好改,使得它的重用性為 0,并且難以閱讀。當(dāng)然,假如 if 只有一兩個(gè),或者3個(gè),這樣寫(xiě)是問(wèn)題不大的。
但是假如多了,這種代碼便會(huì)讓維護(hù)變得困難起來(lái)。
if (strMsgType == "text")
{
textContentClient = rootElement.SelectSingleNode("Content").
2、InnerText;
strResult = SetMsgType_Text(strClientName, textContentClient, db, strServerName, Identity);
System.Diagnostics.Trace.WriteLine(strResult);
return Content(strResult);
}
else if (strMsgType == "event")
{
string eventType = rootElement.SelectSingleNode("Event").In
3、nerText.ToLower();
if (eventType == "subscribe")
{
string keyCode = "";
if (rootElement.SelectSingleNode("EventKey") != null)
keyCode = rootElement.SelectSingleNode("EventKey").InnerText.ToLower();
strResult = FormatEventSubscribe(keyCode);
RecordReplyMessage();
retur
4、n Content(strResult);
}
else if (eventType == "scan")
{
string keyCode = rootElement.SelectSingleNode("EventKey").InnerText.ToLower();
var outLetName = "歡迎關(guān)注";
var outletDB = ShoppingContext.CreateInstance(Identity);
var outLetModel = outletDB.Outlets.FirstOrDefault(o = o
5、.SceneId == Int32.Parse(keyCode));
if (outLetModel != null)
outLetName += outLetModel.Name;
return Content(GetTextTemp(strClientName, strServerName, outLetName));
}
else if (eventType == "click")
{
string keyCode = rootElement.SelectSingleNode("EventKey").InnerText.ToLowe
6、r();
strResult = FomatMenuMessage(keyCode);
return Content(strResult);
}
else if (eventType == "unsubscribe")
{
var subIds = db.ReplyRecords.Where(r = r.FromOpenId == this.ClientId.ToString() r.EMessType == EEventType.Subscribe.ToString() r.KeyWord != null).Select(o = o.Key
7、Word).ToArray();
var unSubIds = db.ReplyRecords.Where(r = r.FromOpenId == this.ClientId.ToString() r.EMessType == EEventType.Unsubscribe.ToString() r.KeyWord != null).Select(o = o.KeyWord).ToArray();
var SencesId = "";
foreach (var k in subIds)
{
if (!unSubIds.Contains(k))
8、{
this.ReplyModel.KeyWord = k;
break;
}
}
this.ReplyModel.EMessType = EEventType.Unsubscribe.ToString();
RecordReplyMessage();
}
}
else if (strMsgType.ToLower() == "location")
{
string strLocation_X = rootElement.SelectSingleNode("Location_X").InnerText;
9、
string strLocation_Y = rootElement.SelectSingleNode("Location_Y").InnerText;
strResult = FormatOutLetLBS(double.Parse(strLocation_X), double.Parse(strLocation_Y), 10);
//strResult = FormatTextMessage(strLocation_X + "|" + strLocation_Y);
return Content(strResult);
}
else if (s
10、trMsgType.ToLower() == "image")
{
string strImgUrl = rootElement.SelectSingleNode("PicUrl").InnerText;
}
一種比較好的處理方法就是把語(yǔ)句塊內(nèi)的代碼抽出來(lái),寫(xiě)成函數(shù),如下面所示:
public class MessageProcesser
{
public ReplyMessage Process(string xml)
{
var msg = PostMessage.FromXml(xml);
switch (m
11、sg.MsgType)
{
case Models.PostMessageType.Event:
var eventType = ((EventMessage)msg).Event;
switch (eventType)
{
case EventType.Click:
return ProcessClickEvent((ClickEvent)msg);
case EventType.Location:
return ProcessLocationEvent((LocationEvent)msg);
case Eve
12、ntType.Scan:
return ProcessScanEvent((ScanEvent)msg);
case EventType.Subscribe:
return ProcessSubscribeEvent((SubscribeEvent)msg);
case EventType.Unsubscribe:
return ProcessUnsubscribeEvent((UnsubscribeEvent)msg);
}
break;
case Models.PostMessageType.Image:
return
13、 ProcessImageMessage((ImageMessage)msg);
case Models.PostMessageType.Link:
return ProcessLinkMessage((LinkMessage)msg);
case Models.PostMessageType.Location:
return ProcessLocationMessage((LocationMessage)msg);
case Models.PostMessageType.Text:
return ProcessTextMessage((Text
14、Message)msg);
case Models.PostMessageType.Video:
return ProcessVideoMessage((VideoMessage)msg);
case Models.PostMessageType.Voice:
return ProcessVoiceMessage((VoiceMessage)msg);
}
return null;
}
protected virtual ReplyMessage ProcessClickEvent(ClickEvent msg)
{
15、 return DefaultProcess(msg);
}
protected virtual ReplyMessage ProcessLocationEvent(LocationEvent msg)
{
return DefaultProcess(msg);
}
protected virtual ReplyMessage ProcessScanEvent(ScanEvent msg)
{
return DefaultProcess(msg);
}
protected virtual ReplyMessage
16、 ProcessSubscribeEvent(SubscribeEvent msg)
{
return DefaultProcess(msg);
}
protected virtual ReplyMessage ProcessUnsubscribeEvent(UnsubscribeEvent msg)
{
return DefaultProcess(msg);
}
protected virtual ReplyMessage ProcessImageMessage(ImageMessage msg)
{
retur
17、n DefaultProcess(msg);
}
protected virtual ReplyMessage ProcessLinkMessage(LinkMessage msg)
{
return DefaultProcess(msg);
}
protected virtual ReplyMessage ProcessLocationMessage(LocationMessage msg)
{
return DefaultProcess(msg);
}
protected virtual ReplyMessag
18、e ProcessTextMessage(TextMessage msg)
{
return DefaultProcess(msg);
}
protected virtual ReplyMessage ProcessVideoMessage(VideoMessage msg)
{
return DefaultProcess(msg);
}
protected virtual ReplyMessage ProcessVoiceMessage(VoiceMessage msg)
{
return DefaultProc
19、ess(msg);
}
protected virtual ReplyMessage DefaultProcess(PostMessage msg)
{
var reply = new TextReply(msg);
if (msg.MsgType == PostMessageType.Event)
{
reply.Content = string.Format("{0} event is not processed.", ((EventMessage)msg).Event);
}
else
{
reply.Content = string.Format("{0} message is not processed.", msg.MsgType);
}
return reply;
}
}
更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄
...