forked from john-bai/DesignPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBridge.java
More file actions
97 lines (87 loc) · 2.09 KB
/
Copy pathBridge.java
File metadata and controls
97 lines (87 loc) · 2.09 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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package designpatterns;
import java.awt.Point;
import java.awt.Rectangle;
/**
*
* @author jtherrell
*/
abstract class Window {
protected WindowImp implementation;
protected Log log;
protected Rectangle boundingBox;
public void drawText() {
implementation.drawText();
}
public void drawRect() {
Point p1 = boundingBox.getLocation();
Point p2 = new Point(p1.x, p1.y + boundingBox.height);
Point p3 = new Point(p1.x + boundingBox.width, p1.y + boundingBox.height);
Point p4 = new Point(p1.x + boundingBox.width, p1.y);
implementation.drawLine(p1, p2);
implementation.drawLine(p2,p3);
implementation.drawLine(p3,p4);
implementation.drawLine(p4,p1);
}
}
class IconWindow extends Window {
public IconWindow(Log l) {
log = l;
implementation = WindowImp.getWindowImp(0);
implementation.attachLog(log);
boundingBox = new Rectangle(10,10);
}
public void draw() {
drawRect();
drawText();
}
}
class TransientWindow extends Window {
public TransientWindow(Log l) {
log = l;
implementation = WindowImp.getWindowImp(1);
implementation.attachLog(log);
boundingBox = new Rectangle(10,10);
}
public void draw() {
drawRect();
}
}
abstract class WindowImp {
protected Log log;
abstract public void drawText();
abstract public void drawLine(Point p1, Point p2);
public void attachLog(Log log) {
this.log = log;
}
static WindowImp getWindowImp(int context) {
return context < 1 ? new XWindowImp() : new YWindowImp();
}
}
class XWindowImp extends WindowImp {
public void drawText() { log.append("world"); }
public void drawLine(Point p1, Point p2) { log.append("hello"); }
}
class YWindowImp extends WindowImp {
public void drawText() { log.append("neato"); }
public void drawLine(Point p1, Point p2) { log.append("wow"); }
}
class Log {
private StringBuffer log;
public Log() {
log = new StringBuffer();
}
public void append(char c) {
log.append(c);
}
public void append(String s) {
log.append(s);
}
@Override
public String toString() {
return log.toString();
}
}