-
Notifications
You must be signed in to change notification settings - Fork 1
/
BridgeCodingExercise.java
117 lines (99 loc) · 2.27 KB
/
BridgeCodingExercise.java
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
/*
Exercise from Bridge section: given an example which results in Cartesian-product
duplication, refactor the hierarchy, giving the base class Shape a constructor that takes
an interface called Renderer defined as
interface Renderer
{
public String whatToRenderAs();
}
*/
package structural.bridge.exercise;
interface Renderer
{
public String whatToRenderAs(String name);
}
abstract class Shape implements Renderer
{
protected Renderer renderer;
public abstract String getName();
public Shape(Renderer renderer) {
this.renderer = renderer;
}
}
class Triangle extends Shape
{
public Triangle(Renderer renderer) {
super(renderer);
}
@Override
public String getName()
{
return "Triangle";
}
@Override
public String whatToRenderAs(String name) {
return renderer.whatToRenderAs(name);
}
@Override
public String toString() {
return whatToRenderAs(this.getName());
}
}
class Square extends Shape
{
public Square(Renderer renderer) {
super(renderer);
}
@Override
public String getName()
{
return "Square";
}
@Override
public String whatToRenderAs(String name) {
return renderer.whatToRenderAs(name);
}
@Override
public String toString() {
return whatToRenderAs(this.getName());
}
}
class VectorRenderer implements Renderer
{
@Override
public String whatToRenderAs(String name) {
return String.format("Drawing %s as lines", name);
}
}
class RasterRenderer implements Renderer
{
@Override
public String whatToRenderAs(String name) {
return String.format("Drawing %s as pixels", name);
}
}
//class VectorSquare extends Square
//{
// @Override
// public String toString()
// {
// return String.format("Drawing %s as lines", getName());
// }
//}
//
//class RasterSquare extends Square
//{
// @Override
// public String toString()
// {
// return String.format("Drawing %s as pixels", getName());
// }
//}
// imagine VectorTriangle and RasterTriangle are here too
class BridgeExerciseDemo
{
public static void main(String[] args) {
Triangle triangle = new Triangle(new VectorRenderer());
System.out.println(triangle);
}
}