Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the prototype pattern of Java design pattern

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to understand the prototype pattern of the Java design pattern", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the prototype pattern of Java design pattern.

I. Preface

The singleton pattern avoids the repeated creation of resource-consuming objects, but has to share objects. What if the object itself does not allow random access to modification? The usual practice is to back up to a copy, manipulate the copy of other objects, and finally obtain permission merging, similar to the PR operation on git.

II. What is the prototype model

The prototype pattern uses prototype instances to specify the types of objects to be created and creates new objects by copying these prototypes. The key word to note is that the new object and class remain the same. .net provides the Cloneable interface in the System namespace, where it provides the only method Clone (), and you only need to implement this interface to complete the prototype pattern. Because it directly manipulates the binary stream in memory, the performance advantage will be obvious when a large number of complex objects are manipulated or manipulated.

Third, the applicable scenarios of the prototype model.

It is often used to create large objects or initialize tedious objects. Such as the background of the game, the map. Canvas in web and so on

The following scenarios apply:

First, class initialization needs to digest a lot of resources, including data, hardware resources and so on.

Second, generating an object through new requires very tedious data preparation or access, so you can use the prototype pattern.

Third, when an object needs to be provided for other object access, and each caller may need to modify its value, you can consider using prototype mode to copy multiple objects for caller use.

In a real project, the prototype pattern rarely appears alone, usually with the factory method pattern, where an object is created by the clone method and then provided to the caller by the factory method.

Fourth, the realization of prototype pattern.

Take a copy of your resume as an example.

1. Shallow copy implementation

Define work experience classes

/ / work experience class / public class WorkExperience {private string _ workDate; public string WorkDate {get {return _ workDate;} set {_ workDate = value;}} private string _ company; public string Company {get {return _ company;} set {_ company = value;}

Define resume classes

/ class Resume: ICloneable {private string name; private string sex; private string age; private WorkExperience work; public Resume (string name) {this.name = name; work = new WorkExperience () } / set personal information / public void SetPersonalInfo (string sex, string age) {this.sex = sex; this.age = age } / set up work experience / public void SetWorkExperience (string workDate, string company) {work.WorkDate = workDate; work.Company = company } / display / public void Display () {Console.WriteLine ("{0} {1} {2}", name, sex, age); Console.WriteLine ("work experience: {0} {1}", work.WorkDate, work.Company) } public object Clone () {/ / create a shallow copy of the current object return (object) this.MemberwiseClone ();}}

Client call

Static void Main (string [] args) {Resume a = new Resume ("Zhang San"); a.SetPersonalInfo ("male", "30"); a.SetWorkExperience ("2010-2018", "Tencent"); Resume b = (Resume) a.Clone (); b.SetWorkExperience ("2010-2015", "Ali Company"); Resume c = (Resume) a.Clone (); c.SetPersonalInfo ("female", "18") C.SetWorkExperience ("2010-2015", "Baidu"); a.Display (); b.Display (); c.Display (); Console.Read ();}

Result

Zhang San male 30

Work experience 2010-2018 Tencent

Zhang San male 30

Work experience 2010-2018 Tencent

Zhang San female 18

Work experience 2010-2018 Tencent

All variables of the copied object contain the same value as the original object, while all references to other objects still point to the original object, which is called shallow copy. But we may need a requirement to copy all the objects referenced by the copied objects. For example, in the example just now, I hope that the objects referenced by a, b, and c are all different. When copying, one becomes two, two becomes three. At this point, the method we are going to use is called "deep replication."

two。 Deep copy implementation

Deep copy points the variable of the referenced object to the copied new object instead of the original referenced object

/ work experience class / public class WorkExperience:ICloneable {private string _ workDate; public string WorkDate {get {return _ workDate;} set {_ workDate = value;}} private string _ company; public string Company {get {return _ company;} set {_ company = value }} public object Clone () {/ / create a shallow copy of the current object return (object) this.MemberwiseClone ();}} / resume class / class Resume: ICloneable {private string name; private string sex; private string age; private WorkExperience work; public Resume (string name) {this.name = name; work = new WorkExperience () } private Resume (WorkExperience work) {/ / provide private constructors for Clone method calls to clone work experience data this.work = (WorkExperience) work.Clone (); / set personal information / public void SetPersonalInfo (string sex, string age) {this.sex = sex This.age = age;} / setup work experience / public void SetWorkExperience (string workDate, string company) {work.WorkDate = workDate; work.Company = company } / display / public void Display () {Console.WriteLine ("{0} {1} {2}", name, sex, age); Console.WriteLine ("work experience: {0} {1}", work.WorkDate, work.Company) } public object Clone () {/ / calls a private constructor to complete the cloning of work experience, then assigns values to the relevant fields of the resume object, and / / finally returns a deeply copied resume object Resume obj = new Resume (this.work); obj.name = this.name; obj.sex = this.sex; obj.age = this.age Return obj;}}

The same as the client calling code

Result

Zhang San male 30

Work experience 2010-2018 Tencent

Zhang San male 30

Work experience 2010-2015 Ali Company

Zhang San female 18

Work experience 2010-2015 Baidu

Because in some specific situations, deep replication and shallow replication are often involved, for example, the dataset object DataSet, it has Clone () method and Copy () method. The Clone () method is used to copy the structure of DataSet, but not the data of DataSet, thus realizing the shallow replication of the prototype pattern.

The Copy () method copies not only the structure but also the data, which is actually a deep copy of the prototype pattern.

At this point, I believe you have a deeper understanding of "how to understand the prototype pattern of the Java design pattern". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report