golang 设计模式 simple 代码实现
package simplefactory
import "fmt"
/API is interface
type API interface {
Say(name string) string
}
/NewAPI return Api instance by type
func NewAPI(t int) API {
if t == 1 {
return &hiAPI{}
} else if t == 2 {
return &helloAPI{}
}
return nil
}
/hiAPI is one of API implement
type hiAPI struct{}
/Say hi to name
func (*hiAPI) Say(name string) string {
return fmt.Sprintf("Hi, %s", name)
}
/HelloAPI is another API implement
type helloAPI struct{}
/Say hello to name
func (*helloAPI) Say(name string) string {
return fmt.Sprintf("Hello, %s", name)
}
目录
go 设计模式
相关文章
简单工厂模式
设计模式 simple_test 实现