-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAdapter.java
More file actions
46 lines (35 loc) · 963 Bytes
/
Adapter.java
File metadata and controls
46 lines (35 loc) · 963 Bytes
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
import static org.junit.Assert.assertEquals;
// Using aggregation, create a component that looks like (adapts to) another interface
class Square0 {
public int side;
public Square0(int side) {
this.side = side;
}
}
interface Rectangle {
int getWidth();
int getHeight();
default int getArea() {
return getWidth() * getHeight();
}
}
class SquareToRectangleAdapter implements Rectangle {
Square0 square;
public SquareToRectangleAdapter(Square0 square) {
this.square = square;
}
public int getWidth() {
return square.side;
}
public int getHeight() {
return square.side;
}
}
class DemoAdapter {
public static void main(String[] args) {
Square0 s = new Square0(5);
Rectangle r = new SquareToRectangleAdapter(s);
assertEquals(5, r.getWidth());
assertEquals(5, r.getHeight());
}
}