职责链模式 专门处理请求链式传递的模式 角色 Handler抽象处理者 ConcreteHandler具体处理者 在职责链模式中,很多对象由每一个对象对其下家的引用而连接成一条链,请求在这条链上传递,直到链上的某一个对象决定处理次请求。 具体处理类的作用。 1.处理请求,handleRequest。 2.转发请求,如果该请求超出了当前处理类的权限,可以转发给下家。
看例子,批复休假
1.休假请求类
package CoR;
/**
* Created by Jiqing on 2016/10/22.
*/
public class LeaveRequest // 休假请求
{
private String leaveName; // 休假人
private int leaveDays; // 休假天数
public LeaveRequest(String leaveName,int leaveDays)
{
this.leaveName=leaveName;
this.leaveDays=leaveDays;
}
public void setLeaveName(String leaveName) {
this.leaveName = leaveName;
}
public void setLeaveDays(int leaveDays) {
this.leaveDays = leaveDays;
}
public String getLeaveName() {
return (this.leaveName);
}
public int getLeaveDays() {
return (this.leaveDays);
}
}
2.抽象处理类
public abstract class Leader // 抽象处理者
{
protected String name;
protected Leader successor;
public Leader(String name)
{
this.name=name;
}
public void setSuccessor(Leader successor)
{ // 设置下一个处理者
this.successor=successor;
}
public abstract void handleRequest(LeaveRequest request); // 处理请求
}
3.主任
package CoR;
/**
* Created by Jiqing on 2016/10/22.
*/
public class Director extends Leader // 主任
{
public Director(String name)
{
super(name);
}
public void handleRequest(LeaveRequest request)
{
if(request.getLeaveDays()<3) // 小于三天主任审批
{
System.out.println("主任" + name + "审批员工" + request.getLeaveName() + "的请假条,请假天数为" + request.getLeaveDays() + "天。");
}
else
{
if(this.successor!=null)
{
this.successor.handleRequest(request);
}
}
}
}
4.经理
package CoR;
/**
* Created by Jiqing on 2016/10/22.
*/
public class Manager extends Leader // 经理审批
{
public Manager(String name)
{
super(name);
}
public void handleRequest(LeaveRequest request)
{
if(request.getLeaveDays()<10)
{
System.out.println("经理" + name + "审批员工" + request.getLeaveName() + "的请假条,请假天数为" + request.getLeaveDays() + "天。");
}
else
{
if(this.successor!=null)
{
this.successor.handleRequest(request);
}
}
}
}
5.副总经理
package CoR;
/**
* Created by Jiqing on 2016/10/22.
*/
public class ViceGeneralManager extends Leader // 副总经理
{
public ViceGeneralManager(String name)
{
super(name);
}
public void handleRequest(LeaveRequest request)
{
if(request.getLeaveDays()<20)
{
System.out.println("副总经理" + name + "审批员工" + request.getLeaveName() + "的请假条,请假天数为" + request.getLeaveDays() + "天。");
}
else
{
if(this.successor!=null)
{
this.successor.handleRequest(request);
}
}
}
}
6.总经理
package CoR;
/**
* Created by Jiqing on 2016/10/22.
*/
public class GeneralManager extends Leader // 总经理审批
{
public GeneralManager(String name)
{
super(name);
}
public void handleRequest(LeaveRequest request)
{
if(request.getLeaveDays()<30)
{
System.out.println("总经理" + name + "审批员工" + request.getLeaveName() + "的请假条,请假天数为" + request.getLeaveDays() + "天。");
}
else
{
System.out.println("莫非" + request.getLeaveName() + "想辞职,居然请假" + request.getLeaveDays() + "天。");
}
}
}
7.客户端
package CoR;
/**
* Created by Jiqing on 2016/10/22.
*/
public class Client
{
public static void main(String args[])
{
Leader objDirector,objManager,objGeneralManager,objViceGeneralManager;
objDirector=new Director("王明");
objManager=new Manager("赵强");
objGeneralManager=new GeneralManager("李波");
objViceGeneralManager=new ViceGeneralManager("肖红");
// 建立职责链
objDirector.setSuccessor(objManager); // 主任上级是经理
objManager.setSuccessor(objViceGeneralManager); // 经理上级是副经理
objViceGeneralManager.setSuccessor(objGeneralManager); // 副经理上级是总经理
LeaveRequest lr1=new LeaveRequest("张三",2);
objDirector.handleRequest(lr1);
LeaveRequest lr2=new LeaveRequest("李四",5);
objDirector.handleRequest(lr2);
LeaveRequest lr3=new LeaveRequest("王五",15);
objDirector.handleRequest(lr3);
LeaveRequest lr4=new LeaveRequest("赵六",25);
objDirector.handleRequest(lr4);
}
}
亮点就在职责链上。
看结果:
主任王明审批员工张三的请假条,请假天数为2天。
经理赵强审批员工李四的请假条,请假天数为5天。
副总经理肖红审批员工王五的请假条,请假天数为15天。
总经理李波审批员工赵六的请假条,请假天数为25天。
本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/5986568.html,如需转载请自行联系原作者