The prototype pattern is the last of the creation patterns and is mainly used when object creation costs are high.

In this paper, a UML class diagram link is: www.processon.com/view/link/6…

This article is linked to: github.com/shidawuhen/…

Definition 1.

1.1 Prototype Mode

Stereotype pattern: Use stereotype instances to specify what kind of objects to create, and create new objects by copying these stereotypes.

UML class diagram:

1.2 analysis

The Client in the UML diagram is the Client and can be ignored. For Prototype, the core is the Clone function of the object itself, which copies itself. Therefore, for the prototype mode, focus on the Clone function, which can realize deep copy or shallow copy.

As for whether you need a Prototype interface class, it depends. If you need a Richter substitution, you create it; if not, you don’t.

2. Application scenarios

The prototype pattern is mainly used when creating objects is expensive. If it’s extremely convenient to create objects, don’t use prototype patterns, or you’ll overdesign and lose.

Generally used in the creation of objects, especially resources, such as the need to obtain a large amount of data from DB, hard disk or need to go through a lot of computing; Or if the contents of the created object store are reusable, it is more time-consuming to completely rebuild a new one.

To use the prototype pattern, you need to understand deep copy and shallow copy. Shallow copy is when all the variables of the copied object have the same values as the original object, and all references to other objects still refer to the original object. Deep copy refers to the variable of a reference object to the copied new object instead of the original referenced object. Whether to use deep copy or shallow copy in a project depends on a case-by-case basis.

When I was looking for a job a few days ago, I used an APP called Super Resume, which seemed to fit the prototype pattern. Once a resume is edited, a copy can be created, and changes to both resumes do not affect each other. Because the focus of a resume may vary from company to company, but much of the content in a resume can be reused, copying is useful.

3. Code implementation

package main import "fmt" /** * @Description: Resume classes, */ type Resume struct {name string age int64 sex string company string experience string @Description: * @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: * @receiver r * @param company * @param Experience */ func (r *Resume) setWorkExperience(company string, experience string) { r.company = company r.experience = experience } /** * @Description: @receiver r */ func (r *Resume) display() {FMT.Printf(" My name is %s, gender %s, this year % D years old, working in %s, %s \n", r.name, R.ex, R.age, r.com any, r.xperience)} /** * @description: * @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.e xperience,}} func main () {FMT. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the original resume ") resume: = & resume {name: "king of work," sex: "Male ", age: 10, company:" Bright top unlimited liability Company ", "To learn martial arts and stroke, touch the fish",} resume. The display (FMT). The Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- exceptionally good resume, Clone () copyresume.setPersoninfo (" ", 21, "male ") copyresume.display ()}Copy the code

The output is:

➜ myproject go run main.go

— — — — — — — — — — — — — — — — — — — — — — — — — — — the original resume

My name is Wang Work, male, 10 years old, working in guangming Unlimited liability company, work experience: learn martial arts, water stroke, fish

————————— resume is very good, copy

My name is Li Work, male, 21 years old, working in guangming Unlimited liability company, work experience: learn martial arts, water stroke, fish

conclusion

Prototype mode is to use the existing object (prototype) to copy (or called copy) to create new objects, in order to achieve the purpose of creating time. You can select deep copy or shallow copy.

The last

If you like my article, you can follow my public account (Programmer Malatang)

My personal blog is shidawuhen.github. IO /

Review of previous articles:

recruitment

  1. Bytes to beat | trill electricity senior server-side development engineer – trading in Shanghai

  2. Bytes to beat | trill electric ShangWuHan server-side development engineer (senior)

  3. Bytes to beat | fly book big customer push product manager

  4. Bytes to beat | trill electricity service side technical posts vacant

  5. Bytedance recruitment special

Design patterns

  1. Go Design Mode (9)- Builder mode

  2. Go Design Pattern (8)- Abstract Factory

  3. Go Design Mode (7)- Factory Mode

  4. Go Design Pattern (6)- Singleton pattern

  5. Go Design Pattern (5)- Class diagram symbolic representation

  6. Go Design Pattern (4)- Code writing optimization

  7. Go Design Pattern (4)- Code writing

  8. Go Design Patterns (3)- Design principles

  9. Go Design Pattern (2)- Object-oriented analysis and design

  10. Go Design Pattern (1)- Syntax

language

  1. Go tool generate

  2. Go singleton implementation scheme

  3. Implementation principle of Go channel

  4. Implementation principle of Go timer

  5. Beego framework use

  6. Golang source BUG tracking

  7. Gin framework concise version

  8. Gin source code analysis

architecture

  1. Payment access general issues

  2. Current limiting 2

  3. Seconds kill system

  4. Distributed systems and consistency protocols

  5. Service framework and registry for microservices

  6. Discussion on Micro-service

  7. Current limiting implementation 1

  8. CDN request process details

  9. Common Cache tips

  10. How to effectively connect with third-party payment

  11. Algorithm is summarized

storage

  1. MySQL development specification

  2. Redis implements distributed locking

  3. The implementation principle of atomicity, consistency and persistence of transactions

  4. InnoDB locks and transactions

network

  1. HTTP2.0 basics tutorial

  2. HTTPS Configuration Combat

  3. HTTPS Connection Process

  4. TCP Performance Optimization

tool

  1. GoLand Practical skills

  2. Automatically generate go struct from mysql table

  3. Markdown editor recommends – Typora

Reading notes

  1. Selected by MAO

  2. The principle of

  3. History As A Mirror

  4. Agile revolution

  5. How to exercise your memory

  6. Simple Logic – After reading

  7. Hot Wind – After reading

  8. Analects of Confucius – After reading

  9. Sun Tzu’s Art of War – Reflections from reading

thinking

  1. Struggle to mobilize all forces for victory

  2. Anti-liberalism

  3. practical

  4. The standard by which you judge yourself

  5. Service team holiday shift plan

  6. Project process management

  7. Some thoughts on project management

  8. Some thoughts on product manager

  9. Thinking about programmer career development

  10. Thinking about code review