-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshapes_example.dart
More file actions
292 lines (241 loc) · 5.97 KB
/
shapes_example.dart
File metadata and controls
292 lines (241 loc) · 5.97 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// behavioral_design_patterns/visitor/shapes_example.dart
/// Visitor Design Pattern:
/// is a behavioral design pattern that allows adding behaviors to existing class hierarchy
/// without altering any existing code.
///
/// Usage examples: Visitor isn’t a very common pattern
/// because of its complexity and narrow applicability.
///
/// Exporting shapes into XML
/// In this example,
/// We would want to export a set of geometric shapes into XML.
/// The catch is that we don’t want to change the code of shapes directly or at least keep it to the minimum.
/// In the end, the Visitor pattern establishes an infrastructure that allows us to add any behaviors to the shapes hierarchy without changing the existing code of those classes.
/// shapes
/// Common shape interface
abstract interface class Shape {
void move(int x, int y);
void draw();
String accept(Visitor visitor);
}
/// A dot
class Dot implements Shape {
final int _id;
final int _x;
final int _y;
const Dot(this._id, this._x, this._y);
/// move shape
@override
void move(int x, int y) {}
/// draw shape
@override
void draw() {}
@override
String accept(Visitor visitor) {
return visitor.visitDot(this);
}
int getX() => _x;
int getY() => _y;
int getId() => _id;
}
/// A circle
class Circle extends Dot {
final int _radius;
const Circle(super.id, super.x, super.y, int radius) : _radius = radius;
@override
String accept(Visitor visitor) {
return visitor.visitCircle(this);
}
int getRadius() => _radius;
}
/// A rectangle
class Rectangle implements Shape {
final int _id;
final int _x;
final int _y;
final int _width;
final int _height;
Rectangle(int id, int x, int y, int width, int height)
: _id = id,
_x = x,
_y = y,
_width = width,
_height = height;
@override
String accept(Visitor visitor) {
return visitor.visitRectangle(this);
}
@override
void move(int x, int y) {
// move shape
}
@override
void draw() {
// draw shape
}
int getId() => _id;
int getX() => _x;
int getY() => _y;
int getWidth() => _width;
int getHeight() => _height;
}
/// A compound shape
class CompoundShape implements Shape {
final int _id;
List<Shape> children = [];
CompoundShape(int id) : _id = id;
// move shape
@override
void move(int x, int y) {}
// draw shape
@override
void draw() {}
int getId() => _id;
@override
String accept(Visitor visitor) {
return visitor.visitCompoundGraphic(this);
}
void add(Shape shape) {
children.add(shape);
}
}
/// visitor
/// Common visitor interface
abstract interface class Visitor {
String visitDot(Dot dot);
String visitCircle(Circle circle);
String visitRectangle(Rectangle rectangle);
String visitCompoundGraphic(CompoundShape cg);
}
/// Concrete visitor, exports all shapes into XML
class XMLExportVisitor implements Visitor {
String export(List<Shape> args) {
StringBuffer sb = StringBuffer();
sb.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
for (Shape shape in args) {
sb.writeln(shape.accept(this));
}
return sb.toString();
}
@override
String visitDot(Dot d) {
return "<dot>\n"
" <id>"
'${d.getId()}'
"</id>\n"
" <x>"
'${d.getX()}'
"</x>\n"
" <y>"
'${d.getY()}'
"</y>\n"
"</dot>";
}
@override
String visitCircle(Circle c) {
return "<circle>\n"
" <id>"
'${c.getId()}'
"</id>\n"
" <x>"
'${c.getX()}'
"</x>\n"
" <y>"
'${c.getY()}'
"</y>\n"
" <radius>"
'${c.getRadius()}'
"</radius>\n"
"</circle>";
}
@override
String visitRectangle(Rectangle r) {
return "<rectangle>\n"
" <id>"
'${r.getId()}'
"</id>\n"
" <x>"
'${r.getX()}'
"</x>\n"
" <y>"
'${r.getY()}'
"</y>\n"
" <width>"
'${r.getWidth()}'
"</width>\n"
" <height>"
'${r.getHeight()}'
"</height>\n"
"</rectangle>";
}
@override
String visitCompoundGraphic(CompoundShape cg) {
return "${"<compound_graphic>" "\n" " <id>" '${cg.getId()}'}</id>\n${_visitCompoundGraphic(cg)}</compound_graphic>";
}
String _visitCompoundGraphic(CompoundShape cg) {
StringBuffer sb = StringBuffer();
for (Shape shape in cg.children) {
String obj = shape.accept(this);
// Proper indentation for sub-objects.
obj = " ${obj.replaceAll("\n", "\n ")}\n";
sb.write(obj);
}
return sb.toString();
}
}
/// Client code
void main() {
Dot dot = Dot(1, 10, 55);
Circle circle = Circle(2, 23, 15, 10);
Rectangle rectangle = Rectangle(3, 10, 17, 20, 30);
CompoundShape compoundShape = CompoundShape(4);
compoundShape.add(dot);
compoundShape.add(circle);
compoundShape.add(rectangle);
CompoundShape c = CompoundShape(5);
c.add(dot);
compoundShape.add(c);
export([compoundShape]);
}
void export(List<Shape> shapes) {
XMLExportVisitor exportVisitor = XMLExportVisitor();
print(exportVisitor.export(shapes));
}
// OutputDemo.txt: Execution result
// <?xml version="1.0" encoding="utf-8"?>
// <circle>
// <id>2</id>
// <x>23</x>
// <y>15</y>
// <radius>10</radius>
// </circle>
// <?xml version="1.0" encoding="utf-8"?>
// <compound_graphic>
// <id>4</id>
// <dot>
// <id>1</id>
// <x>10</x>
// <y>55</y>
// </dot>
// <circle>
// <id>2</id>
// <x>23</x>
// <y>15</y>
// <radius>10</radius>
// </circle>
// <rectangle>
// <id>3</id>
// <x>10</x>
// <y>17</y>
// <width>20</width>
// <height>30</height>
// </rectangle>
// <compound_graphic>
// <id>5</id>
// <dot>
// <id>1</id>
// <x>10</x>
// <y>55</y>
// </dot>
// </compound_graphic>
// </compound_graphic>