二十四种设计模式:责任链模式(Chain of Responsibility Pattern)
责任链模式(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)的更多相关文章
- 23种设计模式--责任链模式-Chain of Responsibility Pattern
一.责任链模式的介绍 责任链模式用简单点的话来说,将责任一步一步传下去,这就是责任,想到这个我们可以相当击鼓传花,这个是为了方便记忆,另外就是我们在项目中经常用到的审批流程等这一类的场景时我们就可以考 ...
- C#设计模式-责任链模式(Chain of Responsibility Pattern)
引子 一个事件需要经过多个对象处理是一个挺常见的场景,譬如采购审批流程,请假流程,软件开发中的异常处理流程,web请求处理流程等各种各样的流程,可以考虑使用责任链模式来实现.现在以请假流程为例,一般公 ...
- 乐在其中设计模式(C#) - 责任链模式(Chain of Responsibility Pattern)
原文:乐在其中设计模式(C#) - 责任链模式(Chain of Responsibility Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 责任链模式(Chain of R ...
- 设计模式-责任链模式Chain of Responsibility)
一.定义 职责链模式是一种对象的行为模式.在职责链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求的客户端并不知道链 ...
- C#设计模式——职责链模式(Chain Of Responsibility Pattern)
一.概述 在软件开发中,某一个对象的请求可能会被多个对象处理,但每次最多只有一个对象处理该请求,对这类问题如果显示指定请求的处理对象,那么势必会造成请求与处理的紧耦合,为了将请求与处理解耦,我们可以使 ...
- 二十四种设计模式:命令模式(Command Pattern)
命令模式(Command Pattern) 介绍将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可取消的操作. 示例有一个Message实体类,某个 ...
- 二十四种设计模式:解释器模式(Interpreter Pattern)
解释器模式(Interpreter Pattern) 介绍给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子. 示例有一个Message实体类,某个类对它的 ...
- 二十四种设计模式:迭代器模式(Iterator Pattern)
迭代器模式(Iterator Pattern) 介绍提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示. 示例有一个Message实体类,某聚合对象内的各个元素均为该实体对象,现 ...
- 二十四种设计模式:策略模式(Strategy Pattern)
策略模式(Strategy Pattern) 介绍定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.本模式使得算法的变化可独立于使用它的客户. 示例有一个Message实体类,对它的操作有 ...
随机推荐
- 数组prototype添加函数呢,采用回调判定函数内容
1.解决方案 Array.prototype.all = function (p) { return this.filter(p).length == this.length; }; Array.pr ...
- spark与Hadoop区别
2分钟读懂Hadoop和Spark的异同 2016.01.25 11:15:59 来源:51cto作者:51cto ( 0 条评论 ) 谈到大数据,相信大家对Hadoop和Apache Spark ...
- android-volley-at-a-glance
http://liubin.org/2013/05/27/android-volley-at-a-glance/ http://www.androidhive.info/2014/05/android ...
- Linux Mint下安装JDK
Linux Mint 17下安装的是默认的OpenJDK,可以使用java -version查看 现在需要使用Sun/Oracle官方的JDK:http://www.oracle.com/techne ...
- win7下环境搭建
1.Python下载 https://www.python.org/downloads/windows/ 选择需要安装的版本,我偷懒装的可执行文件. 下载之后双击安装就OK啦,安装过程中有一项偷懒的选 ...
- C# delegate & event
public delegate void MyDelegate(string mydelegate);//声明一个delegate对象 //实现有相同参数和返回值的函数 public v ...
- 20145210姚思羽《Java程序设计》实验一实验报告
实验一 Java开发环境的熟悉(Linux + Eclipse) 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验知识点 1. ...
- Number Game_状态压缩
Description Christine and Matt are playing an exciting game they just invented: the Number Game. The ...
- 爆破vcrkme01(已补上注册机)
系统 : Windows xp 程序 : vcrkme01 程序下载地址 :http://pan.baidu.com/s/1mh1n33y 要求 : 爆破 使用工具 :OD 可在“PEDIY Crac ...
- JQuery事件手册
blur.focus blur失去焦点:focus获得焦点 load 当指定的元素(及子元素)已加载时,会发生 load() 事件 resize 当调整浏览器窗口的大小时,发生 resize ...