结构
意图 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
适用性
  • 有多个的对象可以处理一个请求,哪个对象处理该请求运行时刻自动确定。
  • 你想在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。
  • 可处理一个请求的对象集合应被动态指定。
 using System;

     abstract class Handler
{
protected Handler successorHandler;
abstract public void HandleRequest(Request request);
public void SetSuccessor(Handler sucessor)
{
successorHandler = sucessor;
}
} class ConcreteHandler1 : Handler
{
override public void HandleRequest(Request request)
{
// determine if we can handle the request
if (request.RequestType == ) // some complex decision making!
{
// request handling code goes here
Console.WriteLine("request handled in ConcreteHandler1");
}
else
{
// not handled here - pass on to next in the chain
if (successorHandler != null)
successorHandler.HandleRequest(request);
}
}
} class ConcreteHandler2 : Handler
{
override public void HandleRequest(Request request)
{
// determine if we can handle the request
if (request.RequestType == ) // some complex decision making!
{
// request handling code goes here
Console.WriteLine("request handled in ConcreteHandler2");
}
else
{
// not handled here - pass on to next in the chain
if (successorHandler != null)
successorHandler.HandleRequest(request);
}
}
} class ConcreteHandler3 : Handler
{
override public void HandleRequest(Request request)
{
// determine if we can handle the request
if (request.RequestType == ) // some complex decision making!
{
// request handling code goes here
Console.WriteLine("request handled in ConcreteHandler3");
}
else
{
// not handled here - pass on to next in the chain
if (successorHandler != null)
successorHandler.HandleRequest(request);
}
}
} class Request
{
private int iRequestType;
private string strRequestParameters; public Request(int requestType, string requestParameters)
{
iRequestType = requestType;
strRequestParameters = requestParameters;
} public int RequestType
{
get
{
return iRequestType;
}
set
{
iRequestType = value;
}
}
} /// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
public static int Main(string[] args)
{
// Set up chain (usually one need to be done once)
Handler firstHandler = new ConcreteHandler1();
Handler secondHandler = new ConcreteHandler2();
Handler thirdHandler = new ConcreteHandler3();
firstHandler.SetSuccessor(secondHandler);
secondHandler.SetSuccessor(thirdHandler); // After setting up the chain of responsibility, we can
// now generate requests and pass then off to the
// chain to be handled // generate and fire request
Request newRequest = new Request(,"This are the request parameters");
firstHandler.HandleRequest(newRequest); return ;
}
}

职责链模式

最新文章

  1. vue-loader配合webpack的使用及安装
  2. SQL中的内连接与外连接
  3. PyCharm5.0.2最新版破解注册激活码
  4. node-webkit 笔记
  5. 【Oracle XE系列之四】创建OracleXE表空间详解
  6. javascript中的call()和apply应用
  7. 一则自用iptables例子解释
  8. 算法导论(第三版)Problems2(归并插入排序、数列逆序计算)
  9. python3之xml&amp;ConfigParser&amp;hashlib&amp;Subprocess&amp;logging模块
  10. 【关于Java移位操作符&amp;按位操作符】
  11. python numpy 间的的数据变算公式
  12. H5 66-清除浮动方式二
  13. 正则RegExp的懒惰性和贪婪性; 分组捕获;
  14. 基于Redis的INCR实现一个限流器
  15. EF 事务(非分布式事务)
  16. Levmar 配置
  17. 关于 Oracle DB CONSTRAINT约束的一些SQL ORA-02292: integrity constraint violated
  18. POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows
  19. 上传xslx文件设置accept的MIME 类型
  20. mybatis学习------打包xml映射文件

热门文章

  1. php性能优化 --- laravel 性能优化
  2. 数据分析处理库Pandas——merge操作
  3. Educational Codeforces Round 43 E. Well played!(贪心)
  4. python创建字典
  5. HyperLedger Fabric 1.4 区块链技术定义(2.1)
  6. urllib使用一
  7. PHP.TP框架下商品项目的优化3-php封装下拉框函数
  8. Windows系统搭建Mysql Cluster集群
  9. 常见排序算法题(java版)
  10. 为什么要内存对齐 Data alignment: Straighten up and fly right