设计模式|职责链模式--状态模式--策略模式

一、职责链模式
使每一个对象都有机会处理请求,避免请求的发送方和接受方的耦合。使任务沿着一条链传递,最终有人处理它。
【设计模式|职责链模式--状态模式--策略模式】优点:
请求者和接收者松耦合。
动态组合职责。
缺点:
会产生很多细粒度对象。
请求不一定被处理。
需要默认处理方法。
过于灵活,在使用它时需要确定下一个对象是谁。
本质是分离职责,动态耦合。
与状态模式的对比,状态模式是一个对象内在状态发生变化,相对稳定,而且固定好处理顺序,知道下一个对象是谁。
而责任链模式是多个对象之间的改变,各个对象不知道下个对象,需要客户端确定。
状态模式--IF ELSE
责任链模式--SWITCH(客户端确定分支)
而责任链模式的多个对象,动态组合。
三者对比
状态模式定义是:通过改变对象内部状态来改变对象的行为,这个对象的表现就像改变了类一样。
职责链模式指的是:使每一个对象都有机会处理请求,避免请求的发送方和接受方的耦合。使任务沿着一条链传递,最终有人处理它。构建一系列类,分别承担不同任务,并通过一定方式连起来,就像一条链。
策略模式定义是:准备一组算法,并封装起来,使其可以互换。
二、职责链模式角色
1、抽象处理者
2、具体处理者
三、代码实例一
请假内容

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChainTwo { class Request { private string type; private string content; private int num; public string Type { get => type; set => type = value; } public string Content { get => content; set => content = value; } public int Num { get => num; set => num = value; } } }

抽象处理者
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChainOne { abstract class Handler { protected Handler successor; public void SetSuccessor(Handler handler) { this.successor = handler; } public abstract void HandleRequest(int request); } }

含有自身对象的引用,定义为私有类型,定义一个抽象函数,让具体实现类实现。
具体处理者
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChainOne { class HandlerOne : Handler { public override void HandleRequest(int request) { if(request>0&&request<=10) { Console.WriteLine("{0}处理请求{1}", this.GetType().Name, request); }else if(successor!=null) { successor.HandleRequest(request); } } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChainOne { class HandlerTwo : Handler { public override void HandleRequest(int request) { if (request >= 10 && request < 20) { Console.WriteLine("{0}处理请求{1}", this.GetType().Name, request); } else if (successor != null) { successor.HandleRequest(request); } } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChainOne { class HandlerThree : Handler { public override void HandleRequest(int request) { if(request >= 20 ) { Console.WriteLine("{0}处理请求{1}", this.GetType().Name, request); } else if (successor != null) { successor.HandleRequest(request); }} } }

测试
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace ChainOne { static class Program { /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Handler a = new HandlerOne(); Handler b = new HandlerTwo(); Handler c = new HandlerThree(); a.SetSuccessor(b); b.SetSuccessor(c); int []re = { 1, 2, 6, 30, 9, 12 }; foreach (int i in re) { a.HandleRequest(i); } } } }

四、代码实例二
加薪
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChainTwo { abstract class Handler { protected Handler successor; public void SetSuccessor(Handler h) { this.successor = h; } abstract public void Request(Request r); } }

具体处理者
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChainTwo { class Manager : Handler { public override void Request(Request r) { if(r.Type=="请假"&&r.Num<=2) { Console.WriteLine("{0}{1}天,被批准", r.Type, r.Num); }else if(successor!=null) { successor.Request(r); } } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChainTwo { class MajorDomo : Handler { public override void Request(Request r) { if (r.Type == "请假" && r.Num <= 5) { Console.WriteLine("{0}{1}天,被批准", r.Type, r.Num); } else if (successor != null) { successor.Request(r); } } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChainTwo { class SuperMajor : Handler { public override void Request(Request r) { if (r.Type == "请假" && r.Num <= 10) { Console.WriteLine("{0}{1}天,被批准", r.Type, r.Num); } else if(r.Type == "加薪" && r.Num <= 500) { Console.WriteLine("{0}{1}元,被批准", r.Type, r.Num); }else if (r.Type == "加薪" && r.Num >=500) { Console.WriteLine("考虑一下!"); } else { Console.WriteLine("未被批准!"); } } } }

测试
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace ChainTwo { static class Program { /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Request re = new Request(); re.Type = "请假"; re.Content = "可不可以"; re.Num = 5; Handler h1 = new Manager(); Handler h2 = new MajorDomo(); Handler h3 = new SuperMajor(); h1.SetSuccessor(h2); h2.SetSuccessor(h3); h1.Request(re); re.Type = "加薪"; re.Num = 300; h1.Request(re); } } }

设计模式|职责链模式--状态模式--策略模式
文章图片

    推荐阅读