菜鸟硕枫设计模式系列之14 职责链模式

职责链模式是保证一个事件一定能够被处理的一种模式。比如:一个coder去请假,temLeader批5天一下的,Manager批10天的,就这么简单。职责链模式是一种行为模式。

职责链模式类图:
                      菜鸟硕枫设计模式系列之14 职责链模式

说明:
Coder发出Something请求,经过TeamLeader->Manager构成的链,如果TeamLeader能处理,就返回结果,不能则有Manager处理。变化之处在于,当TeamLeader不在时(也可以是因为某种原因这里down掉了),则由链中其他环节去处理了,这样就保证了请求一定能被处理,增强了系统的健壮性。


具体代码:

package chainOfResponsibilityPattern;

public class Something {
	private String name;
	private int days;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getDays() {
		return days;
	}
	public void setDays(int days) {
		this.days = days;
	}
	
}

抽象人
package chainOfResponsibilityPattern;

public abstract class Person {

	private Something something;
	private boolean isHere;//上班标志位
	
	public abstract void request(Something something,Person toperson);
	public abstract void response(Person person);
	
	public Something getSomething() {
		return something;
	}
	public void setSomething(Something something) {
		this.something = something;
	}
	public boolean isHere() {
		return isHere;
	}
	public void setHere(boolean isHere) {
		this.isHere = isHere;
	}
	
}


manager:
package chainOfResponsibilityPattern;

public class Manager extends Person{

	@Override
	public void request(Something something, Person toperson) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void response(Person person) {
		if(person.getSomething().getDays()<10&&this.isHere()){
			System.out.println("Manager deal with it");
			System.out.println("Agree");
		}else{
			System.out.println("Disagree");
		}
	}

}


码农:
package chainOfResponsibilityPattern;

public class Coder extends Person{
	
	private Something something;
	private Person leader;
	
	public Person getLeader() {
		return leader;
	}

	public void setLeader(Person leader) {
		this.leader = leader;
	}
	
	public Something getSomething() {
		return something;
	}

	public void setSomething(Something something) {
		this.something = something;
	}

	@Override
	public void response(Person person) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void request(Something something,Person toPerson) {
		
		System.out.println("For "+something.getName()+" ask for a " +
				"leave of about "+something.getDays()+" days");
		
		leader.response(Coder.this);
	}

}


TeamLeader
package chainOfResponsibilityPattern;

public class TeamLeader extends Person{
	
	private Person leader;
	private boolean isHere;
	
	public Person getLeader() {
		return leader;
	}

	public void setLeader(Person leader) {
		this.leader = leader;
	}


	@Override
	public void response( Person person) {
		if(person.getSomething().getDays()<5 && this.isHere){
			System.out.println("Agree");
		}else{
			System.out.println("TeamLeader can not deal,send it to Manager");
			leader.response(person);
		}
		
	}

	@Override
	public void request(Something something, Person toperson) {

		
	}


}


测试类:
package chainOfResponsibilityPattern;

public class ChainOfResponsibilityTest {

	public static void main(String[] args){
		Something something = new Something();
		something.setDays(4);
		something.setName("nothing");
		
		Coder coder = new Coder();
		coder.setSomething(something);
		
		
		TeamLeader teamLeader = new TeamLeader(); 
		Person manager = new Manager();
		
		manager.setHere(true);
		teamLeader.setHere(true);
		teamLeader.setLeader(manager);
		coder.setLeader(teamLeader);
		
		coder.request(something, teamLeader);
		
		teamLeader.setHere(false);
		manager.setHere(true);
		coder.setSomething(something);
		coder.request(something, teamLeader);
		
		
	}
}


你可能感兴趣的:(设计模式)