Chain 责任链模式
目录
[TOC]
1. 概述
1.1. 设计模式定义
职责链模式(Chain of Responsibility):使多个对象都有机会处理同一个请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
1.2. 自我理解
责任链又叫做功能链,个人觉得有两种场景非常适合用:
1、流水线:层层配合,达到最后效果;每个人都要完成自己的工作,最后才能生产出产品;
链中的每个对象都要按照顺序处理,直到最后一个对象处理完毕。
2、责任划分:请假事宜,2天以内找组长,2~5天找经理,5~10天找总监,10~20天找副总….
链中的每个对象扮演处理器,并且有一个后继对象。如果它可以处理请求,就进行处理;否则把请求转发给后继者。
1.3. 责任链模式的组成
抽象链 (AbstractChain):抽象类,抽象链路的下级引用,抽象执行方法。
具体链 (ConcreteChain):抽项链的子类,具体链路的下级引用,具体执行方法,执行下个方法。
2. UML
3. Example
3.1. 场景
用户检验,一个用户登录需要通过层层检查,用户名、密码、权限等。该例子属于流水线式的责任链,所有具体链路全部执行完毕才能返回一个校验完成的用户。
这只是责任链中很简单的一个应用,有些银行的校验链路可以达到100多层。这么多层的校验,如果全部用 if else 去实现,后面的维护人员简直崩溃了。
3.2. 代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
/** * Created * Date: 2021/07/27 00:34 <br/> * Description: 责任链模式 * * @author jomin@loserzhao.com * @see */ public class ChainPatternDemo { public static void main(String[] args) { // 初始化责任链 UserNameCheckChain -> PasswordCheckChain -> PermissionCheckChain PermissionCheckChain permissionCheckChain = new PermissionCheckChain(null); PasswordCheckChain passwordCheckChain = new PasswordCheckChain(permissionCheckChain); UserNameCheckChain userCheckChain = new UserNameCheckChain(passwordCheckChain); User userA = new User(null, null, null); User userB = new User("123", null, null); User userC = new User("123", "123", null); User userD = new User("123", "123", "123"); ResponseEnum responseAEnum = userCheckChain.check(userA); System.out.println("user A, status => [" + responseAEnum.getStatus() + "], message => [" + responseAEnum.getMessage() + "]"); ResponseEnum responseBEnum = userCheckChain.check(userB); System.out.println("user B, status => [" + responseBEnum.getStatus() + "], message => [" + responseBEnum.getMessage() + "]"); ResponseEnum responseCEnum = userCheckChain.check(userC); System.out.println("user C, status => [" + responseCEnum.getStatus() + "], message => [" + responseCEnum.getMessage() + "]"); ResponseEnum responseDEnum = userCheckChain.check(userD); System.out.println("user D, status => [" + responseDEnum.getStatus() + "], message => [" + responseDEnum.getMessage() + "]"); } public static abstract class UserCheckChain { protected UserCheckChain successor; protected UserCheckChain(UserCheckChain successor) { this.successor = successor; } protected abstract ResponseEnum check(User user, ResponseEnum responseEnum); protected ResponseEnum execute(User user, ResponseEnum responseEnum) { ResponseEnum response; // 如果正常,执行下一个规则 if (responseEnum.getStatus().equals(ResponseEnum.NORMAL.getStatus()) && null != successor) { response = successor.check(user, responseEnum); }else { response = responseEnum; } return response; } public ResponseEnum check(User user){ return check(user, ResponseEnum.NORMAL); } } public static class UserNameCheckChain extends UserCheckChain { public UserNameCheckChain(UserCheckChain userCheckChain){ super(userCheckChain); } @Override protected ResponseEnum check(User user, ResponseEnum responseEnum) { if (null == user || StringUtils.isEmpty(user.getUserName())) { responseEnum = ResponseEnum.USERNAME_NULL; } return execute(user, responseEnum); } } public static class PasswordCheckChain extends UserCheckChain { public PasswordCheckChain(UserCheckChain userCheckChain){ super(userCheckChain); } @Override protected ResponseEnum check(User user, ResponseEnum responseEnum) { if (null == user || StringUtils.isEmpty(user.getPassword())) { responseEnum = ResponseEnum.PASSWORD_NULL; } return execute(user, responseEnum); } } public static class PermissionCheckChain extends UserCheckChain { public PermissionCheckChain(UserCheckChain userCheckChain){ super(userCheckChain); } @Override protected ResponseEnum check(User user, ResponseEnum responseEnum) { if (null == user || StringUtils.isEmpty(user.getPermission())) { responseEnum = ResponseEnum.NO_PERMISSION; } return execute(user, responseEnum); } } public static enum ResponseEnum { NORMAL(0, "正常"), USERNAME_NULL(1, "用户名不能为空"), PASSWORD_NULL(2, "密码不能为空"), NO_PERMISSION(3, "权限不足"); private Integer status; private String message; ResponseEnum(Integer status, String message) { this.status = status; this.message = message; } public Integer getStatus() { return status; } public String getMessage() { return message; } } public static class User { private String userName; private String password; private String permission; public User() { } public User(String userName, String password, String permission) { this.userName = userName; this.password = password; this.permission = permission; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPermission() { return permission; } public void setPermission(String permission) { this.permission = permission; } } } |
原创文章,转载请注明: 转载自LoserZhao – 诗和远方[ http://www.loserzhao.com/ ]
本文链接地址: http://www.loserzhao.com/java/designpattern/chain-designpattern.html
文章的脚注信息由WordPress的wp-posturl插件自动生成
0 条评论。