-
Notifications
You must be signed in to change notification settings - Fork 1
/
strategy.go
51 lines (40 loc) · 1.02 KB
/
strategy.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
package strategy
import "fmt"
// Strategy 策略,定义算法的接口
type Strategy interface {
// 某个算法接口
algorithmInterface()
}
// ConcreteStrategyA 实现具体的算法
type ConcreteStrategyA struct {}
func (c ConcreteStrategyA) algorithmInterface() {
// 具体的算法实现
fmt.Println("StrategyA")
}
// ConcreteStrategyB 实现具体的算法
type ConcreteStrategyB struct {}
func (c ConcreteStrategyB) algorithmInterface() {
// 具体的算法实现
fmt.Println("StrategyB")
}
// ConcreteStrategyC 实现具体的算法
type ConcreteStrategyC struct {}
func (c ConcreteStrategyC) algorithmInterface() {
// 具体的算法实现
fmt.Println("StrategyC")
}
// Context 上下文对象
type Context struct {
// 持有策略
strategy Strategy
}
func NewContext(strategy Strategy) *Context {
return &Context{
strategy:strategy,
}
}
// 上下文为客户提供的操作接口
func (c *Context) contextInterface() {
// 通常用转调具体的策略的具体算法
c.strategy.algorithmInterface()
}