forked from lee501/go-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.go
40 lines (32 loc) · 932 Bytes
/
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
package bridge
/*
分离抽象部分和实现部分, 桥接设计的核心在于抽象接口和组合抽象接口的结构体
form me: 抽象和实现分离
设计思想:
1. 抽象接口,(实现该接口的具体struct,可扩展多个struct)
2. 属性为抽象接口的struct Phone(桥接层)
3. 与Phone组合模式的具体struct(可以是多个struct)
*/
type Implementor interface {
Operation() string
}
type ConcreateImplementorA struct{}
func (a *ConcreateImplementorA) Operation() string {
return "implementor a"
}
type ConcreateImplementorB struct{}
func (b *ConcreateImplementorB) Operation() string {
return "implementor b"
}
type Abstraction struct {
Imp Implementor
}
func NewAbstraction() *Abstraction {
return &Abstraction{}
}
func (abs *Abstraction) SetImplementor(imp Implementor) {
abs.Imp = imp
}
func (abs *Abstraction) Operation() string {
return abs.Imp.Operation()
}