原型模式
原型接口
package prototype
type Cloneable interface{
clone() Cloneable
}
执行深拷贝的原型对象
package prototype
type Deep struct {
name string
}
func NewDeep(name string) *Deep {
deep := new(Deep)
deep.name = name
return deep
}
func (d *Deep)clone()Cloneable{
copy := *d
return ©
}
执行浅拷贝的原型对象
package prototype
type Fleet struct {
name string
}
func NewFleet(name string) *Fleet {
fleet := new(Fleet)
fleet.name = name
return fleet
}
func (f *Fleet)clone()Cloneable{
return f
}
测试文件
package prototype
import (
"fmt"
"testing"
)
func TestPrototype(T *testing.T){
d := NewDeep("tom")
d1 := d.clone()
fmt.Println(d == d1)
f := NewFleet("jack")
f1 := f.clone()
fmt.Println(f == f1)
}
标签:return,name,clone,模式,原型,func,string
From: https://www.cnblogs.com/mathsmouse/p/16664986.html