-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstrategy.go
More file actions
43 lines (33 loc) · 757 Bytes
/
strategy.go
File metadata and controls
43 lines (33 loc) · 757 Bytes
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
package ch2
type Strategy interface {
AlgorithmInterface()
}
type ConcreteStrategyA struct {
}
func (this *ConcreteStrategyA) AlgorithmInterface() {
println("this is ConcreteStrategyA")
}
type ConcreteStrategyB struct {
}
func (this *ConcreteStrategyB) AlgorithmInterface() {
println("this is ConcreteStrategyB")
}
type Context struct {
strategy Strategy
}
func NewContext(strategy Strategy) *Context {
context := new(Context)
context.strategy = strategy
return context
}
func (this *Context) ContextInterface() {
this.strategy.AlgorithmInterface()
}
func StrategyRun() {
sa := &ConcreteStrategyA{}
context := NewContext(sa)
context.ContextInterface()
sb := &ConcreteStrategyB{}
context = NewContext(sb)
context.ContextInterface()
}