-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrc.com.solid.LSP.java
101 lines (82 loc) · 1.64 KB
/
src.com.solid.LSP.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
package src.com.solid.LSP;
class Rectangle
{
protected int width, height;
public Rectangle() {
}
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getArea() { return width*height; }
@Override
public String toString() {
return "Rectangle{" +
"width=" + width +
", height=" + height +
'}';
}
public boolean isSquare()
{
return width == height;
}
}
class Square extends Rectangle
{
public Square() {
}
public Square(int size) {
width = height = size;
}
@Override
public void setWidth(int width) {
super.setWidth(width);
super.setHeight(width);
}
@Override
public void setHeight(int height) {
super.setHeight(height);
super.setWidth(height);
}
}
class RectangleFactory
{
public static Rectangle newSquare(int side)
{
return new Rectangle(side, side);
}
public static Rectangle newRectangle(int width, int height)
{
return new Rectangle(width, height);
}
}
class LSPDemo
{
// maybe conform to ++
static void useIt(Rectangle r)
{
int width = r.getWidth();
r.setHeight(10);
System.out.println("Expected area of " + (width*10) + ", got " + r.getArea());
}
public static void main(String[] args) {
Rectangle rc = new Rectangle(2, 3);
useIt(rc);
Rectangle sq = new Square();
sq.setHeight(5);
sq.setWidth(10);
useIt(sq);
}
}