-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.java
122 lines (99 loc) · 2.39 KB
/
builder.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
class Sticker {
private String color;
private int width;
private int height;
public Sticker() {
}
public Sticker(String color, int width, int height) {
this.color = color;
this.width = width;
this.height = height;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
void show(){
System.out.printf("my color: %s, my width: %d, my height: %d\n", color, width, height);
}
}
interface StickerBuilder {
void buildColor();
void buildWidth();
void buildHeight();
Sticker getResult();
}
class StickerDirector {
void construct(StickerBuilder bui){
bui.buildColor();
bui.buildWidth();
bui.buildHeight();
}
}
class CuteStickerBuilder implements StickerBuilder{
Sticker sticker = new Sticker();
@Override
public void buildColor() {
sticker.setColor("blue");
}
@Override
public void buildWidth() {
sticker.setWidth(12);
}
@Override
public void buildHeight() {
sticker.setHeight(12);
}
@Override
public Sticker getResult() {
return sticker;
}
}
class FunnyStickerBuilder implements StickerBuilder {
Sticker sticker = new Sticker();
@Override
public void buildColor() {
sticker.setColor("orange");
}
@Override
public void buildWidth() {
sticker.setWidth(87);
}
@Override
public void buildHeight() {
sticker.setHeight(78);
}
@Override
public Sticker getResult() {
return sticker;
}
}
public class Main {
public static void main(String[] args) {
StickerDirector di = new StickerDirector();
StickerBuilder bui1 = new CuteStickerBuilder();
StickerBuilder bui2 = new FunnyStickerBuilder();
// build sticker based on builder 1
di.construct(bui1);
Sticker sticker1 = bui1.getResult();
sticker1.show();
// build sticker based on builder 2
di.construct(bui2);
Sticker sticker2 = bui2.getResult();
sticker2.show();
}
}