行为型设计模式之职责链模式(Chain of Responsibility)
2024-10-21 09:57:14
结构 | |
意图 | 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。 |
适用性 |
|
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 ;
}
}
职责链模式
最新文章
- vue-loader配合webpack的使用及安装
- SQL中的内连接与外连接
- PyCharm5.0.2最新版破解注册激活码
- node-webkit 笔记
- 【Oracle XE系列之四】创建OracleXE表空间详解
- javascript中的call()和apply应用
- 一则自用iptables例子解释
- 算法导论(第三版)Problems2(归并插入排序、数列逆序计算)
- python3之xml&;ConfigParser&;hashlib&;Subprocess&;logging模块
- 【关于Java移位操作符&;按位操作符】
- python numpy 间的的数据变算公式
- H5 66-清除浮动方式二
- 正则RegExp的懒惰性和贪婪性; 分组捕获;
- 基于Redis的INCR实现一个限流器
- EF 事务(非分布式事务)
- Levmar 配置
- 关于 Oracle DB CONSTRAINT约束的一些SQL ORA-02292: integrity constraint violated
- POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows
- 上传xslx文件设置accept的MIME 类型
- mybatis学习------打包xml映射文件
热门文章
- php性能优化 --- laravel 性能优化
- 数据分析处理库Pandas——merge操作
- Educational Codeforces Round 43 E. Well played!(贪心)
- python创建字典
- HyperLedger Fabric 1.4 区块链技术定义(2.1)
- urllib使用一
- PHP.TP框架下商品项目的优化3-php封装下拉框函数
- Windows系统搭建Mysql Cluster集群
- 常见排序算法题(java版)
- 为什么要内存对齐 Data alignment: Straighten up and fly right