-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS09_Decorator.java
More file actions
66 lines (49 loc) · 1.38 KB
/
DS09_Decorator.java
File metadata and controls
66 lines (49 loc) · 1.38 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
//Component定义一个对象接口,可以给这些对象动态地添加职责。
interface Person {
void eat();
}
//ConcreteComponent 定义一个对象,可以给这个对象添加一些职责。
class Man implements Person {
public void eat() {
System.out.println("男人在吃");
}
}
//Decorator 维持一个执行Component对象的指针,并定义一个与Componect 接口一致的接口。
abstract class Decorator implements Person {
protected Person person;
public void setPerson(Person person) {
this.person = person;
}
public void eat() {
person.eat();
}
}
//ConcreteDectrator 想组建添加职责
class ManDecoratorA extends Decorator {
public void eat() {
super.eat();
reEat();
System.out.println("ManDecoratorA类");
}
public void reEat() {
System.out.println("再吃一顿饭");
}
}
class ManDecoratorB extends Decorator {
public void eat() {
super.eat();
System.out.println("===============");
System.out.println("ManDecoratorB类");
}
}
public class DS09_Decorator{
public static void main(String[] args) {
Man man = new Man();
ManDecoratorA md1 = new ManDecoratorA();
ManDecoratorB md2 = new ManDecoratorB();
md1.setPerson(man);
md1.eat();
md2.setPerson(md1);
md2.eat();
}
}