比较忙,只放代码吧。。。。
大概的意思是Client组装一个责任链条,每个环节处理一个事情,如果自己不能处理则转到下一个链条去。
package
org.benewu.patterns.chainOfResponsibility;
public class Client ... {
/** *//**
* @param args
*/
public static void main(String[] args) ...{
Request request = new RequestTwo("RequestTwo");
Handler handlerChainStart = new HandlerOne();
//组装下一个链条环节
Handler handlerChainTwo = new HandlerTwo();
handlerChainStart.setNextHandler(handlerChainTwo);
//链条开始
handlerChainStart.handle(request);
}
}
public class Client ... {
/** *//**
* @param args
*/
public static void main(String[] args) ...{
Request request = new RequestTwo("RequestTwo");
Handler handlerChainStart = new HandlerOne();
//组装下一个链条环节
Handler handlerChainTwo = new HandlerTwo();
handlerChainStart.setNextHandler(handlerChainTwo);
//链条开始
handlerChainStart.handle(request);
}
}
链条有一个公共接口Handler。
package
org.benewu.patterns.chainOfResponsibility;
public abstract class Handler ... {
private Handler nextHandler;
public abstract void handle(Request request );
public Handler getNextHandler() ...{
return nextHandler;
}
public void setNextHandler(Handler nextHandler) ...{
this.nextHandler = nextHandler;
}
}
public abstract class Handler ... {
private Handler nextHandler;
public abstract void handle(Request request );
public Handler getNextHandler() ...{
return nextHandler;
}
public void setNextHandler(Handler nextHandler) ...{
this.nextHandler = nextHandler;
}
}
下面只示范实现两个链条环节
package
org.benewu.patterns.chainOfResponsibility;
public class HandlerOne extends Handler ... {
@Override
public void handle(Request request) ...{
if (request instanceof RequestOne) ...{
RequestOne newRequest = (RequestOne) request;
System.out.println(newRequest.getRequestType());
}else...{
if(getNextHandler() != null)...{
getNextHandler().handle(request);
}else...{
System.out.println("Sorry,noBody resposnsibility it");
}
}
}
}
public class HandlerOne extends Handler ... {
@Override
public void handle(Request request) ...{
if (request instanceof RequestOne) ...{
RequestOne newRequest = (RequestOne) request;
System.out.println(newRequest.getRequestType());
}else...{
if(getNextHandler() != null)...{
getNextHandler().handle(request);
}else...{
System.out.println("Sorry,noBody resposnsibility it");
}
}
}
}
再来一个
package
org.benewu.patterns.chainOfResponsibility;
public class HandlerTwo extends Handler ... {
@Override
public void handle(Request request) ...{
if (request instanceof RequestTwo) ...{
RequestTwo newRequest = (RequestTwo) request;
System.out.println(newRequest.getRequestType());
}else...{
if(getNextHandler() != null)...{
getNextHandler().handle(request);
}else...{
System.out.println("Sorry,noBody resposnsibility it");
}
}
}
}
public class HandlerTwo extends Handler ... {
@Override
public void handle(Request request) ...{
if (request instanceof RequestTwo) ...{
RequestTwo newRequest = (RequestTwo) request;
System.out.println(newRequest.getRequestType());
}else...{
if(getNextHandler() != null)...{
getNextHandler().handle(request);
}else...{
System.out.println("Sorry,noBody resposnsibility it");
}
}
}
}
请求也有一个公共的类
package
org.benewu.patterns.chainOfResponsibility;
public abstract class Request ... {
private String requestType;
public String getRequestType() ...{
return requestType;
}
public void setRequestType(String requestType) ...{
this.requestType = requestType;
}
}
public abstract class Request ... {
private String requestType;
public String getRequestType() ...{
return requestType;
}
public void setRequestType(String requestType) ...{
this.requestType = requestType;
}
}
实现两个具体的请求
package
org.benewu.patterns.chainOfResponsibility;
public class RequestOne extends Request ... {
public RequestOne(String requestType)...{
super();
this.setRequestType(requestType);
}
}
public class RequestOne extends Request ... {
public RequestOne(String requestType)...{
super();
this.setRequestType(requestType);
}
}
package
org.benewu.patterns.chainOfResponsibility;
public class RequestTwo extends Request ... {
public RequestTwo(String requestType)...{
super();
this.setRequestType(requestType);
}
}
public class RequestTwo extends Request ... {
public RequestTwo(String requestType)...{
super();
this.setRequestType(requestType);
}
}