责任链模式

抽象处理者

package ChainOfResponsibilityPattern.handler;

import ChainOfResponsibilityPattern.requester.Woman;

public abstract class Handler {
	
	private String handleType;

	private  Handler nextHandler;
	
	public Handler(String handleType) {
		this.handleType = handleType;
	}

	public void handle(Woman woman){
		if (woman.getType().equals(handleType)) {
			response(woman);
		}else {
			if (this.nextHandler != null) {
				nextHandler.handle(woman);
			}else {
				System.out.println("没人能处理这个请求...");
			}
			
		}
	}
	
	//对该请求的处理
	public abstract void response(Woman woman);

	public void setNextHandler(Handler nextHandler) {
		this.nextHandler = nextHandler;
	}
	
	
}

 

 

具体处理者

package ChainOfResponsibilityPattern.handler;

import ChainOfResponsibilityPattern.requester.Woman;

public class FatherHandler extends Handler{

	public FatherHandler() {
		super("1");
	}

	@Override
	public void response(Woman woman) {
		System.out.println("女儿向父亲请示:");
		System.out.println("女儿的请求是:"+woman.getQequest());
		System.out.println("父亲的回答是:同意");
		System.out.println();
	}

}

 

package ChainOfResponsibilityPattern.handler;

import ChainOfResponsibilityPattern.requester.Woman;

public class HusbandHandler extends Handler{

	public HusbandHandler() {
		super("2");
	}

	@Override
	public void response(Woman woman) {
		System.out.println("妻子向丈夫请示:");
		System.out.println("妻子的请求是:"+woman.getQequest());
		System.out.println("丈夫的回答是:同意");
		System.out.println();
		
	}

}

 

package ChainOfResponsibilityPattern.handler;

import ChainOfResponsibilityPattern.requester.Woman;

public class SonHandler extends Handler{

	public SonHandler() {
		super("3");
	}

	@Override
	public void response(Woman woman) {
		System.out.println("母亲向儿子请示:");
		System.out.println("母亲的请求是:"+woman.getQequest());
		System.out.println("儿子的回答是:同意");
		System.out.println();
		
	}

}

 

 

请求者

package ChainOfResponsibilityPattern.requester;

public interface Woman {
	
	public String shaonv = "1";
	
	public String shaofu = "2";
	
	public String guafu = "3";
	
	public String getQequest();
	
	public String getType();

}

 

package ChainOfResponsibilityPattern.requester;

public class WomanImpl implements Woman{
	
	public String type;
	
	private String request;
	
	public WomanImpl(String type,String request){
		this.type = type;
		this.request = request;
	}
	
	public String getType() {
		return type;
	}

	@Override
	public String getQequest() {
		return request;
	}
	
}

 

 

客户端

package ChainOfResponsibilityPattern;

import ChainOfResponsibilityPattern.handler.FatherHandler;
import ChainOfResponsibilityPattern.handler.Handler;
import ChainOfResponsibilityPattern.handler.HusbandHandler;
import ChainOfResponsibilityPattern.handler.SonHandler;
import ChainOfResponsibilityPattern.requester.Woman;
import ChainOfResponsibilityPattern.requester.WomanImpl;

public class Client {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Handler fatherHandler = new FatherHandler();
		Handler husbandHandler = new HusbandHandler();
		Handler sonHandler = new SonHandler();
		
		fatherHandler.setNextHandler(husbandHandler);
		husbandHandler.setNextHandler(sonHandler);
		
		Woman woman = new WomanImpl("3","我要逛街");
		
		fatherHandler.handle(woman);

	}

}

 

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