-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjava-patt.4.java
238 lines (232 loc) · 5.53 KB
/
java-patt.4.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// -*- mode: java; buffer-read-only: t -*-
class IsVegetarianV {
boolean forSkewer () {
return true;
}
boolean forOnion (ShishD s) {
return s.isVegetarian();
}
boolean forLamb (ShishD s) {
return false;
}
boolean forTomato (ShishD s) {
return s.isVegetarian();
}
}
class OnlyOnionsV {
boolean forSkewer() {
return true;
}
boolean forOnion(ShishD s) {
return s.onlyOnions();
}
boolean forLamb(ShishD s) {
return false;
}
boolean forTomato(ShishD s) {
return false;
}
}
abstract class ShishD {
OnlyOnionsV oofn = new OnlyOnionsV();
IsVegetarianV ivfn = new IsVegetarianV();
abstract boolean onlyOnions();
abstract boolean isVegetarian();
}
class Skewer extends ShishD {
boolean onlyOnions () {
return oofn.forSkewer();
}
boolean isVegetarian () {
return ivfn.forSkewer();
}
}
class Onion extends ShishD {
ShishD s;
Onion(ShishD _s) {
s = _s;
}
// ----------------------------------------
boolean onlyOnions () {
return oofn.forOnion(s);
}
boolean isVegetarian () {
return ivfn.forOnion(s);
}
}
class Lamb extends ShishD {
ShishD s;
Lamb(ShishD _s) {
s = _s;
}
// ----------------------------------------
boolean onlyOnions () {
return oofn.forLamb(s);
}
boolean isVegetarian () {
return ivfn.forLamb(s);
}
}
class Tomato extends ShishD {
ShishD s;
Tomato(ShishD _s) {
s = _s;
}
// ----------------------------------------
boolean onlyOnions () {
return oofn.forLamb(s);
}
boolean isVegetarian () {
return ivfn.forTomato(s);
}
}
class RemAV {
PizzaD forCrust() {
return new Crust();
}
PizzaD forCheese(PizzaD p) {
return new Cheese(p.remA());
}
PizzaD forOlive(PizzaD p) {
return new Olive(p.remA());
}
PizzaD forAnchovy(PizzaD p) {
return p.remA();
}
PizzaD forSausage(PizzaD p) {
return new Sausage(p.remA());
}
}
class TopAwCV {
PizzaD forCrust() {
return new Crust();
}
PizzaD forCheese(PizzaD p) {
return new Cheese(p.topAwC());
}
PizzaD forOlive(PizzaD p) {
return new Olive(p.topAwC());
}
PizzaD forAnchovy(PizzaD p) {
return
new Cheese
(new Anchovy
(p.topAwC()));
}
PizzaD forSausage(PizzaD p) {
return new Sausage(p.topAwC());
}
}
class SubAbCV {
PizzaD forCrust() {
return new Crust();
}
PizzaD forAnchovy(PizzaD p) {
return new Cheese(p.subAbC());
}
PizzaD forSausage(PizzaD p) {
return new Sausage(p.subAbC());
}
PizzaD forOlive(PizzaD p) {
return new Olive(p.subAbC());
}
PizzaD forCheese(PizzaD p) {
return new Cheese(p.subAbC());
}
}
abstract class PizzaD {
RemAV remfn = new RemAV();
TopAwCV topfn = new TopAwCV();
SubAbCV subfn = new SubAbCV();
abstract PizzaD remA();
abstract PizzaD topAwC();
abstract PizzaD subAbC();
};
class Crust extends PizzaD {
PizzaD remA() { return remfn.forCrust(); }
PizzaD topAwC() { return topfn.forCrust(); }
PizzaD subAbC() { return subfn.forCrust(); }
public String toString() {
return "Crust";
}
}
class Cheese extends PizzaD {
PizzaD p;
Cheese(PizzaD _p) {
p = _p;
}
// ----------------------------------------
PizzaD remA() { return remfn.forCheese(p); }
PizzaD topAwC() { return topfn.forCheese(p); }
PizzaD subAbC() { return subfn.forCheese(p); }
public String toString() {
return String.format("Cheese %s", p);
}
}
class Olive extends PizzaD {
PizzaD p;
Olive(PizzaD _p) {
p = _p;
}
// ----------------------------------------
PizzaD remA() { return remfn.forOlive(p); }
PizzaD topAwC() { return topfn.forOlive(p); }
PizzaD subAbC() { return subfn.forOlive(p); }
public String toString() {
return String.format("Olive %s", p);
}
}
class Anchovy extends PizzaD {
PizzaD p;
Anchovy(PizzaD _p) {
p = _p;
}
// ----------------------------------------
PizzaD remA() { return remfn.forAnchovy(p); }
PizzaD topAwC() { return topfn.forAnchovy(p); }
PizzaD subAbC() { return subfn.forAnchovy(p); }
public String toString() {
return String.format("Anchovy %s", p);
}
}
class Sausage extends PizzaD {
PizzaD p;
Sausage(PizzaD _p) {
p = _p;
}
// ----------------------------------------
PizzaD remA() { return remfn.forSausage(p); }
PizzaD topAwC() { return topfn.forSausage(p); }
PizzaD subAbC() { return subfn.forSausage(p); }
public String toString() {
return String.format("Sausage %s", p);
}
}
class Test {
static void p(Object... objs) {
for(Object o:objs) {
System.out.printf("%s ", o);
}
}
static private void test1() {
ShishD s1 = new Skewer();
ShishD s2 = new Onion(s1);
ShishD s3 = new Onion(s2);
ShishD s4 = new Lamb(s3);
ShishD s5 = new Lamb(s4);
ShishD s6 = new Tomato(s5);
p(s6.isVegetarian());
p(s3.isVegetarian());
p(s3.onlyOnions());
p(s6.onlyOnions());
}
static private void test2() {
}
static public void main(String[] args) {
p("\n", "---test1", "\n");
test1();
p("\n", "---test2", "\n");
test2();
p ("\n", "--", "\n");
}
}