装饰器模式
装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案
JDK中的使用
- java.io.BufferedInputStream#BufferedInputStream(java.io.InputStream)
- java.io.BufferedOutputStream#BufferedOutputStream(java.io.OutputStream)
- java.io.DataInputStream#DataInputStream(java.io.InputStream)
- java.util.Collections.CheckedList
- java.util.Collections.CheckedSet
- ava.util.Collections.CheckedMap
代码实现
齐天大圣可以72变 在和杨戬大战中分别变了鸟、鱼、树、寺庙、本身猴子 各个变换之间来回变化
java
public interface TheGreatestSage {
String move();
}
public class Monkey implements TheGreatestSage {
private TheGreatestSage theGreatestSage;
public Monkey() {
}
public Monkey(TheGreatestSage theGreatestSage) {
this.theGreatestSage = theGreatestSage;
}
@Override
public String move() {
StringBuilder sb = new StringBuilder();
if (null != this.theGreatestSage) {
sb.append(this.theGreatestSage.move());
sb.append(" -> ");
}
sb.append("monkey");
return sb.toString();
}
}
public class Bird implements TheGreatestSage {
private TheGreatestSage theGreatestSage;
public Bird(TheGreatestSage theGreatestSage){
this.theGreatestSage = theGreatestSage;
}
@Override
public String move() {
StringBuilder sb = new StringBuilder(this.theGreatestSage.move());
sb.append(" -> ");
sb.append("bird");
return sb.toString();
}
}
public class Fish implements TheGreatestSage {
private TheGreatestSage theGreatestSage;
public Fish(TheGreatestSage theGreatestSage){
this.theGreatestSage = theGreatestSage;
}
@Override
public String move() {
StringBuilder sb = new StringBuilder(this.theGreatestSage.move());
sb.append(" -> ");
sb.append("fish");
return sb.toString();
}
}
public class Tree implements TheGreatestSage {
private TheGreatestSage theGreatestSage;
public Tree(TheGreatestSage theGreatestSage){
this.theGreatestSage = theGreatestSage;
}
@Override
public String move() {
StringBuilder sb = new StringBuilder(this.theGreatestSage.move());
sb.append(" -> ");
sb.append("tree");
return sb.toString();
}
}
public class Temple implements TheGreatestSage {
private TheGreatestSage theGreatestSage;
public Temple(TheGreatestSage theGreatestSage){
this.theGreatestSage = theGreatestSage;
}
@Override
public String move() {
StringBuilder sb = new StringBuilder(this.theGreatestSage.move());
sb.append(" -> ");
sb.append("temple");
return sb.toString();
}
}
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
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
单元测试
groovy
class TheGreatestSageSpec extends Specification {
def theGreatestSage() {
expect:
theGreatestSage.move() == changeProcess
where:
theGreatestSage || changeProcess
new Monkey() || "monkey"
new Bird(new Monkey()) || "monkey -> bird"
new Tree(new Fish(new Monkey())) || "monkey -> fish -> tree"
new Fish(new Bird(new Temple(new Monkey()))) || "monkey -> temple -> bird -> fish"
new Temple(new Tree(new Bird(new Fish(new Monkey())))) || "monkey -> fish -> bird -> tree -> temple"
new Monkey(new Tree(new Temple(new Bird(new Fish(new Monkey()))))) || "monkey -> fish -> bird -> temple -> tree -> monkey"
}
}
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