责任链模式(chain of responsibility)

Posted 每天一篇,坚持...

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了责任链模式(chain of responsibility)相关的知识,希望对你有一定的参考价值。

责任链模式定义:

  将能够处理同一类请求的对象连成一条链,所提及的请求沿着链传递,链上的对象逐个判断是否有能力处理该请求,如果能则处理,如果不能则传递给链上的下一个对象。

 

责任链属于行为型模式的一种。行为型模式关注系统中对象之间的相互交互,研究系统在运行时对象之间的相互通信和协作,进一步明确对象的职责。

下面使用公司请假的例子来说明:

 

 

  创建请假请求者

package com.note.pattern.responsibility;

public class  LeaveRequest {
	
	private String name;
	private int days;
    private String reason;
    
	public LeaveRequest(String name, int days, String reason) {
		super();
		this.name = name;
		this.days = days;
		this.reason = reason;
	}

	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;
	}


	public String getReason() {
		return reason;
	}


	public void setReason(String reason) {
		this.reason = reason;
	}
    
    
	
}

  创建Leader抽象类与不同级别的Leader作为实现子类,负责接收请假请求:

package com.note.pattern.responsibility;

public abstract class Leader {
	
	protected String name;
	protected Leader nextLeader; //责任链上的后继对象
	
	public Leader(String name) {
		this.name = name;
	}
	
	//设定责任链上的后继对象
	public void setNextLeader(Leader leader) {
		nextLeader = leader;
	}
	
	/**
     * 处理请求的核心的业务方法
     * @param request
     */
    public abstract void handleRequest(LeaveRequest request);
}


/***
 * 主任
 */
class Director extends Leader {

	public Director(String name) {
		super(name);
	}

	@Override
	public void handleRequest(LeaveRequest request) {
		if(request.getDays() < 3) {
			System.out.println("员工:"+request.getName()+"请假,天数:"+request.getDays()+",理由:"+request.getReason());
            System.out.println("主任:"+this.name+",审批通过!");
		}else {
			nextLeader.handleRequest(request);
		}
	}
}

/**
 * 经理
 */
class Manager extends Leader {

	public Manager(String name) {
		super(name);
	}

	@Override
	public void handleRequest(LeaveRequest request) {
		if(request.getDays() < 10) {
			System.out.println("员工:"+request.getName()+"请假,天数:"+request.getDays()+",理由:"+request.getReason());
            System.out.println("经理:"+this.name+",审批通过!");
		}else {
			nextLeader.handleRequest(request);
		}
	}
}


/**
 * 总经理
 */
class GeneralManager extends Leader {

	public GeneralManager(String name) {
		super(name);
	}

	@Override
	public void handleRequest(LeaveRequest request) {
		if(request.getDays() < 30) {
			System.out.println("员工:"+request.getName()+"请假,天数:"+request.getDays()+",理由:"+request.getReason());
            System.out.println("总经理:"+this.name+",审批通过!");
		}else {
			System.out.println("莫非"+request.getName()+"想辞职,居然请假"+request.getDays()+"天!");
		}
	}
}

  

package com.note.pattern.responsibility;

public class Client {

	public static void main(String[] args) {
		Leader a = new Director("张三");
		Leader b = new Manager("李四");
		a.setNextLeader(b);
		Leader c = new GeneralManager("王五");
		b.setNextLeader(c);
		
		 //开始请假操作
        LeaveRequest req1 = new LeaveRequest("TOM", 25, "回英国老家探亲!");
        a.handleRequest(req1);
	}

}

责任链模式在开发中的应用场景:   

  Java中异常机制就是一种责任链模式。一个try可以对应多个catch,当第一个catch不匹配类型,则自动跳到第二个catch;   
  javascript中事件的冒泡和捕获机制; 
  Servlet开发中过滤器的链式处理; 
  Struts2中拦截器的调用;

 

以上是关于责任链模式(chain of responsibility)的主要内容,如果未能解决你的问题,请参考以下文章

责任链模式(Chain of Responsibility)

15-责任链(Chain of Responsibility)模式Ruby实现

责任链模式(Chain of Responsibility Pattern)

责任链模式(chain of responsibility)

责任链模式(Chain of Responsibility)

设计模式(13)--Chain of Responsibility(责任链模式)--行为型