forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
597 B
46 lines
597 B
3 years ago
|
package strategy
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
type Strategy interface {
|
||
|
Execute()
|
||
|
}
|
||
|
|
||
|
type StrategyA struct {
|
||
|
}
|
||
|
|
||
|
func (s *StrategyA) Execute() {
|
||
|
fmt.Println("A 执行了...")
|
||
|
}
|
||
|
|
||
|
func NewStrategyA() *StrategyA {
|
||
|
return &StrategyA{}
|
||
|
}
|
||
|
|
||
|
type StrategyB struct {
|
||
|
}
|
||
|
|
||
|
func (s *StrategyB) Execute() {
|
||
|
fmt.Println("B 执行了...")
|
||
|
}
|
||
|
|
||
|
func NewStrategyB() *StrategyB {
|
||
|
return &StrategyB{}
|
||
|
}
|
||
|
|
||
|
type Contest struct {
|
||
|
strategy Strategy
|
||
|
}
|
||
|
|
||
|
func (c *Contest) Execute() {
|
||
|
c.strategy.Execute()
|
||
|
}
|
||
|
|
||
|
func NewContest() *Contest {
|
||
|
return &Contest{}
|
||
|
}
|
||
|
|
||
|
func (c *Contest) SetContest(strategy Strategy) {
|
||
|
c.strategy = strategy
|
||
|
}
|