模板模式
此开源图书由ithaiq原创,创作不易转载请注明出处
该模式的主要用途是:真正执行步骤已确定但是里面的部分步骤扩展到子类执行。
可以学习的是go仿其他语言的抽象方法的写法
package main
import "fmt"
type ICache interface {
Get() string
Output()
}
type Cache struct {
c ICache
}
func (r *Cache) Get() string {
return ""
}
func (r *Cache) Output() {
fmt.Println(r.c.Get())
}
type Redis struct {
*Cache
}
func NewRedis() *Redis {
this := &Redis{}
this.Cache = &Cache{this}
return this
}
func (r *Redis) Get() string {
return "redis获取"
}
type Mysql struct {
*Cache
}
func NewMysql() *Mysql {
this := &Mysql{}
this.Cache = &Cache{this}
return this
}
func (r *Mysql) Get() string {
return "mysql获取"
}
func main() {
db := NewRedis()
db.Output()
db2 := NewMysql()
db2.Output()
}
最后更新于
这有帮助吗?