Patterns in SOME –Chain Of Responsibility
来源:互联网 发布:武汉java培训机构名单 编辑:程序博客网 时间:2024/05/01 21:51
Code in C#:
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;
}
}
}
Code in SOME:
AHandler ->AHandler[m_successorHandler]
a_HandleCRequest(CRequest )
SetSuccessor(AHandler m_successorHandler)
CConcreteHandler1 : AHandler
o_HandleCRequest(CRequest)
CConcreteHandler2 : AHandler
o_HandleCRequest(CRequest)
CConcreteHandler3 : AHandler
o_HandleCRequest(CRequest)
CRequest
(int _iCRequestType, string _strCRequestParameters) //include defination of 2 private members
CClient
main
CClient.main
{
// Set up chain (usually one need to be done once)
AHandler firstHandler<CConcreteHandler1>.();
AHandler secondHandler<CConcreteHandler2>.();
AHandler thirdHandler<CConcreteHandler3>.();
firstHandler.SetSuccessor(m_successorHandler = secondHandler);
secondHandler.SetSuccessor(m_successorHandler = thirdHandler);
// generate and fire CRequest
CRequest newCRequest.(2,"This are the CRequest parameters");
firstHandler.HandleCRequest<CConcreteHandler1>(CRequest[newCRequest]) //alias of newCRequest
{
<%
// determine if we can handle the CRequest
if (CRequest.CRequestType == 1) // some complex decision making!
{
// CRequest handling code goes here
Console.WriteLine("CRequest handled in ConcreteHandler1");
}
else
{
// not handled here - pass on to next in the chain
if (m_successorHandler != null)
%>
m_successorHandler.HandleCRequest<CConcreteHandler2>(CRequest)
{
<%
// determine if we can handle the CRequest
if (CRequest.CRequestType == 2) // some complex decision making!
{
// CRequest handling code goes here
Console.WriteLine("CRequest handled in ConcreteHandler2");
}
else
{
// not handled here - pass on to next in the chain
if (m_successorHandler != null)
%>
m_successorHandler.HandleCRequest<CConcreteHandler3>(CRequest)
{
<%
// determine if we can handle the CRequest
if (CRequest.CRequestType == 3) // some complex decision making!
{
// CRequest handling code goes here
Console.WriteLine("CRequest handled in ConcreteHandler3");
}
else
{
// not handled here - pass on to next in the chain
if (m_successorHandler != null)
m_successorHandler.HandleCRequest(CRequest);
}
%>
};
<%}%>
};
<%}%>
};
}
- Patterns in SOME –Chain Of Responsibility
- Design Patterns -- Chain of Responsibility (CoR)
- Chain of Responsibility(Gof-Behavioral Design Patterns)
- Design Patterns 入門 - 職責鏈(Chain of Responsibility) [精華]
- 大卫的Design Patterns学习笔记13:Chain of Responsibility
- Behavioral Patterns Part 1/11: Chain Of Responsibility Pattern
- Design Pattern - Behavioral Patterns - Chain of Responsibility Pattern
- 任链(chain of Responsibility) in Java
- Patterns in SOME –State
- Patterns in SOME –Observer
- Patterns in SOME –Memento
- Patterns in SOME –Mediator
- Patterns in SOME –Iterator
- Patterns in SOME – Command
- Patterns in SOME –Interpreter
- Patterns in SOME –Proxy
- Patterns in SOME –Flyweight
- Patterns in SOME –Facade
- 玩转brew菜单
- Sqlserver MD5 加密
- newxy标签实现访问控制
- 关于对栈溢出的分析
- 探讨两道.Net关于字符串的上机题
- Patterns in SOME –Chain Of Responsibility
- 工作流(WfMC ) 术语解释一------------基本概念
- 单向收费的背后:飞信飞入寻常百姓家
- 今年要把这个blog写下去。
- [精]采用dwr+ajax和struts开发文件上传进度条
- DataGrid删除记录弹出对话框的解决办法
- Patterns in SOME –Template Method
- Oracle数据直接导出到文本文件的方法()
- Patterns in SOME –Interpreter