二十四种设计模式:责任链模式(Chain of Responsibility Pattern)

责任链模式(Chain of Responsibility Pattern)

介绍
为解除请求的发送者和接收者之间耦合,而使多个对象都有机会处理这个请求。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它。

示例
有一个Message实体类,某个类对它的操作有Insert()方法。现在要求根据插入信息的字符长度,让不同的对象去处理,这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它。

二十四种设计模式:责任链模式(Chain of Responsibility Pattern)

  MessageModel

using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.ChainOfResponsibility
{
/// <summary>
/// Message实体类
/// </summary>
public class MessageModel
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="msg">Message内容</param>
/// <param name="pt">Message发布时间</param>
public MessageModel(string msg, DateTime pt)
{
this._message = msg;
this._publishTime = pt;
} private string _message;
/// <summary>
/// Message内容
/// </summary>
public string Message
{
get { return _message; }
set { _message = value; }
} private DateTime _publishTime;
/// <summary>
/// Message发布时间
/// </summary>
public DateTime PublishTime
{
get { return _publishTime; }
set { _publishTime = value; }
}
}
}

  SqlMessage

using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.ChainOfResponsibility
{
/// <summary>
/// Sql方式操作Message
/// </summary>
public class SqlMessage
{
/// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns></returns>
public bool Insert(MessageModel mm)
{
// 代码略
return true;
}
}
}

  AbstractExecutor

using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.ChainOfResponsibility
{
/// <summary>
/// 抽象处理者(Handler)角色
/// </summary>
public abstract class AbstractExecutor
{
/// <summary>
/// 抽象处理者(Handler)角色
/// </summary>
protected AbstractExecutor _executor; /// <summary>
/// 设置责任链的上一级对象
/// </summary>
/// <param name="executor"></param>
public void SetSuccessor(AbstractExecutor executor)
{
this._executor = executor;
} /// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns>执行者;内容;时间</returns>
public abstract string Insert(MessageModel mm);
}
}

  Employee

using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.ChainOfResponsibility
{
/// <summary>
/// 具体处理者(ConcreteHandler)角色
/// </summary>
public class Employee : AbstractExecutor
{
/// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns>执行者;内容;时间</returns>
public override string Insert(MessageModel mm)
{
string rtn = ""; // 插入的信息字符数小于5
if (mm.Message.Length < 5)
{
SqlMessage m = new SqlMessage(); if (m.Insert(mm))
{
rtn = "执行者:雇员" + " 内容:" + mm.Message + " 时间:" + mm.PublishTime.ToString();
}
}
// 否则让上级去执行
else if (base._executor != null)
{
rtn = _executor.Insert(mm);
} return rtn;
}
}
}

  Leader

using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.ChainOfResponsibility
{
/// <summary>
/// 抽象处理者(Handler)角色
/// </summary>
public class Leader : AbstractExecutor
{
/// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns>执行者;内容;时间</returns>
public override string Insert(MessageModel mm)
{
string rtn = ""; // 插入的信息字符数小于10
if (mm.Message.Length < 10)
{
SqlMessage m = new SqlMessage(); if (m.Insert(mm))
{
rtn = "执行者:主管" + " 内容:" + mm.Message + " 时间:" + mm.PublishTime.ToString();
}
}
// 否则让上级去执行
else if (base._executor != null)
{
rtn = _executor.Insert(mm);
} return rtn;
}
}
}

  Manager

using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.ChainOfResponsibility
{
/// <summary>
/// 抽象处理者(Handler)角色
/// </summary>
public class Manager : AbstractExecutor
{
/// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message实体对象</param>
/// <returns>执行者;内容;时间</returns>
public override string Insert(MessageModel mm)
{
string rtn = ""; // 插入的信息字符数小于15
if (mm.Message.Length < 15)
{
SqlMessage m = new SqlMessage(); if (m.Insert(mm))
{
rtn = "执行者:经理" + " 内容:" + mm.Message + " 时间:" + mm.PublishTime.ToString();
}
}
else
{
rtn = "你所插入的Message不符合要求";
} return rtn;
}
}
}

  Client

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; using Pattern.ChainOfResponsibility; public partial class ChainOfResponsibility : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AbstractExecutor employee = new Employee();
AbstractExecutor leader = new Leader();
AbstractExecutor manager = new Manager();
employee.SetSuccessor(leader);
leader.SetSuccessor(manager); Response.Write(employee.Insert(new MessageModel("abcd", DateTime.Now)));
Response.Write("<br />");
Response.Write(employee.Insert(new MessageModel("abcdefgh", DateTime.Now)));
Response.Write("<br />");
Response.Write(employee.Insert(new MessageModel("abcdefghigkl", DateTime.Now)));
Response.Write("<br />");
Response.Write(employee.Insert(new MessageModel("abcdefghigklmnop", DateTime.Now)));
}
}

  运行结果
  执行者:雇员 内容:abcd 时间:2007-4-22 20:04:41
  执行者:主管 内容:abcdefgh 时间:2007-4-22 20:04:41
  执行者:经理 内容:abcdefghigkl 时间:2007-4-22 20:04:41
  你所插入的Message不符合要求

上一篇:职责链模式(chain of responsibility Pattern)


下一篇:ASP.NET - Web API,从简单类型到复杂类型的参数传递用例,以及传递简单string类型的解决办法