定义:避免请求的发送者和接收者耦合在一起,让多个对象都有可能接收请求,将这些对象连接成一条链,沿着这条链传递请求,直到有对象处理它为止。
角色:
- 抽象处理者:Handler
- 具体处理者:ConcreteHandler
- 客户类:Client
模式分析:
关键在于抽象处理者类的设计:很多对象由每一个对象对其下家的引用而连接在一起。
抽象处理者典型代码:
//审批者类:抽象处理者
abstract class Approver {
protected Approver successor; //定义后继对象
protected String name; //审批者姓名
public Approver(String name) {
this.name = name;
}
//设置后继者
public void setSuccessor(Approver successor) {
this.successor = successor;
}
//抽象请求处理方法
public abstract void processRequest(PurchaseRequest request);
}
具体处理者典型代码:
//董事长类:具体处理者
class ViceManager extends Approver {
public ViceManager(String name) {
super(name);
}
//具体请求处理方法
public void processRequest(PurchaseRequest request) {
if (request.getAmount() < 100000) {
System.out.println("副总经理" + this.name + "审批采购单:" + request.getNumber() + ",金额:" + request.getAmount() + "元,采购目的:" + request.getPurpose() + "。"); //处理请求
}
else {
this.successor.processRequest(request); //转发请求
}
}
}
客户端调用典型代码:轮流设置下家
position1.setSuccessor(position2);
position2.setSuccessor(position3);
position3.setSuccessor(position4);
position4.setSuccessor(meeting);
package com.c015;
public class Client {
public static void main(String[] args) {
Approver position1,position2,position3,position4,meeting; // 多个处理者
position1 = new Director("甲");
position2 = new PartManager("乙");
position3 = new ViceManager("丙");
position4 = new Manager("丁");
meeting = new Congress("职工大会");
//创建职责链
position1.setSuccessor(position2);
position2.setSuccessor(position3);
position3.setSuccessor(position4);
position4.setSuccessor(meeting);
//创建采购单
PurchaseRequest pr1 = new PurchaseRequest(5000,10001,"XXX");
position1.processRequest(pr1);
PurchaseRequest pr2 = new PurchaseRequest(45000,10002,"XXX");
position1.processRequest(pr2);
PurchaseRequest pr3 = new PurchaseRequest(77000,10003,"XXX");
position1.processRequest(pr3);
PurchaseRequest pr4 = new PurchaseRequest(150000,10004,"XXX");
position1.processRequest(pr4);
PurchaseRequest pr5 = new PurchaseRequest(800000,10005,"XXX");
position1.processRequest(pr5);
}
}
核心理解
职责链模式关键在于设置职责的下家!
抽象处理者类要有一个自身的对象作为成员属性变量,并通过一个set方法完成赋值,之后要提供一个具体处理的方法接口供子类重写!
后续的子类重写具体的处理办法,如果处理不了,再次调用父类的处理方法直接把请求交给下家来完成!