-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestChain.java
More file actions
64 lines (60 loc) · 1.36 KB
/
TestChain.java
File metadata and controls
64 lines (60 loc) · 1.36 KB
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
package design.pattern;
abstract class Chain{
public static int One = 1;
public static int Two = 2;
public static int Three = 3;
protected int Threshold;
protected Chain next;
public void setNext(Chain chain){
next = chain;
}
public void message(String msg, int priority){
if (priority <= Threshold) {
writeMessage(msg);
}
if (next != null) {
next.message(msg, priority);
}
}
abstract protected void writeMessage(String msg);
}
class A extends Chain{
public A(int threshold){
this.Threshold = threshold;
}
protected void writeMessage(String msg) {
System.out.println("A: " + msg);
}
}
class B extends Chain{
public B(int threshold){
this.Threshold = threshold;
}
protected void writeMessage(String msg) {
System.out.println("B: " + msg);
}
}
class C extends Chain{
public C(int threshold){
this.Threshold = threshold;
}
protected void writeMessage(String msg) {
System.out.println("C: " + msg);
}
}
public class TestChain {
private static Chain createChain() {
Chain chain1 = new A(Chain.Three);
Chain chain2 = new B(Chain.Two);
chain1.setNext(chain2);
Chain chain3 = new C(Chain.One);
chain2.setNext(chain3);
return chain1;
}
public static void main(String[] args) {
Chain chain = createChain();
chain.message("level 3", Chain.Three);
chain.message("level 2", Chain.Two);
chain.message("level 1", Chain.One);
}
}