-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolid.isp.go
98 lines (71 loc) · 1.36 KB
/
solid.isp.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
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
type Document struct {
}
type Machine interface {
Print(d Document)
Fax(d Document)
Scan(d Document)
}
// ok if you need a multifunction device
type MultiFunctionPrinter struct {
// ...
}
func (m MultiFunctionPrinter) Print(d Document) {
}
func (m MultiFunctionPrinter) Fax(d Document) {
}
func (m MultiFunctionPrinter) Scan(d Document) {
}
type OldFashionedPrinter struct {
// ...
}
func (o OldFashionedPrinter) Print(d Document) {
// ok
}
// Deprecated: ...
func (o OldFashionedPrinter) Fax(d Document) {
panic("operation not supported")
}
// Deprecated: ...
func (o OldFashionedPrinter) Scan(d Document) {
panic("operation not supported")
}
// better approach: split into several interfaces
type Printer interface {
Print(d Document)
}
type Scanner interface {
Scan(d Document)
}
// printer only
type MyPrinter struct {
// ...
}
func (m MyPrinter) Print(d Document) {
// ...
}
// combine interfaces
type Photocopier struct {}
func (p Photocopier) Scan(d Document) {
//
}
func (p Photocopier) Print(d Document) {
//
}
type MultiFunctionDevice interface {
Printer
Scanner
}
// interface combination + decorator
type MultiFunctionMachine struct {
printer Printer
scanner Scanner
}
func (m MultiFunctionMachine) Print(d Document) {
m.printer.Print(d)
}
func (m MultiFunctionMachine) Scan(d Document) {
m.scanner.Scan(d)
}
func main() {
}