Original design pattern (Golang) VI, prototype mode

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Pattern Features: Use prototype instances to specify the kind of objects that are created, and create new objects by copying those prototypes.
Program examples: From the Resume prototype, generate a new resume, resume class resumes provide Clone () method.

package mainimport ("fmt")type PersonalInfo struct {name stringsex  stringage  string}type WorkExperience struct {timeArea stringcompany  string}type Resume struct {PersonalInfoWorkExperience}func (this *Resume) SetPersonalInfo(name string, sex string, age string) {this.name = namethis.sex = sexthis.age = age}func (this *Resume) SetWorkExperience(timeArea string, company string) {this.timeArea = timeAreathis.company = company}func (this *Resume) Display() {fmt.Println(this.name, this.sex, this.age)fmt.Println("工作经历:", this.timeArea, this.company)}func (this *Resume) Clone() *Resume {resume := *thisreturn &resume}func main() {r1 := &Resume{}r1.SetPersonalInfo("大鸟", "男", "29")r1.SetWorkExperience("1998-2000", "xx公司")r2 := r1.Clone()r2.SetWorkExperience("2001-2006", "yy公司")r3 := r1.Clone()r3.SetPersonalInfo("大鸟", "男", "24")r1.Display()r2.Display()r3.Display()}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.