My views on the Design Pattern-6 prototype Pattern

Source: Internet
Author: User

In prototype mode, a prototype instance is used to specify the type of the object to be created, and a new object is created by copying the prototype.

The Prototype mode is a creation design mode. The Prototype mode allows an object to create another custom object. You do not need to know how to create any details. The working principle is: by passing a prototype object to the object to be created, the object to be created is created by requesting the prototype object to copy them.

When the building details inside an object are very complicated, and we often use multiple such instance objects, if we need to create them every time, it will also affect the performance.

Next, we will use an example to describe how to prepare a resume. The construction process of each resume is similar, there will be some specific changes (for example, a resume is for a company and generally does not need to be modified), so that we can create a resume first, then use this resume to copy two copies. This saves us the trouble of creating a resume instance every time and avoids mistakes. If there is something wrong with our resume, we can create three instances by using the soil method, so we have to modify the prototype. If we use the prototype mode, we only need to modify the prototype error, but do not change anything else.

Let's take a look at the code below:

[Java]
Package ResumeShallowCopy;
 
Class Resume implements Cloneable {
Private String name;
Private String sex;
Private int age;
Public Resume (String name)
{
This. name = name;
}
Public void SetPersonalInfo (String sex, int age)
{
This. sex = sex;
This. age = age;
}

Public void Display ()
{
System. out. println (name + "" + sex + "" + age );
}

Public Object Clone ()
{
Try
{
Return (Object) super. clone ();
}
Catch (Exception ex ){
Return null;
}
}
}

[Java]
Package ResumeShallowCopy;
 
Class Test {
Public static void main (String [] args ){
Resume a = new Resume ("");
A. SetPersonalInfo ("male", 30); // You can initialize it in the constructor.

Resume B = (Resume) a. Clone ();
Resume c = (Resume) a. Clone ();
C. SetPersonalInfo ("male", 24); // The information modification method should be provided.
A. Display ();
B. Display ();
C. Display ();
}
 
}

In this example, we only provide a method for preparing resumes. The prototype is more suitable for the following scenarios:

This is a class image, but it cannot be uploaded successfully. The image content is that there is a resume interface, there is a Clone method, and then there is a resume class and a video resume class inherit this interface.
In this way, we can create different resumes.

You should also pay attention to one problem when using the prototype mode: Deep replication and shallow replication. If we add a work experience to our resume, and this work experience uses an instance of another class, it will be referenced by the same object in the light copy, in this way, when we modify the work experience of an object, the work experience of all objects will be affected, and deep replication should be used at this time. You must handle this problem when using the prototype mode. Otherwise, unexpected troubles may occur.
Author: mengxiangyue

Related Article

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.