Java Design pattern: Prototype prototype

Source: Internet
Author: User

Prototype Mode prototype

Prototype mode is also a pattern of object creation, and the main purpose is to create new objects by copying existing objects. It has the advantage of not having to use the new one every time you instantiate the object, which can improve the efficiency of the program if the new object is time-consuming.

Copy in prototype mode is divided into shallow copy and deep copy

Shallow copy: Copies the value types in the object, does not copy the reference type, or points to the same reference;

Deep copy: Copies the value types in the object, and also copies the reference types;

Example:

  Existing CV class, class attributes have name, age and work experience, work experience for reference type including company name and working time

The shallow copy code is as follows:

Working Experience Class Workexperience

Class Workexperience{private string companyname;private int years;public workexperience (string cname, int years) {THIS.C Ompanyname = CNAME; this.years = years;} Public String Getcompanyname () {return companyName;} public void Setcompanyname (String companyName) {this.companyname = CompanyName;} public int Getyears () {return years;} public void Setyears (int years) {this.years = years;}}

Resume class Resumes

Class Resume implements Cloneable{private workexperience workexperience;p rivate String name;p rivate int age;p ublic ResU Me (workexperience workexperience, String name, int age) {super (); this.workexperience = Workexperience;this.name = name; This.age = age;} Public Workexperience getworkexperience () {return workexperience;} public void Setworkexperience (Workexperience workexperience) {this.workexperience = workexperience;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Object Clone () {Try{return super.clone ();  Call the Clone method of the parent class directly}catch (Clonenotsupportedexception e) {//TODO auto-generated catch Blocke.printstacktrace (); return null;}}}

Run Test 1: Create two working experience classes We1 and We2, create a resume class R1 and create a resume class R2 with a copy. Set method to modify the name, age, and work experience of a resume R2

public class Prototype{public static void Main (String args[]) {workexperience we1 = new Workexperience ("Company A", 2); Workexperience we2 = new Workexperience ("Company B", 3); Resume R1 = new Resume (we1, "Xiao Zhang", 24); Resume r2= (Resume) R1.clone () r2.setname ("Xiao Li"), R2.setage (r2.setworkexperience); System.out.println (R1.getname () +r1.getage () + "years old, working experience" +r1.getworkexperience (). Getcompanyname () + "," + R1.getworkexperience (). Getyears () + "year"); System.out.println (R2.getname () +r2.getage () + "years old, working experience" +r2.getworkexperience (). Getcompanyname () + "," + R2.getworkexperience (). Getyears () + "Year");}}

The operation results are as follows

Xiao Zhang 24 years old, working experience company a,2 30 years old, working experience company b,3 years

CV class R2 does not affect R1 after the property value is modified by the Set method.

Run Test 2: Create a work experience class we, create CV class R1 and create CV class R2 by cloning. R2 the property value by the Set method name, age, and through Getworkexperience () to obtain work experience class after the modification work experience. The code is as follows

  

public class Prototype{public static void Main (String args[]) {workexperience we = new Workexperience ("Company A", 2); Resume R1 = new Resume (We, "Xiao Zhang", 24); Resume r2= (Resume) r1.clone (); R2.setname ("Xiao Li"); R2.setage (+); R2.getworkexperience () Setcompanyname ("Company B"); R2.getworkexperience (). Setyears (3); System.out.println (R1.getname () +r1.getage () + "years old, working experience" +r1.getworkexperience (). Getcompanyname () + "," + R1.getworkexperience (). Getyears () + "year"); System.out.println (R2.getname () +r2.getage () + "years old, working experience" +r2.getworkexperience (). Getcompanyname () + "," + R2.getworkexperience (). Getyears () + "Year");}}

Run results

Xiao Zhang 24 years old, working experience company b,3 30 years old, working experience company b,3 years

Resume R2 The work experience of the resume R1 after revising the work experience also has been changed. This is because the copy of the reference in the shallow copy is only the copy of the application address, R1 and R2 point to the same we reference.

Deep copy:

A deep copy copies all of the attributes and copies the dynamically allocated memory that the attribute points to.

Work experience class also implements Cloneable interface

  

Class Workexperience implements cloneable{private string companyname;private int years;public workexperience (string CNAME, int years) {this.companyname = cname; this.years = years;} Public String Getcompanyname () {return companyName;} public void Setcompanyname (String companyName) {this.companyname = CompanyName;} public int Getyears () {return years;} public void Setyears (int years) {this.years = years;} Public Object Clone () {Try{return super.clone ();  Call the Clone method of the parent class directly}catch (Clonenotsupportedexception e) {//TODO auto-generated catch Blocke.printstacktrace (); return null;}}}

  

Resume class: The revision in the Resume class is a copy of the experience of setting up work

  

Class Resume implements Cloneable{private workexperience workexperience;p rivate String name;p rivate int age;p ublic ResU Me (workexperience workexperience, String name, int age) {super (); this.workexperience = Workexperience;this.name = name; This.age = age;} Public Workexperience getworkexperience () {return workexperience;} public void Setworkexperience (Workexperience workexperience) {this.workexperience = workexperience;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Object Clone () {Resume R = null;try{r= (Resume) super.clone (); R.setworkexperience ((Workexperience) Workexperience.clone ()); Set working experience via copy return R; catch (Clonenotsupportedexception e) {//TODO auto-generated catch Blocke.printstacktrace (); return null;}}}

To run the test:

Same as the code in run Test 2

  

public class Prototype{public static void Main (String args[]) {workexperience we = new Workexperience ("Company A", 2); Resume R1 = new Resume (We, "Xiao Zhang", 24); Resume r2= (Resume) r1.clone (); R2.setname ("Xiao Li"); R2.setage (+); R2.getworkexperience () Setcompanyname ("Company B"); R2.getworkexperience (). Setyears (3); System.out.println (R1.getname () +r1.getage () + "years old, working experience" +r1.getworkexperience (). Getcompanyname () + "," + R1.getworkexperience (). Getyears () + "year"); System.out.println (R2.getname () +r2.getage () + "years old, working experience" +r2.getworkexperience (). Getcompanyname () + "," + R2.getworkexperience (). Getyears () + "Year");}}

Run results

Xiao Zhang 24 years old, working experience company a,2 30 years old, working experience company b,3 years

R2 modified work experience did not affect R1.

Summarize:

The primary technique used in prototype mode is cloning, which can improve the efficiency of the program if it is more time-consuming to create objects.

A deep copy takes more memory overhead than a shallow copy, which can be used depending on the situation;

Deep and shallow copies can be used in conjunction (delayed copy) to improve efficiency and deep copy.

  

  

  

  

  

    

Java Design pattern: Prototype prototype

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.