-
Notifications
You must be signed in to change notification settings - Fork 0
/
solid.lsp.go
85 lines (68 loc) · 1.37 KB
/
solid.lsp.go
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
package main
import "fmt"
type Sized interface {
GetWidth() int
SetWidth(width int)
GetHeight() int
SetHeight(height int)
}
type Rectangle struct {
width, height int
}
// vvv !! POINTER
func (r *Rectangle) GetWidth() int {
return r.width
}
func (r *Rectangle) SetWidth(width int) {
r.width = width
}
func (r *Rectangle) GetHeight() int {
return r.height
}
func (r *Rectangle) SetHeight(height int) {
r.height = height
}
// modified LSP
// If a function takes an interface and
// works with a type T that implements this
// interface, any structure that aggregates T
// should also be usable in that function.
type Square struct {
Rectangle
}
func NewSquare(size int) *Square {
sq := Square{}
sq.width = size
sq.height = size
return &sq
}
func (s *Square) SetWidth(width int) {
s.width = width
s.height = width
}
func (s *Square) SetHeight(height int) {
s.width = height
s.height = height
}
type Square2 struct {
size int
}
func (s *Square2) Rectangle() *Rectangle {
return &Rectangle{s.size, s.size}
}
func UseIt(sized Sized) {
width := sized.GetWidth()
sized.SetHeight(10)
expectedArea := 10 * width
actualArea := sized.GetWidth() * sized.GetHeight()
fmt.Print("Expected an area of ", expectedArea,
", but got ", actualArea, "\n")
}
func main() {
rc := &Rectangle{2, 3}
UseIt(rc)
sq := NewSquare(5)
UseIt(sq)
sq2 := Square2{5}
UseIt(sq2.Rectangle())
}