一、概念
职责链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象接受它为止。
二、类图及基本代码
//处理请示的接口
public abstract class Handler {
protected Handler successor;
//设置后继者
public void SetSuccessor(Handler successor){
this.successor=successor;
}
//处理请求
public abstract void HandleRequest(int request);
}
//具体处理者,处理它所负责的请求,可访问它的后继者,
//如果可处理该请求,就处理之,否则将该请求转发给它的后继者
//有权处理0~10
public class ConcreteHandler1 extends Handler{
@Override
public void HandleRequest(int request) {
if(request>=0&&request<10){
System.out.println(this.getClass().getName()+"处理请求"+request);
}else if(successor!=null){
successor.HandleRequest(request);
}
}
}
//有权处理10~20
public class ConcreteHandler2 extends Handler{
@Override
public void HandleRequest(int request) {
if(request>=10&&request<20){
System.out.println(this.getClass().getName()+"处理请求"+request);
}else if(successor!=null){
successor.HandleRequest(request);
}
}
}
//有权处理20~30以内
public class ConcreteHandler3 extends Handler{
@Override
public void HandleRequest(int request) {
if(request>=20&&request<30){
System.out.println(this.getClass().getName()+"处理"+request);
}else if (successor!=null) {
successor.HandleRequest(request);
}
}
}
//客户端
public class Client {
public static void main(String[] args) {
Handler handler1=new ConcreteHandler1();
Handler handler2=new ConcreteHandler2();
Handler handler3=new ConcreteHandler3();
handler1.SetSuccessor(handler2);
handler2.SetSuccessor(handler3);
int requests[]=new int[]{
2,5,14,22,18,3,27,20
};
for (int i : requests) {
handler1.HandleRequest(i);
}
}
}
三、实例之请假及涨工资
//管理者(抽象)
public abstract class Manager {
protected String name;
protected Manager superior;
public Manager(String name){
this.name=name;
}
//设置管理者的上级
public void SetSuperior(Manager superior){
this.superior=superior;
}
//处理请求
public abstract void RequestApplications(Request request);
}
//经理
public class CommonManager extends Manager{
public CommonManager(String name) {
super(name);
}
@Override
public void RequestApplications(Request request) {
if(request.getRequestType()=="请假"&&request.getNumber()<=2){
System.out.println(name+"批准"+request.getRequestType()+" 数量:"+request.getNumber());
}else if(superior!=null){
superior.RequestApplications(request);
}
}
}
//总经理
public class GeneralManager extends Manager{
public GeneralManager(String name) {
super(name);
}
@Override
public void RequestApplications(Request request) {
if(request.getRequestType()=="请假"){
System.out.println(name+"批准"+request.getRequestType()+" 数量:"+request.getNumber());
}else if(request.getRequestType()=="加薪"&&request.getNumber()<=500){
System.out.println(name+"批准"+request.getRequestType()+" 数量:"+request.getNumber());
}else if(request.getRequestType()=="加薪"&&request.getNumber()>500){
System.out.println(name+"不批准"+request.getRequestType()+" 数量:"+request.getNumber());
}
}
}
//总监
public class Majordomo extends Manager{
public Majordomo(String name) {
super(name);
}
@Override
public void RequestApplications(Request request) {
if(request.getRequestType()=="请假"&&request.getNumber()<=5){
System.out.println(name+"批准"+request.getRequestType()+" 数量:"+request.getNumber());
}else if(superior!=null){
superior.RequestApplications(request);
}
}
}
//申请
public class Request {
//申请类型
private String requestType;
//申请内容
private String requestContent;
//数量
private int number;
public String getRequestType() {
return requestType;
}
public void setRequestType(String requestType) {
this.requestType = requestType;
}
public String getRequestContent() {
return requestContent;
}
public void setRequestContent(String requestContent) {
this.requestContent = requestContent;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
/*
* 客户端的申请都是由经理a开始,具体由谁处理,客户端并不知道
*/
public class Client {
public static void main(String[] args) {
CommonManager a=new CommonManager("a");
CommonManager b=new CommonManager("b");
CommonManager c=new CommonManager("c");
a.SetSuperior(b);
b.SetSuperior(c);
Request request=new Request();
request.setRequestType("请假");
request.setRequestContent("小菜请假");
request.setNumber(1);
a.RequestApplications(request);
Request request2=new Request();
request2.setNumber(4);
request2.setRequestType("请假");
request2.setRequestContent("小菜请假");
a.RequestApplications(request2);
Request request3=new Request();
request3.setNumber(500);
request3.setRequestType("加薪");
request3.setRequestContent("小菜请求加薪");
a.RequestApplications(request3);
Request request4=new Request();
request4.setNumber(1000);
request4.setRequestType("加薪");
request4.setRequestContent("小菜请求加薪");
a.RequestApplications(request4);
}
}
四、总结
1、好处。降低系统耦合度,可以简化对象之间的相互连接,只需要保存一个指向其后继者的引用,并不需要保存它对所有的候选接受者的引用。增强了给对象指派职责的灵活性,可以通过对职责链的增加或修改来增加或改变处理一个请求的职责。
2、使用时机。当有多个对象可以处理同一个请求,但具体哪一个对象处理该请求由运行时刻自动确定,即不明确该请求的具体接受者。
The path and dream is up to you to weave.