概述
定义一系列算法,将每个算法封装起来。并让它们能够相互替换。策略模式让算法独立于使用它的客户而变化。
针对同一类型问题的多种处理方式
一、不使用策略模式
package main
import "fmt"
type User struct {
Name string
}
func (this User) travel(t string) {
switch t {
case "飞机":
fmt.Printf("%s,飞机出行\n", this.Name)
case "火车":
fmt.Printf("%s,火车出行\n", this.Name)
case "走路":
fmt.Printf("%s,走路出行\n"