Go design pattern (10)-prototype pattern

Go design pattern (10)-prototype pattern

The prototype mode is the last of the creational modes, and it is mainly used when the cost of object creation is relatively high.

The link to the UML class diagram in this article is: www.processon.com/view/link/6...

The code link for this article is: github.com/shidawuhen/...

1. Definition

1.1 Prototype mode

Prototype mode: Use prototype instances to specify the types of objects to be created, and create new objects by copying these prototypes.

UML class diagram:

1.2 Analysis

The Client in the UML diagram is the client, so you can leave it alone. For Prototype, the core is the Clone function of the object itself, which is used to copy itself. So for the prototype mode, focus on the Clone function. Clone can implement deep copy or shallow copy.

As for whether you need a Prototype interface class, it depends on the specific situation. If you need to use Richter replacement, create an interface class. If it's not useful, you don't need to create it.

2. Usage scenarios

Prototype mode is mainly used in situations where the cost of creating objects is high. If it is extremely convenient to create objects, don't use the prototype mode, otherwise it will be over-designed and the gain will not be worth the loss.

Generally, it is very resource intensive to create an object. For example, a large amount of data needs to be obtained from DB, hard disk, etc., or a large amount of calculation is required; or the content of the created object can be reused, and it is more time-consuming to completely rebuild a new one.

To use the prototype mode, you need to understand deep copy and shallow copy. Shallow copy means that all variables of the copied object contain the same value as the original object, and all references to other objects still point to the original object. Deep copy points the variable of the referenced object to the copied new object instead of the original referenced object. Whether to use deep copy or shallow copy in the project requires specific analysis of specific issues.

When I was looking for a job a few days ago, I used an app called Super Resume, which seemed to fit the prototype model. After editing a resume, you can generate a copy, and the changes to the two resumes will not affect each other. Because different companies have different emphasis on the expression of resumes, but a lot of content in the resume can be reused, so the operation of copying is very practical.

3. Code implementation

package main import "fmt" /** * @Description: Resume category, which contains the basic information of the resume */ type Resume struct { name string age int64 sex string company string experience string } /** * @Description: Set resume personal information * @receiver r * @param name * @param age * @param sex */ func (r *Resume) setPersonInfo(name string, age int64, sex string) { r.name = name r.age = age r.sex = sex } /** * @Description: Set work experience * @receiver r * @param company * @param experience */ func (r *Resume) setWorkExperience(company string, experience string) { r.company = company r.experience = experience } /** * @Description: Display resume content * @receiver r */ func (r *Resume) display() { fmt.Printf("My name is %s, my gender is %s, I am %d years old this year, I work in %s, and my work experience is: %s/n", r.name, r.sex, r.age, r. company, r.experience) } /** * @Description: Deep copy, the core of prototype mode * @receiver r * @return *Resume */ func (r *Resume) clone() *Resume { return &Resume{ name: r.name, sex: r.sex, age: r.age, company: r.company, experience: r.experience, } } func main() { fmt.Println("---------------------------Original resume") resume := &Resume{ name: "Wang Work", sex: "Male", age: 10, company: "Guangmingding Unlimited Liability Company", experience: "Learning martial arts and paddling and fishing", } resume.display() fmt.Println("---------------------------The resume is very good, copy it") copyResume := resume.clone() copyResume.setPersonInfo("Work Li", 21, "Male") copyResume.display() } Copy code

The output is:

myproject go run main.go

---------------------------Original resume

My name is Wang Jiao, gender is male, and I am 10 years old this year. I work in Guangmingding Co., Ltd., and my work experience is: learning martial arts, paddling and fishing

--------------------------- The resume is particularly good, copy it

My name is Job Li, gender, male, 21 years old this year, working in Guangmingding Infinite Liability Company, work experience: learning martial arts and paddling, fishing


The prototype mode is to use the method of copying (or copying) existing objects (prototypes) to create new objects in order to save creation time. Copy can choose deep copy or shallow copy.

At last

If you like my article, you can follow my official account (Programmer Mala Tang)

My personal blog is: shidawuhen.github.io/

Review of previous articles:


  1. Bytedance|Senior server development engineer of Douyin E-commerce Shanghai-Transaction

  2. ByteDance|Wuhan server (senior) development engineer of Douyin e-commerce

  3. Bytedance|Feishu's key account product manager internally pushes slightly

  4. Byte Beat | Douyin e-commerce server technical positions are waiting

  5. ByteDance Recruitment Topic

Design Patterns

  1. Go design pattern (9)-builder pattern

  2. Go design pattern (8)-abstract factory

  3. Go design pattern (7)-factory pattern

  4. Go design pattern (6)-singleton pattern

  5. Go design pattern (5)-class diagram notation

  6. Go design pattern (4)-code writing optimization

  7. Go design pattern (4)-code writing

  8. Go design pattern (3)-design principles

  9. Go design pattern (2)-object-oriented analysis and design

  10. Go design pattern (1)-syntax


  1. Generate of Go tools

  2. Go singleton implementation scheme

  3. Go channel implementation principle

  4. Go timer implementation principle

  5. Beego framework usage

  6. Golang source code bug tracking

  7. Gin framework concise version

  8. Gin source code analysis


  1. General issues of payment access

  2. Current limit realization 2

  3. Spike system

  4. Distributed system and consensus protocol

  5. Service framework and registry of microservices

  6. Talking about microservices

  7. Current limit realization 1

  8. Detailed explanation of CDN request process

  9. Common caching techniques

  10. How to efficiently connect with third-party payment

  11. Algorithm summary


  1. MySQL Development Specification

  2. Redis implements distributed locks

  3. The realization principle of transaction atomicity, consistency and durability

  4. A brief analysis of InnoDB locks and transactions

The internet

  1. HTTP2.0 basic tutorial

  2. HTTPS configuration combat

  3. HTTPS connection process

  4. TCP performance optimization


  1. GoLand practical skills

  2. Automatically generate go struct according to mysql table

  3. Markdown editor recommendation-typora

study notes

  1. Recommended by "Mao Xuan"

  2. in principle

  3. Zi Zhi Tong Jian

  4. Agile revolution

  5. How to exercise your memory

  6. Simple logic-after reading

  7. Hot air-after reading

  8. The Analects-Thoughts after Reading

  9. Sun Tzu Art of War-Thoughts after reading


  1. Fight to mobilize all forces for victory

  2. Against liberalism

  3. Practice theory

  4. Evaluate one's own standards

  5. Server team holiday duty plan

  6. Project process management

  7. Some views on project management

  8. Some thoughts on product managers

  9. Thoughts on the career development of programmers

  10. Thinking about code review