责任链模式是23种设计模式种的一种,是一种行为型模型,传统的责任链模式是有多个对象,每个对象持有另一个对象的引用,,这样就形成一条链,请求将会在这个链上传递,具体哪个对象将会调用事先并不会知道。
image.png
public interface Handler {
void operate();
}
public abstract class AbstractHandler {
private Handler handler;
public Handler getHandler() {
return handler;
}
public void setHandler(Handler handler) {
this.handler = handler;
}
}
public class MyHanlder extends AbstractHandler implements Handler {
private String name;
public MyHanlder(String name){
this.name = name;
}
@Override
public void operate() {
Log.e("info","----name:"+name);
if(null != getHandler()) {
getHandler().operate();
}
}
}
MyHanlder myHanlder1 = new MyHanlder("test1");
MyHanlder myHanlder2 = new MyHanlder("test2");
MyHanlder myHanlder3 = new MyHanlder("test3");
MyHanlder myHanlder4 = new MyHanlder("test4");
myHanlder1.setHandler(myHanlder2);
myHanlder2.setHandler(myHanlder3);
myHanlder3.setHandler(myHanlder4);
myHanlder1.operate();
还有一种变种的责任链模型,组合了其他模式,可以动态的调整请求的内容,将请求和返回都封装到责任链中,不断传递责任链给到不同的对象,直到有对象处理请求或者返回默认的结果。
image.png
public class Request {
private String name;
private String reason;
private int day;
private String groupInfo;
private String manageInfo;
private String customInfo;
private String departmentInfo;
public Request(Builder builder) {
this.name = builder.name;
this.reason = builder.reason;
this.day = builder.day;
this.groupInfo = builder.groupInfo;
this.manageInfo = builder.manageInfo;
this.customInfo = builder.customInfo;
this.departmentInfo = builder.departmentInfo;
}
public static class Builder {
private String name;
private String reason;
private int day;
private String groupInfo;
private String manageInfo;
private String customInfo;
private String departmentInfo;
public Builder() {
}
public Builder newRequst(Request request) {
this.name = request.name;
this.reason = request.reason;
this.day = request.day;
this.groupInfo = request.groupInfo;
this.manageInfo = request.manageInfo;
this.customInfo = request.customInfo;
this.departmentInfo = request.departmentInfo;
return this;
}
public Request builder() {
return new Request(this);
}
public Builder setName(String name) {
this.name = name;
return this;
}
public Builder setReason (String reason){
this.reason = reason;
return this;
}
public Builder setDay ( int day){
this.day = day;
return this;
}
public Builder setGroupInfo (String groupInfo){
this.groupInfo = groupInfo;
return this;
}
public Builder setManageInfo (String manageInfo){
this.manageInfo = manageInfo;
return this;
}
public Builder setCustomInfo (String customInfo){
this.customInfo = customInfo;
return this;
}
public Builder setDepartmentInfo (String departmentInfo){
this.departmentInfo = departmentInfo;
return this;
}
}
public String getName() {
return name;
}
public String getReason() {
return reason;
}
public int getDay() {
return day;
}
public String getGroupInfo() {
return groupInfo;
}
public String getManageInfo() {
return manageInfo;
}
public String getCustomInfo() {
return customInfo;
}
public String getDepartmentInfo() {
return departmentInfo;
}
@Override
public String toString() {
return "Request{" +
"name='" + name + '\'' +
", reason='" + reason + '\'' +
", day=" + day +
", groupInfo='" + groupInfo + '\'' +
", manageInfo='" + manageInfo + '\'' +
", customInfo='" + customInfo + '\'' +
", departmentInfo='" + departmentInfo + '\'' +
'}';
}
}
public class Response {
private boolean isArgee;
private String info;
public Response(boolean isArgee, String info) {
this.info = info;
this.isArgee = isArgee;
}
public boolean isArgee() {
return isArgee;
}
public void setArgee(boolean argee) {
isArgee = argee;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "Response{" +
"isArgee=" + isArgee +
", info='" + info + '\'' +
'}';
}
}
public interface Agree {
//处理请求
Response deal(Chain chain);
interface Chain{
Request request();
Response proceed(Request request);
}
}
public class RealChain implements Agree.Chain {
private Request request;
private List<Agree> list;
private int index;
public RealChain(Request request , List<Agree> l, int i ){
this.request = request;
this.list = l;
this.index = i;
}
@Override
public Request request() {
return request;
}
@Override
public Response proceed(Request request) {
Response procees =null;
if(list.size() >index){
RealChain groupChain = new RealChain(request,list,index+1);
Agree agree = list.get(index);
procees = agree.deal(groupChain);
}
return procees;
}
}
在deal 中修改了request请求
public class DepartmentLeader implements Agree {
@Override
public Response deal(Chain chain) {
Request request = chain.request();
Log.e("info","----"+request.toString());
if(request.getDay() >5){
Request request1 = new Request.Builder().newRequst(request).setDepartmentInfo("每月的KPI考核还不错,可以批准").builder();
return chain.proceed(request1);
}
return new Response(true,"Depatemne 不要着急,把事情处理完再回来") ;
}
}
public class GroupLeader implements Agree {
@Override
public Response deal(Chain chain) {
Request request = chain.request();
Log.e("info" ,request.toString());
if(request.getDay()>1){
Request newRequest = new Request.Builder().newRequst(request).setGroupInfo(request.getName()+"平时表现不错,而且现在项目也不忙").builder();
return chain.proceed(newRequest);
}
return new Response(true,"group : to早点把事情办完,项目离不开你");
}
}
public class ChainOfResponsibilityClient {
private List<Agree> list;
public ChainOfResponsibilityClient(){
this.list = new ArrayList<>();
}
public void add(List<Agree> list) {
this.list.addAll(list);
}
public Response excute(Request request){
ArrayList<Agree> list = new ArrayList<>();
list.addAll(list);
list.add(new GroupLeader());
list.add(new DepartmentLeader());
RealChain realChain = new RealChain(request,list,0);
return realChain.proceed(request);
}
}
最后的调用
Request request = new Request.Builder().setName("张山").setDay(5).setReason("我有事").builder();
ChainOfResponsibilityClient chainOfResponsibilityClient = new ChainOfResponsibilityClient();
Response response =chainOfResponsibilityClient.excute(request);
Log.e("info","response:"+response.toString());
网友评论