-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_example.dart
101 lines (81 loc) · 3.09 KB
/
window_example.dart
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
// Wikipedia First example (window/scrolling scenario)
// The following example illustrates the use of decorators
// using the window/scrolling scenario.
// source https://en.wikipedia.org/wiki/Decorator_pattern#Java
//step1
// The Window interface class
abstract class IWindow {
void draw(); // Draws the Window
String get getDescription; // returns a description of the Window
}
// step 2
// Implementation of a simple Window without any scrollbars
class SimpleWindow implements IWindow {
@override
void draw() => print("This is SimpleWindow Class Draw"); // Draw window
@override
String get getDescription => "simple window Description";
}
// step 3
// The decorator interface class
// abstract decorator class - note that it implements Window
abstract class WindowDecorator implements IWindow {
final IWindow _windowToBeDecorated; // the Window being decorated
WindowDecorator(IWindow windowToBeDecorated)
: _windowToBeDecorated = windowToBeDecorated;
@override
void draw() => _windowToBeDecorated.draw(); //Delegation
@override
String get getDescription => _windowToBeDecorated.getDescription; //Delegation
}
// step 4
// The following classes contain the decorators for all Window classes,
// Step 4.1
// The first concrete decorator which adds vertical scrollbar functionality
class VerticalScrollBarDecorator extends WindowDecorator {
VerticalScrollBarDecorator(IWindow windowToBeDecorated)
: super(windowToBeDecorated);
@override
void draw() {
super.draw();
_drawVerticalScrollBar();
}
@override
String get getDescription =>
super.getDescription + ", including vertical scrollbars";
// new functionality >> Draw the vertical scrollbar
void _drawVerticalScrollBar() =>
print("add >> vertical scrollbar functionality");
}
// Step 4.2\
// The second concrete decorator which adds horizontal scrollbar functionality
class HorizontalScrollBarDecorator extends WindowDecorator {
HorizontalScrollBarDecorator(IWindow windowToBeDecorated)
: super(windowToBeDecorated);
@override
void draw() {
super.draw();
_drawHorizontalScrollBar();
}
@override
String get getDescription =>
super.getDescription + ", including horizontal scrollbars";
void _drawHorizontalScrollBar() =>
print("add >> horizontal scrollbar functionality");
}
// Here's a test program that creates a Window instance which is fully decorated
//(i.e., with vertical and horizontal scrollbars),
// and prints its description:
void main(List<String> args) {
// Create a decorated Window with horizontal and vertical scrollbars
IWindow simpleWindowWithoutScrolling = SimpleWindow();
IWindow decoratedWindow = HorizontalScrollBarDecorator(
VerticalScrollBarDecorator(simpleWindowWithoutScrolling));
// Print the Window's description
print(decoratedWindow.getDescription);
}
// The output of this program is
// "simple window Description, including vertical scrollbars, including horizontal scrollbars".
// Notice how the getDescription method of the two decorators
// first retrieve the decorated Window's description
// and decorates it with a suffix.