<六> Read < <大话设计模式> > Prototype mode __ design mode

Source: Internet
Author: User
 

Prototype mode is also a very simple pattern, for Java already has a corresponding interface (cloneable). About the prototype mode << big talk design pattern >> is to put the resume as an example to explain, that I want to put a lot of resumes, in fact, each resume is the same, so as long as I write a copy, the other replication on the line, in fact, is today the prototype model, is to copy the attributes of the class object that you want to copy onto another object (instead of copying the object's reference change).

Prototype Model: Specifies the type of object to create with a prototype instance, and creates a new object by copying the prototypes.

Shallow copy vs deep copy

There are two concepts about cloning

Shallow copy: Just a field that copies a value type, and cannot copy a referenced object. This means that the data in the reference object will not be copied if the other object is referenced in the Resume class.

Deep copy: A field that does not copy a value type, and also copies a referenced object. Distinguish between shallow replication does not copy reference objects, deep copy references objects.

Let's use the code to better understand them.

Shallow copy Code

1, Work experience category (will be referenced by the resume class)

/*

* Working experience

*/

Publicclass Workexperiencesimple {

Private Stringtimearea;

Public String Gettimearea () {

return timearea;

}

publicvoid Settimearea (String timearea) {

this. Timearea = Timearea;

}

}

2, Resume Class (the key is to achieve the Cloneable interface)

/*

* Clone of the shallow copy code

*/

Publicclass Resumesimpleimplements cloneable {

Private Stringname;

Private String Timearea;

Public Workexperiencesimpletimearea;

Public Resumesimple (String name) {

this. Name = name;

Timearea =new workexperiencesimple ();

}

publicvoid setworkexperience (String workname) {

Timearea.settimearea (Workname);

}

publicvoid display () {

System.out.println ("Resume Name:" +name);

System.out.println ("Work Address:" +timearea.gettimearea ());

}

@Override

Public Object Clone ()throws clonenotsupportedexception {

Resumesimpleo = null;

Try {

o = (resumesimple) Super. Clone ();

Catch (Clonenotsupportedexception e) {

E.printstacktrace ();

}

return o;

}

}

3. Client

Publicclass prototypeclient {

/**

* @param args

*/

publicstaticvoid Main (string[] args) {

TODO auto-generated Method stub

Shallow copy

System.out.println ("--------Shallow copy start----------");

Resumesimpleresumesimple =new resumesimple ("Big Bird");

Resumesimple.setworkexperience ("Beijing");

Clone 1, perfect presentation

ResumeSimpleresumeSimple2;

Try {

Resumesimple2= (Resumesimple) Resumesimple.clone ();

Resumesimple2.setworkexperience ("Shanghai");

Resumesimple.display ();

Resumesimple2.display ();

Catch (Clonenotsupportedexception e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

}

}

Print results:

--------Shallow copy starts----------

Resume name: Big Bird

Work Address: Shanghai

Resume name: Big Bird

Work Address: Shanghai

See, the work experience has become Shanghai, the original Beijing has not been copied over. This is the work experience class that has not been replicated.

Deep copy

1, Work experience class (note and shallow copy difference)

/*

* Working experience

* Difference with shallow copy 1, realize the Cloneable interface 2, the method of implementing the interface clone

*/

Publicclass Workexperiencedeepimplements cloneable{

Private Stringtimearea;

Public String Gettimearea () {

return timearea;

}

publicvoid Settimearea (String timearea) {

this. Timearea = Timearea;

}

@Override

Public Object Clone ()throws clonenotsupportedexception {

Workexperiencedeep o =null;

Try {

o = (workexperiencedeep)Super. Clone ();

Catch (Clonenotsupportedexception e) {

E.printstacktrace ();

}

return o;

}

}

2, resume class (pay attention to the difference)

/*

* Clone of the shallow copy code

* differ from shallow copy 1, change the initialization of the work experience class, that is, increase the constructor resumedeep (workexperiencedeepworkname)

* 2,

*/

public class Resumedeepimplements Cloneable {

private String name;

Private String Timearea;

Public Workexperiencedeep Timearea;

Public Resumedeep (String name) {

THIS.name = name;

Timearea = new Workexperiencedeep ();

}

Public Resumedeep (Workexperiencedeep workname) {

try {

Timearea = (workexperiencedeep) workname.clone ();

catch (Clonenotsupportedexception e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

}

public void Setworkexperience (String workname) {

Timearea.settimearea (Workname);

}

public void display () {

System.out.println ("Resume Name:" +name);

System.out.println ("Work Address:" +timearea.gettimearea ());

}

@Override

Public Object Clone () {

Resumedeep o = new Resumedeep (This.timearea); Objective to make a deep copy of work experience

O.setname (name);

return o;

}

public void SetName (String name) {

THIS.name = name;

}

}

3. Client

Publicclass prototypeclient {

/**

* @param args

*/

publicstaticvoid Main (string[] args) {

TODO auto-generated Method stub

Shallow copy

System.out.println ("--------Shallow copy start----------");

Resumesimpleresumesimple =new resumesimple ("Big Bird");

Resumesimple.setworkexperience ("Beijing");

Clone 1, perfect presentation

ResumeSimpleresumeSimple2;

Try {

Resumesimple2= (Resumesimple) Resumesimple.clone ();

Resumesimple2.setworkexperience ("Shanghai");

Resumesimple.display ();

Resumesimple2.display ();

Catch (Clonenotsupportedexception e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

Deep copy

System.out.println ("--------deep copy begins----------");

Resumedeepresumedeep = new resumedeep ("Big Bird");

Resumedeep.setworkexperience ("Beijing");

Clone 1, perfect presentation

RESUMEDEEPRESUMEDEEP2;

Resumedeep2= (Resumedeep) Resumedeep.clone ();

Resumedeep2.setworkexperience ("Shanghai");

Resumedeep.display ();

Resumedeep2.display ();

}

}

Print results:

--------Shallow copy starts----------

Resume name: Big Bird

Work Address: Shanghai

Resume name: Big Bird

Work Address: Shanghai

--------Deep Copy begins----------

Resume name: Big Bird

Work Address: Beijing

Resume name: Big Bird

Work Address: Shanghai

It is obvious that deep replication has replicated the work experience class, so the original Beijing value has not changed.

Attached prototype pattern structure diagram:

The prototype mode __ design mode ">

Summary: Shallow replication has no problem with value types. For reference types, only the reference is copied, and the referenced object is only directed to the original object, so the "work experience" of the two reference settings appears, but it is the last set value of the application, because two references point to the same object. But we might need a requirement to copy the objects that are referenced by the objects we are copying, so we can choose how to copy them deeply.

Attached code: http://download.csdn.net/detail/jzhf2012/8102153

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.