【图解设计模式】Chain of Responsibility模式

将多个对象组成一条职责链,然后按照它们在职责链上的顺序一个一个地找出到底应该谁来负责处理。

示例

当一个人被要求做什么事情时,如果他可以做就自己做,如果不能做就将“要求”转给另外一个人。下一个人如果可以自己处理,就自己做;如果也不能自己处理,就再转给另外一个人…

类图

eLOc1e.png

时序图

eLOf0I.png

Trouble类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Trouble {
private int number;

public Trouble(int number) {
this.number = number;
}

public int getNumber() {
return number;
}

@Override
public String toString() {
return "[Trouble " + number + "]";
}
}

Support类

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
public abstract class Support {
private String name;
private Support next;

public Support(String name) {
this.name = name;
}

public Support setNext(Support next) {
this.next = next;
return next;
}

@Override
public String toString() {
return "[" + name + "]";
}

protected abstract boolean resolve(Trouble trouble);

protected void done(Trouble trouble) {
System.out.println(trouble + " is resolved by " + this + ".");
}

protected void fail(Trouble trouble) {
System.out.println(trouble + " cannot be resolved.");
}

public final void support(Trouble trouble) {
if (resolve(trouble))
done(trouble);
else if (next != null)
next.support(trouble);
else
fail(trouble);
}
}

NoSupport类

1
2
3
4
5
6
7
8
9
10
public class NoSupport extends Support {
public NoSupport(String name) {
super(name);
}

@Override
protected boolean resolve(Trouble trouble) {
return false;
}
}

LimitSupport类

1
2
3
4
5
6
7
8
9
10
11
12
13
public class LimitSupport extends Support {
private int limit;

public LimitSupport(String name, int limit) {
super(name);
this.limit = limit;
}

@Override
protected boolean resolve(Trouble trouble) {
return trouble.getNumber() < limit;
}
}

OddSupport类

1
2
3
4
5
6
7
8
9
10
public class OddSupport extends Support {
public OddSupport(String name) {
super(name);
}

@Override
protected boolean resolve(Trouble trouble) {
return trouble.getNumber() % 2 == 1;
}
}

SpecialSupport类

1
2
3
4
5
6
7
8
9
10
11
12
13
public class SpecialSupport extends Support {
private int number;

public SpecialSupport(String name, int number) {
super(name);
this.number = number;
}

@Override
protected boolean resolve(Trouble trouble) {
return trouble.getNumber() == number;
}
}

Main类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Main {

public static void main(String[] args) {
Support alice = new NoSupport("Alice");
Support bob = new LimitSupport("Bob", 100);
Support charlie = new SpecialSupport("Charlie", 429);
Support diana = new LimitSupport("Diana", 200);
Support elmo = new OddSupport("Elmo");
Support fred = new LimitSupport("Fred", 300);

alice.setNext(bob).setNext(charlie).setNext(diana).setNext(elmo).setNext(fred);

for (int i = 0; i < 500; i += 33)
alice.support(new Trouble(i));
}
}

运行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Trouble 0] is resolved by [Bob].
[Trouble 33] is resolved by [Bob].
[Trouble 66] is resolved by [Bob].
[Trouble 99] is resolved by [Bob].
[Trouble 132] is resolved by [Diana].
[Trouble 165] is resolved by [Diana].
[Trouble 198] is resolved by [Diana].
[Trouble 231] is resolved by [Elmo].
[Trouble 264] is resolved by [Fred].
[Trouble 297] is resolved by [Elmo].
[Trouble 330] cannot be resolved.
[Trouble 363] is resolved by [Elmo].
[Trouble 396] cannot be resolved.
[Trouble 429] is resolved by [Charlie].
[Trouble 462] cannot be resolved.
[Trouble 495] is resolved by [Elmo].

登场角色

Handler(处理者)

Handler角色定义了处理请求的接口(API)。Handler角色知道“下一个处理者”是谁,如果自己无法处理请求,它会将请求转给“下一个处理者”。当然,“下一个处理者”也是Handler角色。在示例程序中,由Support类扮演此角色。负责处理请求的是support方法。

ConcreteHandler(具体的处理者)

ConcreteHandler角色是处理请求的具体角色。在示例程序中,由NoSupport、LimitSupport、OddSupport、SpecialSupport等各个类扮演此角色。

Client(请求者)

Client角色是向第一个ConcreteHandler角色发送请求的角色。在示例程序中,由Main类扮演此角色。

类图

eOiGQ0.png