📄 client.java
字号:
/*
* Created on 2005-5-11
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.test.pattern.behavir.chain;
/**
* @author Administrator
*
* 意图 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
* 适用性 有多个的对象可以处理一个请求,哪个对象处理该请求运行时刻自动确定。 你想在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。
* 可处理一个请求的对象集合应被动态指定。
*
*/
abstract class Handler {
protected Handler successorHandler;
abstract public void HandleRequest(Request request);
public void SetSuccessor(Handler sucessor) {
successorHandler = sucessor;
}
}
class ConcreteHandler1 extends Handler {
public void HandleRequest(Request request) {
// determine if we can handle the request
if (request.getRequestType() == 1) // some complex decision making!
{
// request handling code goes here
System.out.println("request handled in ConcreteHandler1");
} else {
// not handled here - pass on to next in the chain
if (successorHandler != null)
successorHandler.HandleRequest(request);
}
}
}
class ConcreteHandler2 extends Handler {
public void HandleRequest(Request request) {
// determine if we can handle the request
if (request.getRequestType() == 2) // some complex decision making!
{
// request handling code goes here
System.out.println("request handled in ConcreteHandler2");
} else {
// not handled here - pass on to next in the chain
if (successorHandler != null)
successorHandler.HandleRequest(request);
}
}
}
class ConcreteHandler3 extends Handler {
public void HandleRequest(Request request) {
// determine if we can handle the request
if (request.getRequestType() == 3) // some complex decision making!
{
// request handling code goes here
System.out.println("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 getRequestType() {
return iRequestType;
}
public void setRequestType(int value) {
iRequestType = value;
}
}
/// <summary>
/// Summary description for Client.
/// </summary>
public class Client {
public static void 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);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -