package prototype
import "time"
type Lemon struct {
Name string
Address string
CreateTime time.Time
}
func New(name string, address string, time time.Time) *Lemon{
return &Lemon{
Name: name,
Address: address,
CreateTime: time,
}
}
func (this *Lemon) Clone() *Lemon {
return New(this.Name,this.Address,this.CreateTime)
}
type School struct {
ClassId int
}
func NewSchool(classId int) *School {
return &School{ClassId: classId}
}
func (s *School) Clone() *School{
return NewSchool(s.ClassId)
}
type Home struct {
Address string
phone string
}
func NewHome(address string,phone string) *Home{
return &Home{
Address: address,
phone: phone,
}
}
func (h *Home) Clone() *Home {
return NewHome(h.Address,h.phone)
}
type Person struct {
Name string
Age int
School *School
Home *Home
}
func NewPerson(name string,age int,school *School,home *Home) *Person {
return &Person{
Name: name,
Age: age,
School: school,
Home: home,
}
}
func (p *Person) Clone() *Person{
return NewPerson(p.Name,p.Age,p.School.Clone(),p.Home.Clone())
}
package prototype
import (
"fmt"
"testing"
"time"
)
func TestLemon_Clone(t *testing.T) {
lemon := New("Tom","gdsz",time.Now())
fmt.Printf("prototype -> lemon:%#v, time:%s\n",lemon,lemon.CreateTime.Format("2006-01-02 15:04:05"))
ln := lemon.Clone()
fmt.Printf("clone -> lemon:%#v, time:%s\n",ln,ln.CreateTime.Format("2006-01-02 15:04:05"))
fmt.Println("chmod time")
fmt.Printf("prototype ->time:%s\n",lemon.CreateTime.Format("2006-01-02 15:04:05"))
}
func TestPersonClone(t *testing.T) {
person := &Person{
Name: "Tom",
Age: 22,
School: &School{10001},
Home: &Home{
Address: "广东深圳",
phone: "15000011000",
},
}
fmt.Printf("\nperson:%#v,school:%#v, home:%#v\n",*person,*person.School,*person.Home)
p2 := person.Clone()
fmt.Printf("\nperson:%#v,school:%#v, home:%#v\n",*p2,*p2.School,*p2.Home)
person.Home.phone= "1860000xxx"
fmt.Println("修改原型中home中手机号:")
fmt.Printf("\n克隆中:person:%#v,school:%#v, home:%#v\n",*p2,*p2.School,*p2.Home)
fmt.Printf("\n原型中: person:%#v,school:%#v, home:%#v\n",*person,*person.School,*person.Home)
}
go test -v
=== RUN TestLemon_Clone
prototype -> lemon:&prototype.Lemon{Name:"Tom", Address:"gdsz", CreateTime:time.Time{wall:0xbfe1a7251f02fae8, ext:711862, loc:(*time.Location)(0x1227280)}}, time:2020-11-07 12:39:48
clone -> lemon:&prototype.Lemon{Name:"Tom", Address:"gdsz", CreateTime:time.Time{wall:0xbfe1a7251f02fae8, ext:711862, loc:(*time.Location)(0x1227280)}}, time:2020-11-07 12:39:48
chmod time
prototype ->time:2020-11-07 12:39:48
--- PASS: TestLemon_Clone (0.00s)
=== RUN TestPersonClone
person:prototype.Person{Name:"Tom", Age:22, School:(*prototype.School)(0xc00011e128), Home:(*prototype.Home)(0xc00012c0a0)},school:prototype.School{ClassId:10001}, home:prototype.Home{Address:"广东深圳", phone:"15000011000"}
person:prototype.Person{Name:"Tom", Age:22, School:(*prototype.School)(0xc00011e170), Home:(*prototype.Home)(0xc00012c0e0)},school:prototype.School{ClassId:10001}, home:prototype.Home{Address:"广东深圳", phone:"15000011000"}
修改原型中home中手机号:
克隆中:person:prototype.Person{Name:"Tom", Age:22, School:(*prototype.School)(0xc00011e170), Home:(*prototype.Home)(0xc00012c0e0)},school:prototype.School{ClassId:10001}, home:prototype.Home{Address:"广东深圳", phone:"15000011000"}
原型中: person:prototype.Person{Name:"Tom", Age:22, School:(*prototype.School)(0xc00011e128), Home:(*prototype.Home)(0xc00012c0a0)},school:prototypchool{ClassId:10001}, home:prototype.Home{Address:"广东深圳", phone:"1860000xxx"}
--- PASS: TestPersonClone (0.00s)
PASS
ok mlemon_ssh/factory/prototype 0.212s