设计模式之责任链模式

main
===================================================================================
package com.handler.sym;

public class Testhandler {

	/**
	 * @param args
	 * 
               抽象处理类:抽象处理类中主要包含一个指向下一处理类的成员变量nextHandler和一个处理请求的方法handRequest,handRequest方法的主要主要思想是,
               如果满足处理的条件,则有本处理类来进行处理,否则由nextHandler来处理。
              具体处理类:具体处理类主要是对具体的处理逻辑和处理的适用条件进行实现。
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Handler handler1 = new ConcreteHandler1();  
        Handler handler2 = new ConcreteHandler2();  
        Handler handler3 = new ConcreteHandler3();  
        handler1.setNextHandler(handler2);  
        handler2.setNextHandler(handler3);  
        Response response = handler1.handleRequest(new Request(new Level(4)));  
	}

}
请求与响应
=================================================================================
package com.handler.sym;

public class Level {
	private int level = 0;

	public Level(int level) {
		this.level = level;
	}
	public boolean above(Level level){
		if(this.level>=level.level){
			return true;
		}else {
			return false;
		}
	}
}

package com.handler.sym;

public class Request {
	Level level;

	public Request(Level level) {
		this.level = level;
	}

	public Level getLevel() {
		return level;
	}
}


package com.handler.sym;

public class Response {

}

处理抽象类
==============================================================================
package com.handler.sym;

public abstract class Handler {
private Handler nextHandler;
public final Response handleRequest(Request request){
	Response response=null;
	if (this.getHandlerLevel().above(request.getLevel())) {
		response=this.response(request);
	}else if(this.nextHandler!=null){
		this.nextHandler.handleRequest(request);
	}else {
		System.out.print("没有合适的handler");
	}
	return response;
	
}
public void setNextHandler(Handler handler){  
    this.nextHandler = handler;  
}  
protected abstract Level getHandlerLevel();  
public abstract Response response(Request request);  
}

处理具体类1
===============================================================================
package com.handler.sym;

public class ConcreteHandler1 extends Handler {
	protected Level getHandlerLevel() {  
        return new Level(1);  
    }  
    public Response response(Request request) {  
        System.out.println("-----请求由处理器1进行处理-----");  
        return null;  
    }  
}

处理具体类2
===============================================================================
package com.handler.sym;

public class ConcreteHandler2 extends Handler {

	@Override
	protected Level getHandlerLevel() {
		// TODO Auto-generated method stub
		return new Level(3);  
	}

	@Override
	public Response response(Request request) {
		 System.out.println("-----请求由处理器2进行处理-----");  
		return null;
	}

}
处理具体类3
===============================================================================
package com.handler.sym;

public class ConcreteHandler3 extends Handler {

	@Override
	protected Level getHandlerLevel() {
		// TODO Auto-generated method stub
		return new Level(5);  
	}

	@Override
	public Response response(Request request) {
		 System.out.println("-----请求由处理器3进行处理-----");  
		return null;
	}

}


你可能感兴趣的:(责任链模式)