处理链模式二原创
# 背景
假如项目中有多条过滤链处理线,那么如何设计一套多处理链的代码呢? 假如有以下流程
- 买车流程
- 登陆流程
# 创建 Chain 接口
package com.design.chain;
/**
/**
* @program: spring-test
* @ClassName Handler
* @description: 责任链处理器接口
* * 1. 参数校验 2. 用户名密码校验 3. 权限校验
* @author: sizegang
* @create: 2022-01-01 00:40
* @Version 1.0
**/
public interface Chain {
void execute(Member member);
void doExecute(Member member);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 登陆处理链
# 会员辅助类
@Data
public class Member {
private String userName;
private String passWord;
private String roleName;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 登陆链接口
/**
* @program: spring-test
* @ClassName LoginChain
* @description: 登录责任链
* @author: sizegang
* @create: 2022-01-01 13:24
* @Version 1.0
**/
public interface LoginChain extends Chain {
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 抽象责任链
/**
* @program: spring-test
* @ClassName AbstractHandler
* @description: 抽象责任链处理器
* * 1. 参数校验 2. 用户名密码校验 3. 权限校验
* @author: sizegang
* @create: 2022-01-01 00:41
* @Version 1.0
**/
public abstract class AbstractLoginExecutor<T> implements LoginChain {
protected Chain chain;
public void next(Chain chain) {
this.chain = chain;
}
// 这里结合模板方法
@Override
public void execute(Member member) {
doExecute(member);
}
/**
* 责任链模式配合建造者模式
*
* @param <T>
*/
public static class Builder<T> {
private AbstractLoginExecutor<T> head;
private AbstractLoginExecutor<T> tail;
public Builder<T> addChain(AbstractLoginExecutor<T> handler) {
if (this.head == null) {
this.head = this.tail = handler;
return this;
}
// 设置自身的下一个节点
this.tail.next(handler);
// 修改指针指向
this.tail = handler;
return this;
}
public AbstractLoginExecutor<T> build() {
return this.head;
}
}
}
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
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
# 验证责任链
@Slf4j
public class ValidateExecutor extends AbstractLoginExecutor {
@Override
public void doExecute(Member member) {
if(StringUtils.isBlank(member.getUserName()) || StringUtils.isBlank(member.getPassWord())){
System.out.println("用户名密码为空");
return;
}
System.out.println("用户名密码校验成功,可以往下执行");
chain.doExecute(member);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
### 权限责任链
```java
/**
* @program: spring-test
* @ClassName AuthHandler
* @description: 权限校验责任链执行器
* @author: sizegang
* @Version 1.0
**/
public class AuthHandler extends AbstractLoginExecutor {
@Override
public void doExecute(Member member) {
if(!"管理员".equals(member.getRoleName())){
System.out.println("您不是管理员没有操作权限");
return;
}
System.out.println("您是管理员允许操作");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 登陆责任链
public class LoginExecutor extends AbstractLoginExecutor {
@Override
public void doExecute(Member member) {
System.out.println("登录成功");
member.setRoleName("管理员");
chain.doExecute(member);
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 用户登陆服务
public class LoginAssembler {
/**
* 会员等六
* @param member 会员
*/
public void login(Member member){
AbstractLoginExecutor.Builder builder = new AbstractLoginExecutor.Builder();
AbstractLoginExecutor build = builder.addChain(new ValidateExecutor()).addChain(new LoginExecutor()).addChain(new AuthHandler()).build();
build.execute(member);
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 测试
public class TestLoginChain {
public static void main(String[] args) {
LoginAssembler memberService = new LoginAssembler();
Member member = new Member();
member.setPassWord("1234");
member.setUserName("sizegang");
memberService.login(member);
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 买车流程
# 买车接口
public interface BuyCarChain extends Chain {
}
1
2
2
# 抽象买车流程
@Slf4j
public abstract class AbstractBuyCarChain<T> implements BuyCarChain {
protected Chain chain;
public void next(Chain chain) {
this.chain = chain;
}
/**
* 买车处理器
*
* @param member
*/
@Override
public void execute(Member member) {
doExecute(member);
}
public static class Build<T> {
private AbstractBuyCarChain head;
private AbstractBuyCarChain tail;
public Build<T> addChain(AbstractBuyCarChain chain) {
if (this.head == null) {
this.head = this.tail = chain;
return this;
}
this.tail.next(chain);
this.tail = chain;
return this;
}
public AbstractBuyCarChain build() {
return this.head;
}
}
}
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
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
# 看车责任链
@Slf4j
public class SeeCarHandler extends AbstractBuyCarChain{
@Override
public void doExecute(Member member) {
System.out.println("会员开始看车");
if(chain != null){
chain.doExecute(member);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 付款责任链
public class PayMoneyHandler extends AbstractBuyCarChain{
@Override
public void doExecute(Member member) {
System.out.println("付款成功");
if(chain != null){
chain.doExecute(member);
}
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 提车责任链
public class PickCarHandler extends AbstractBuyCarChain{
@Override
public void doExecute(Member member) {
System.out.println(String.format("%s 成功提车", member.getUserName()));
}
}
1
2
3
4
5
6
2
3
4
5
6
# 责任链触发器
public class BuyCarAssembler {
public void buyCar(Member member ){
AbstractBuyCarChain.Build builder = new AbstractBuyCarChain.Build<>();
AbstractBuyCarChain chain = builder.addChain(new SeeCarHandler()).addChain(new PayMoneyHandler()).addChain(new
PickCarHandler()).build();
chain.execute(member);
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 测试
public class TestBuyCar {
public static void main(String[] args) {
BuyCarAssembler buyCarAssembler = new BuyCarAssembler();
Member member = new Member();
member.setUserName("司泽刚");
member.setRoleName("研发");
buyCarAssembler.buyCar(member);
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
编辑 (opens new window)
上次更新: 2022/05/01, 19:42:49