-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathstrategy.go
56 lines (44 loc) · 1.18 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
52
53
54
55
56
package behavioral
import "fmt"
// Strategy defines the interface for the strategy to execute.
type Strategy interface {
Execute()
}
// strategyA defines an implementation of a Strategy to execute.
type strategyA struct {
}
// NewStrategyA creates a new instance of strategy A.
func NewStrategyA() Strategy {
return &strategyA{}
}
// Execute executes strategy A.
func (s *strategyA) Execute() {
fmt.Fprintf(outputWriter, "executing strategy A\n")
}
// strategyB defines an implementation of a Strategy to execute.
type strategyB struct {
}
// NewStrategyB creates a new instance of strategy B.
func NewStrategyB() Strategy {
return &strategyB{}
}
// Execute executes strategy B.
func (s *strategyB) Execute() {
fmt.Fprintf(outputWriter, "executing strategy B\n")
}
// Context defines a context for executing a strategy.
type Context struct {
strategy Strategy
}
// NewContext creates a new instance of a context.
func NewContext() *Context {
return &Context{}
}
// SetStrategy sets the strategy to execute for this context.
func (c *Context) SetStrategy(strategy Strategy) {
c.strategy = strategy
}
// Execute executes the strategy.
func (c *Context) Execute() {
c.strategy.Execute()
}