2原型模式-golang

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)
}

//result:
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值