作者:机加工N_918 | 来源:互联网 | 2023-10-14 12:21
什么是责任链模式
顾名思义,责任链就是将多个处理单元串成链路,是一种行为模式。例如我们的登录校验(用户判断 -> 账号密码判断 -> 验证码校验-> …)一系列的从处理逻辑进行了解耦,各自处理各自的业务,处理完成后交给后边的链路继续处理。
优缺点
优点
- 解耦,各自的业务分开各自处理
- 灵活,处理单元无需关心链路情况,并且非常方便的增删链路单元和改变处理顺序
缺点
- 链路过长会导致处理速度变慢
- 如果出现循环引用,则会导致系统崩溃
- 请求并不一定能够被责任链处理到(可能存在每个处理单元都无法进行数据处理)这种就属于系统需求设计bug了
应用场景
应用实战
我们就用登录拦截来举例。
这个是我们的抽象类
package com.chengfengfeng.design.chain;
public abstract class Handler {protected Handler next = null;public abstract void doHandler(Member member);public void next(Handler handler) {this.next = handler;}
}
package com.chengfengfeng.design.chain;
public class AuthHandler extends Handler {@Overridepublic void doHandler(Member member) {if (checkUsernameAndPwd(member.getUsername(),member.getPassword())) {System.out.println("用户名密码成功");if (next != null){next.doHandler(member);}}else {System.err.println("用户名或密码错误");}}private boolean checkUsernameAndPwd(String username, String pwd) {return "cff".equals(username) && "123".equals(pwd);}
}
package com.chengfengfeng.design.chain;
public class CodeHandler extends Handler {@Overridepublic void doHandler(Member member) {if (checkCode(member.getCode())) {System.out.println("验证码校验通过");if(next != null){next.doHandler(member);}}else {System.err.println("验证码校验失败");}}private boolean checkCode(String code){return true;}
}
package com.chengfengfeng.design.chain;
public class UserHandler extends Handler {@Overridepublic void doHandler(Member member) {if (checkUser(member.getUsername())) {System.out.println("用户存在,校验通过");if (next != null) {next.doHandler(member);}} else {System.err.println("用户不存在,请求非法");}}private boolean checkUser(String username) {return "cff".equals(username);}
}
package com.chengfengfeng.design.chain;import lombok.Data;
@Data
public class Member {public String username;public String password;public String code;
}
package com.chengfengfeng.design.chain;
public class Test {public static void main(String[] args) {Handler authHandler = new AuthHandler();Handler codeHandler = new CodeHandler();Handler userHandler = new UserHandler();userHandler.next(authHandler);authHandler.next(codeHandler);Member member = new Member();member.setCode("123");member.setUsername("cff");member.setPassword("123");userHandler.doHandler(member);}
}
执行下,看下结果
有的同学就会说了,当我们有七八个链路需要处理时,进行初始化很容易就弄错了,新建七八个对象就已经够烦了,还要设置next,能不能不要这么麻烦。当然有了,看下面。
进阶版 和建造者模式联用
改造我们的handler类,屌丝立即变高富帅。建造者单独会讲。
package com.chengfengfeng.design.chain;
public abstract class Handler {protected Handler next = null;public abstract void doHandler(Member member);public void next(Handler handler) {this.next = handler;}public static class Builder {private Handler header = null;private Handler tail = null;public Builder add(Handler handler) {if (this.header == null) {this.header = this.tail = handler;} else {tail.next = handler;tail = handler;}return this;}public Handler build() {return this.header;}}
}
看下我们的测试类
package com.chengfengfeng.design.chain;
public class Test {public static void main(String[] args) {Handler authHandler = new AuthHandler();Handler codeHandler = new CodeHandler();Handler userHandler = new UserHandler();Member member = new Member();member.setCode("123");member.setUsername("cff");member.setPassword("123");Handler.Builder builder = new Handler.Builder();builder.add(userHandler).add(authHandler).add(codeHandler);Handler handler = builder.build();handler.doHandler(member);}
}
这样写完的代码,巴适~~~~~。
此处应该有掌声👏👏
👉给个点赞关注👈