-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecorator.java
69 lines (54 loc) · 1.54 KB
/
decorator.java
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
interface Component {
void operation();
}
class ConcreteComponent implements Component {
@Override
public void operation() {
System.out.println("Original undecorated component");
}
}
class Decorator implements Component {
protected Component component;
public Decorator(Component component) {
this.component = component;
}
@Override
public void operation() {
component.operation();
}
}
class ConcreteDecoratorA extends Decorator {
public ConcreteDecoratorA(Component component) {
super(component);
}
/* ConcreteDecoratorA 想要增加的職責 */
private String addedState;
@Override
public void operation(){
super.operation();
addedState = "New State";
System.out.println("DecoratorA operation");
System.out.println("New state is added in concrete decorator A");
}
}
class ConcreteDecoratorB extends Decorator {
public ConcreteDecoratorB(Component component) {
super(component);
}
/* ConcreteDecoratorB 想要增加的職責 */
private void AddedBehavior(){
System.out.println("This is added behavior in concrete decorator B");
}
@Override
public void operation(){
super.operation();
System.out.println("DecoratorB operation");
AddedBehavior();
}
}
public class Main {
public static void main(String[] args){
Component component = new ConcreteDecoratorB(new ConcreteDecoratorA(new ConcreteComponent()));
component.operation();
}
}