|
| 1 | +// Wikipedia First example (window/scrolling scenario) |
| 2 | +// The following example illustrates the use of decorators |
| 3 | +// using the window/scrolling scenario. |
| 4 | +// source https://en.wikipedia.org/wiki/Decorator_pattern#Java |
| 5 | + |
| 6 | +//step1 |
| 7 | +// The Window interface class |
| 8 | +abstract class IWindow { |
| 9 | + void draw(); // Draws the Window |
| 10 | + String get getDescription; // returns a description of the Window |
| 11 | +} |
| 12 | + |
| 13 | +// step 2 |
| 14 | +// Implementation of a simple Window without any scrollbars |
| 15 | +class SimpleWindow implements IWindow { |
| 16 | + @override |
| 17 | + void draw() => print("This is SimpleWindow Class Draw"); // Draw window |
| 18 | + |
| 19 | + @override |
| 20 | + String get getDescription => "simple window Description"; |
| 21 | +} |
| 22 | + |
| 23 | +// step 3 |
| 24 | +// The decorator interface class |
| 25 | +// abstract decorator class - note that it implements Window |
| 26 | +abstract class WindowDecorator implements IWindow { |
| 27 | + final IWindow _windowToBeDecorated; // the Window being decorated |
| 28 | + |
| 29 | + WindowDecorator(IWindow windowToBeDecorated) |
| 30 | + : _windowToBeDecorated = windowToBeDecorated; |
| 31 | + |
| 32 | + @override |
| 33 | + void draw() => _windowToBeDecorated.draw(); //Delegation |
| 34 | + |
| 35 | + @override |
| 36 | + String get getDescription => _windowToBeDecorated.getDescription; //Delegation |
| 37 | + |
| 38 | +} |
| 39 | + |
| 40 | +// step 4 |
| 41 | +// The following classes contain the decorators for all Window classes, |
| 42 | +// Step 4.1 |
| 43 | +// The first concrete decorator which adds vertical scrollbar functionality |
| 44 | +class VerticalScrollBarDecorator extends WindowDecorator { |
| 45 | + VerticalScrollBarDecorator(IWindow windowToBeDecorated) |
| 46 | + : super(windowToBeDecorated); |
| 47 | + |
| 48 | + @override |
| 49 | + void draw() { |
| 50 | + super.draw(); |
| 51 | + _drawVerticalScrollBar(); |
| 52 | + } |
| 53 | + |
| 54 | + @override |
| 55 | + String get getDescription => |
| 56 | + super.getDescription + ", including vertical scrollbars"; |
| 57 | + |
| 58 | + // new functionality >> Draw the vertical scrollbar |
| 59 | + void _drawVerticalScrollBar() => |
| 60 | + print("add >> vertical scrollbar functionality"); |
| 61 | +} |
| 62 | + |
| 63 | +// Step 4.2\ |
| 64 | +// The second concrete decorator which adds horizontal scrollbar functionality |
| 65 | +class HorizontalScrollBarDecorator extends WindowDecorator { |
| 66 | + HorizontalScrollBarDecorator(IWindow windowToBeDecorated) |
| 67 | + : super(windowToBeDecorated); |
| 68 | + |
| 69 | + @override |
| 70 | + void draw() { |
| 71 | + super.draw(); |
| 72 | + _drawHorizontalScrollBar(); |
| 73 | + } |
| 74 | + |
| 75 | + @override |
| 76 | + String get getDescription => |
| 77 | + super.getDescription + ", including horizontal scrollbars"; |
| 78 | + |
| 79 | + void _drawHorizontalScrollBar() => |
| 80 | + print("add >> horizontal scrollbar functionality"); |
| 81 | +} |
| 82 | + |
| 83 | +// Here's a test program that creates a Window instance which is fully decorated |
| 84 | +//(i.e., with vertical and horizontal scrollbars), |
| 85 | +// and prints its description: |
| 86 | +void main(List<String> args) { |
| 87 | + // Create a decorated Window with horizontal and vertical scrollbars |
| 88 | + IWindow simpleWindowWithoutScrolling = SimpleWindow(); |
| 89 | + IWindow decoratedWindow = HorizontalScrollBarDecorator( |
| 90 | + VerticalScrollBarDecorator(simpleWindowWithoutScrolling)); |
| 91 | + |
| 92 | + // Print the Window's description |
| 93 | + print(decoratedWindow.getDescription); |
| 94 | +} |
| 95 | + |
| 96 | +// The output of this program is |
| 97 | +// "simple window Description, including vertical scrollbars, including horizontal scrollbars". |
| 98 | + |
| 99 | +// Notice how the getDescription method of the two decorators |
| 100 | +// first retrieve the decorated Window's description |
| 101 | +// and decorates it with a suffix. |
0 commit comments