// Chain Of Responsibility
// Intent: "Avoid coupling the sender of a request to its receiver by giving
// more than one object a chance to handle the reuqest. Chain the receiving
// objects and pass the request along the chain until an object handles it."
// For further information, read "Design Patterns", p223, Gamma et al.,
// Addison-Wesley, ISBN:0-201-63361-2
/* Notes:
* The client sends a request and it will be operated on by one of a number
* of potential receivers, in a chain. The client does not know (and does
* not need to know) which receiver handles the request. The receivers are
* in a chain, and the request is passed from one to the next, until one
* receiver actually performs the request.
*/
namespace ChainOfResponsibility_DesignPattern
{
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 == 1) // 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 == 2) // 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 == 3) // 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(2,"This are the request parameters");
firstHandler.HandleRequest(newRequest);
return 0;
}
}
}
23个设计模式C#代码
需积分: 8 26 浏览量
2018-09-10
22:42:21
上传
评论
收藏 27KB ZIP 举报
qq_35688594
- 粉丝: 13
- 资源: 32
最新资源
- 三菱PLC(Mitsubishi)通讯协议的C#实现,支持FX、Q系列的ASCII-3E、BIN-3E、FX串口格式
- 五一建模20242024
- rainy-day.jpg
- IMG_20240501_171218.jpg
- Swift-内购封装swift版本
- 经典CNN网络之ResNet 图像分类网络实战项目:7种小麦叶片病害分类(迁移学习)
- Java毕设之ssm010基于ssm的新能源汽车在线租赁管理系统+vue.rar
- Java毕设之ssm009毕业生就业信息统计系统+vue.rar
- Java毕设之ssm008医院门诊挂号系统+jsp.rar
- Java毕设之ssm007亚盛汽车配件销售业绩管理统+jsp.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈