责任链模式(Chain of Responsibility Pattern)

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

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

  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)的更多相关文章

  1. 23种设计模式--责任链模式-Chain of Responsibility Pattern

    一.责任链模式的介绍 责任链模式用简单点的话来说,将责任一步一步传下去,这就是责任,想到这个我们可以相当击鼓传花,这个是为了方便记忆,另外就是我们在项目中经常用到的审批流程等这一类的场景时我们就可以考 ...

  2. C#设计模式-责任链模式(Chain of Responsibility Pattern)

    引子 一个事件需要经过多个对象处理是一个挺常见的场景,譬如采购审批流程,请假流程,软件开发中的异常处理流程,web请求处理流程等各种各样的流程,可以考虑使用责任链模式来实现.现在以请假流程为例,一般公 ...

  3. 乐在其中设计模式(C#) - 责任链模式(Chain of Responsibility Pattern)

    原文:乐在其中设计模式(C#) - 责任链模式(Chain of Responsibility Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 责任链模式(Chain of R ...

  4. 设计模式-责任链模式Chain of Responsibility)

    一.定义 职责链模式是一种对象的行为模式.在职责链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求的客户端并不知道链 ...

  5. C#设计模式——职责链模式(Chain Of Responsibility Pattern)

    一.概述 在软件开发中,某一个对象的请求可能会被多个对象处理,但每次最多只有一个对象处理该请求,对这类问题如果显示指定请求的处理对象,那么势必会造成请求与处理的紧耦合,为了将请求与处理解耦,我们可以使 ...

  6. 二十四种设计模式:命令模式(Command Pattern)

    命令模式(Command Pattern) 介绍将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可取消的操作. 示例有一个Message实体类,某个 ...

  7. 二十四种设计模式:解释器模式(Interpreter Pattern)

    解释器模式(Interpreter Pattern) 介绍给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子. 示例有一个Message实体类,某个类对它的 ...

  8. 二十四种设计模式:迭代器模式(Iterator Pattern)

    迭代器模式(Iterator Pattern) 介绍提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示. 示例有一个Message实体类,某聚合对象内的各个元素均为该实体对象,现 ...

  9. 二十四种设计模式:策略模式(Strategy Pattern)

    策略模式(Strategy Pattern) 介绍定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.本模式使得算法的变化可独立于使用它的客户. 示例有一个Message实体类,对它的操作有 ...

随机推荐

  1. android-volley-at-a-glance

    http://liubin.org/2013/05/27/android-volley-at-a-glance/ http://www.androidhive.info/2014/05/android ...

  2. C++除法取整

    使用floor函数.floor(x)返回的是小于或等于x的最大整数.如:     floor(2.5) = 2 floor(-2.5) = -3 使用ceil函数.ceil(x)返回的是大于x的最小整 ...

  3. GPRS Sniffing Tutorial

    - Download sources into ~/gprs_sniffer git clone git://git.osmocom.org/osmocom-bb.git git clone git: ...

  4. BZOJ 4390 Max Flow

    同运输计划. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm ...

  5. vijos 1779 国王游戏

    练了一下高精度..结果敲了这么久... #include<iostream> #include<cstdio> #include<cstring> #include ...

  6. dfs介绍

    深度优先搜索(DFS) [算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍 ...

  7. linux centos安装编译phantomjs 2.0的方法

    phantomjs 2.0最新版的官方不提供编译好的文件下载,只能自己编译,有教程但是过于简单,特别是服务器上要安装N多的支持.折腾到现在终于装好了并且能正常运行了,截图mark一下: linux c ...

  8. MonogoDB的GirdFS

    GirdFS是一种在MongoDB中存储大二进制文件的机制. mongofiles内置在MongoDB发布版中,可以用来在GridFS中上传.下载.列示.查找或删除文件. $ echo "H ...

  9. CoreData的使用入门到精通

    源码下载地址: http://download.csdn.net/download/huntaiji/6664567 一,创建项目文件--选择Empty Application  起名:CoreDat ...

  10. explicit用法

    explicit用来防止由构造函数定义的隐式转换. 要明白它的作用,首先要了解隐式转换:可以用单个实参来调用的构造函数定义了从形参类型到该类类型的一个隐式转换. 例如: class things { ...