1: // the Window interface
2:
3: interface Window {
4:
5: public void draw(); // draws the Window
6:
7: public String getDescription(); // returns a description of the Window
8:
9: }
10:
11: // implementation of a simple Window without any scrollbars
12:
13: class SimpleWindow implements Window {
14:
15: public void draw() {
16:
17: // draw window
18:
19: }
20:
21: public String getDescription() {
22:
23: return "simple window";
24:
25: }
26:
27: }
28:
29:
30:
31: // abstract decorator class - note that it implements Window
32:
33: abstract class WindowDecorator implements Window {
34:
35: protected Window decoratedWindow; // the Window being decorated
36:
37: public WindowDecorator (Window decoratedWindow) {
38:
39: this.decoratedWindow = decoratedWindow;
40:
41: }
42:
43: }
44:
45:
46:
47: // the first concrete decorator which adds vertical scrollbar functionality
48:
49: class VerticalScrollBarDecorator extends WindowDecorator {
50:
51: public VerticalScrollBarDecorator (Window decoratedWindow) {
52:
53: super(decoratedWindow);
54:
55: }
56:
57: public void draw() {
58:
59: drawVerticalScrollBar();
60:
61: decoratedWindow.draw();
62:
63: }
64:
65: private void drawVerticalScrollBar() {
66:
67: // draw the vertical scrollbar
68:
69: }
70:
71: public String getDescription() {
72:
73: return decoratedWindow.getDescription() + ", including vertical scrollbars";
74:
75: }
76:
77: }
78:
79:
80:
81: // the second concrete decorator which adds horizontal scrollbar functionality
82:
83: class HorizontalScrollBarDecorator extends WindowDecorator {
84:
85: public HorizontalScrollBarDecorator (Window decoratedWindow) {
86:
87: super(decoratedWindow);
88:
89: }
90:
91: public void draw() {
92:
93: drawHorizontalScrollBar();
94:
95: decoratedWindow.draw();
96:
97: }
98:
99: private void drawHorizontalScrollBar() {
100:
101: // draw the horizontal scrollbar
102:
103: }
104:
105: public String getDescription() {
106:
107: return decoratedWindow.getDescription() + ", including horizontal scrollbars";
108:
109: }
110:
111: }
112:
113:
114:
115: public class DecoratedWindowTest {
116:
117: public static void main(String[] args) {
118:
119: // create a decorated Window with horizontal and vertical scrollbars
120:
121: Window decoratedWindow = new HorizontalScrollBarDecorator (
122:
123: new VerticalScrollBarDecorator(new SimpleWindow()));
124:
125: // print the Window's description
126:
127: System.out.println(decoratedWindow.getDescription());
128:
129: }
130:
131: }