-
Notifications
You must be signed in to change notification settings - Fork 141
/
bridge.go
63 lines (47 loc) · 1.18 KB
/
bridge.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
package bridge
import "fmt"
/*
桥接模式分离抽象部分和实现部分。使得两部分独立扩展。
桥接模式类似于策略模式,区别在于策略模式封装一系列算法使得算法可以互相替换。
策略模式使抽象部分和实现部分分离,可以独立变化。
*/
type AbstractMessage interface {
SendMessage(text, to string)
}
type MessageImplementer interface {
Send(text, to string)
}
type SMS struct {
}
func ViaSMS() MessageImplementer {
return &SMS{}
}
func (*SMS) Send(msg, to string) {
fmt.Printf("Send msg %v to %v via SMS \n", msg, to)
}
type Email struct {
}
func ViaEmail() MessageImplementer {
return &Email{}
}
func (*Email) Send(msg, to string) {
fmt.Printf("Send msg %v to %v via Email \n", msg, to)
}
type CommonMessage struct {
via MessageImplementer
}
func NewCommentMessage(via MessageImplementer) *CommonMessage {
return &CommonMessage{via: via}
}
func (m *CommonMessage) SendMessage(msg, to string) {
m.via.Send(msg, to)
}
type Urgency struct {
via MessageImplementer
}
func NewUrgencyMessage(via MessageImplementer) *Urgency {
return &Urgency{via: via}
}
func (m *Urgency) SendMessage(msg, to string) {
m.via.Send(msg, to)
}