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 }