责任链模式

类图

代码

Handler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
abstract class Handler{

private Handler next;

Handler getNext() {
return next;
}

void setNext(Handler next) {
this.next = next;
}

public abstract boolean handle(String name, double price);
}

ConcreteHandler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class ProjectHandler extends Handler{

@Override
public boolean handle(String name, double price) {
if(price <= 500){
if(name.startsWith("王")){
return true;
}
return false;
}

if(this.getNext() != null){
return this.getNext().handle(name, price);
}

return false;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class DeptHandler extends Handler{

@Override
public boolean handle(String name, double price) {
if(price <= 5000){
if(name.startsWith("王")){
return true;
}
return false;
}
if(this.getNext() != null){
return this.getNext().handle(name, price);
}

return false;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class CompanyHandler extends Handler{

@Override
public boolean handle(String name, double price) {
if(price <= 50000){
if(name.startsWith("王")){
return true;
}
return false;
}
if(this.getNext() != null){
return this.getNext().handle(name, price);
}

return false;
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Main {
public static void main(String args[]){

Handler root = new ProjectHandler();

Handler deptHandler = new DeptHandler();

Handler companyHandler = new CompanyHandler();

deptHandler.setNext(companyHandler);
root.setNext(deptHandler);

System.out.println(root.handle("王", 50000));
}
}

Pipeline模式

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
interface Valve {
Valve getNext();
void setNext(Valve valve);
void invoke(String handling);
}

interface Pipeline {
Valve getFirst();
Valve getBasic();
void setBasic(Valve valve);
void addValve(Valve valve);
}

class BasicValve implements Valve {
protected Valve next = null;

public Valve getNext() {
return next;
}

@Override
public void setNext(Valve valve) {
this.next = valve;
}

@Override
public void invoke(String handling) {
handling=handling.replaceAll("aa", "bb");
System.out.println("基础阀门处理完后:" + handling);
}
}

class SecondValve implements Valve {
protected Valve next = null;
public Valve getNext() {
return next;
}
public void invoke(String handling) {
handling = handling.replaceAll("11", "22");
System.out.println("Second阀门处理完后:" + handling);
getNext().invoke(handling);
}
public void setNext(Valve valve) {
this.next = valve;
}
}

class ThirdValve implements Valve {
protected Valve next = null;
public Valve getNext() {
return next;
}
public void invoke(String handling) {
handling = handling.replaceAll("zz", "yy");
System.out.println("Third阀门处理完后:" + handling);
getNext().invoke(handling);
}
public void setNext(Valve valve) {
this.next = valve;
}
}

class StandardPipeline implements Pipeline {
protected Valve first = null;
protected Valve basic = null;
public void addValve(Valve valve) {
if (first == null) {
first = valve;
valve.setNext(basic);
} else {
Valve current = first;
while (current != null) {
if (current.getNext() == basic) {
current.setNext(valve);
valve.setNext(basic);
break;
}
current = current.getNext();
}
}
}
public Valve getBasic() {
return basic;
}
public Valve getFirst() {
return first;
}
public void setBasic(Valve valve) {
this.basic = valve;
}
}

测试类

1
2
3
4
5
6
7
8
9
10
public class Main {
public static void main(String args[]){
Pipeline pipeline = new StandardPipeline();
String handling = "aabb1122zzyy";
pipeline.setBasic(new BasicValve());
pipeline.addValve(new SecondValve());
pipeline.addValve(new ThirdValve());
pipeline.getFirst().invoke(handling);
}
}

总结

概述

文中例子为公司审批流程的简化版,项目组审批权限为500以下,部门审批权限为5000以下,公司领导审批权限为50000以下,名字为王带头的会过,其他都不过

文中还有个例子是Pipeline模式,Pipeline可以说是tomcat的核心,它实际上也是一种变种的责任链模式,只是对整个责任链进行了包装(Pipeline类),它负责整个链表的维护工作,然后basic节点进行兜底,防止出现NPE

优点

实现了请求者和处理者之间的代码分离

缺点

链表的结构导致了悲观情况下,遍历到链尾才能找到对应的处理器