从真实项目中抠出来的设计模式——第三篇:责任链模式
一:現實場景
? ? 有時候在開發的過程中,我們經常會根據某個狀態的值,寫出很多的ifelse邏輯,比如拿項目里面的案例來說,如果當前發送的是彩信,此種狀態需要如何給實體賦值,如果是短信,郵件又是其他方式的賦值,等等此類,這種情況下一般會寫出如下if判斷,對吧,真實代碼如下:
?if (leaflet.CommunicationtypeEnum.HasFlag(CommunicationTypeEnum.郵件))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? //第三步:動態生成郵件模板
? ? ? ? ? ? ? ? ? ? var styleInfo = CacheUtil.GetRandomEmailStyle();
? ? ? ? ? ? ? ? ? ? var tuple = new EdmDraftBoxBLL().GetEdmHtmlTitle(communicationInfo.EDMJson, styleInfo.StyleId);
? ? ? ? ? ? ? ? ? ? leaflet.Title = tuple.Item1;
? ? ? ? ? ? ? ? ? ? leaflet.EDMContent = tuple.Item2;
? ? ? ? ? ? ? ? ? ? leaflet.Header = tuple.Item3;
? ? ? ? ? ? ? ? ? ? leaflet.SendSMSCount = 1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (leaflet.CommunicationtypeEnum.HasFlag(CommunicationTypeEnum.短信))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? leaflet.SMSContent = communicationInfo.SMSContent;
? ? ? ? ? ? ? ? ? ? leaflet.SendSMSCount = communicationInfo.SMSCount;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? if (leaflet.CommunicationtypeEnum.HasFlag(CommunicationTypeEnum.彩信))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? leaflet.MMSContent = communicationInfo.MMSContent;
? ? ? ? ? ? ? ? }
上面的代碼還是非常簡單明了的,程序會根據leaflet.CommunicationtypeEnum的不同做不同的判斷,比如說當前狀態是郵件的話,程序會從30套郵件
模板庫中隨機抽取一封,給leaflet的title,header...賦值,有些人可能會說這段代碼不難看哈,確實是這樣,但是如果面對需求變更呢?比如說后期需要增加微信,微博渠道,那是不是又要加上兩個if才能把這個問題解決呢? 這就違背了設計模式中開閉原則,對吧,面對這種場景,可以用責任鏈模式擺平。
?
二:責任鏈模式
? ? ?責任鏈模式講的就是將請求的發送者和接收者進行分離,避免請求發送者與接收者耦合在一起,讓多個對象都有可能接收請求,將這些對象連接成一條鏈,并且沿著這條鏈傳遞請求,直到有對象處理它為止,面對需求變更,只需要更加處理類就好了,而且客戶端可以按照自己的需求拼接處理鏈條,是不是很強大。
1.?AbstractComunication
public abstract class AbstractComunication
? ? {
? ? ? ? AbstractComunication abstractComunication = null;
? ? ? ? public void SetHandler(AbstractComunication abstractComunication)
? ? ? ? {
? ? ? ? ? ? this.abstractComunication = abstractComunication;
? ? ? ? }
? ? ? ? public abstract void HanderRequest(LeafletEntity leaflet,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? EventmarketingSmsEdmContentInfo communicationInfo);
? ? }
2.?MMSComunication
public class MMSComunication : AbstractComunication
? ? {
? ? ? ? public override void HanderRequest(LeafletEntity leaflet, EventmarketingSmsEdmContentInfo communicationInfo)
? ? ? ? {
? ? ? ? ? ? if (leaflet.CommunicationtypeEnum.HasFlag(CommunicationTypeEnum.彩信))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? leaflet.MMSContent = communicationInfo.MMSContent;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? abstractComunication.HanderRequest(leaflet, communicationInfo);
? ? ? ? ? ? }
? ? ? ? }
? ? }
3.EDMComunication
public class EDMComunication : AbstractComunication
? ? {
? ? ? ? public override void HanderRequest(LeafletEntity leaflet, EventmarketingSmsEdmContentInfo communicationInfo)
? ? ? ? {
? ? ? ? ? ? if (leaflet.CommunicationtypeEnum.HasFlag(CommunicationTypeEnum.郵件))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //第三步:動態生成郵件模板
? ? ? ? ? ? ? ? var styleInfo = CacheUtil.GetRandomEmailStyle();
? ? ? ? ? ? ? ? var tuple = new EdmDraftBoxBLL().GetEdmHtmlTitle(communicationInfo.EDMJson, styleInfo.StyleId);
? ? ? ? ? ? ? ? leaflet.Title = tuple.Item1;
? ? ? ? ? ? ? ? leaflet.EDMContent = tuple.Item2;
? ? ? ? ? ? ? ? leaflet.Header = tuple.Item3;
? ? ? ? ? ? ? ? leaflet.SendSMSCount = 1;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? abstractComunication.HanderRequest(leaflet, communicationInfo);
? ? ? ? ? ? }
? ? ? ? }
? ? }
4.SMSComunication
public class SMSComunication : AbstractComunication
? ? {
? ? ? ? public override void HanderRequest(LeafletEntity leaflet, EventmarketingSmsEdmContentInfo communicationInfo)
? ? ? ? {
? ? ? ? ? ? if (leaflet.CommunicationtypeEnum.HasFlag(CommunicationTypeEnum.短信))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? leaflet.SMSContent = communicationInfo.SMSContent;
? ? ? ? ? ? ? ? leaflet.SendSMSCount = communicationInfo.SMSCount;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? abstractComunication.HanderRequest(leaflet, communicationInfo);
? ? ? ? ? ? }
? ? ? ? }
? ? }
5.客戶端調用
AbstractComunication communication1 = new EDMComunication();
?AbstractComunication communication2 = new SMSComunication();
AbstractComunication communication3 = new MMSComunication();
?//手工將三個Comunication 憑借成一個鏈條,形成單鏈表的模型
?communication1.SetHandler(communication2);
communication2.SetHandler(communication3);
?communication1.HanderRequest(leaflet, communicationInfo);
其實上面的代碼,需要繞一下腦子的就是如何通過SetHandler將三個xxxComunication拼接成一個單鏈表的形式,鏈表怎么拼接在于客戶端如何設置sethandler,
靈活性完全就在客戶端這邊,然后就非常方便將leaflet在責任鏈中游走,最終會被某一狀態處理邏輯處理,講到這里,我想大家應該都知道責任鏈模式是干嘛的了,
由于是真實案例就不方便跑代碼了,下面我構建一個責任鏈模型,大家比照一下就可以了,是不是有種請求和處理的分離,而且我還可以根據需要組合我的責任鏈,
其實js的冒泡機制就是這種模式的一個體現。
public abstract class AbstractHandler
? ? {
? ? ? ? protected AbstractHandler abstractHandler = null;
? ? ? ? public void SetHandler(AbstractHandler abstractHandler)
? ? ? ? {
? ? ? ? ? ? this.abstractHandler = abstractHandler;
? ? ? ? }
? ? ? ? public virtual void HandleRequest(int request) { }
? ? }
? ?public class ConcreteHandler1 : AbstractHandler
? ? {
? ? ? ? public override void HandleRequest(int request)
? ? ? ? {
? ? ? ? ? ? if (request == 1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("handler1 給你處理了");
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? abstractHandler.HandleRequest(request);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public class ConcreteHandler2 : AbstractHandler
? ? {
? ? ? ? public override void HandleRequest(int request)
? ? ? ? {
? ? ? ? ? ? if (request == 2)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("handler2 給你處理了");
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? abstractHandler.HandleRequest(request);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public class ConcreteHandler3 : AbstractHandler
? ? {
? ? ? ? public override void HandleRequest(int request)
? ? ? ? {
? ? ? ? ? ? if (request == 3)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("handler3 給你處理了");
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? abstractHandler.HandleRequest(request);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? AbstractHandler hander1 = new ConcreteHandler1();
? ? ? ? ? ? AbstractHandler hander2 = new ConcreteHandler2();
? ? ? ? ? ? AbstractHandler hander3 = new ConcreteHandler3();
? ? ? ? ? ? hander1.SetHandler(hander2);
? ? ? ? ? ? hander2.SetHandler(hander3);
? ? ? ? ? ? hander1.HandleRequest(3);
? ? ? ? }
? ? }
?
好了,模板和實際項目的案例都給大家展示了,希望能幫助到你
相關文章;
從真實項目中摳出來的設計模式——第一篇:策略模式
從真實項目中摳出來的設計模式——第二篇:過濾器模式
原文地址:http://www.cnblogs.com/huangxincheng/p/6429284.html
.NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注
總結
以上是生活随笔為你收集整理的从真实项目中抠出来的设计模式——第三篇:责任链模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么转换到Visual Studio
- 下一篇: asp.net ajax控件工具集 Au