Design Pattern-Prototype mode

Source: Internet
Author: User

Prototype mode (PROTOTYPE), which specifies the kind of object created with the prototype instance, and creates a new object by copying the prototypes.

Prototype mode is actually the creation of another customizable object from one object without the need to know the details of any creation.

The Clone () method is this: if the field is a value type, the field performs bit-by-copy, and if the field is a reference type, the reference is copied but the referenced object is not copied;

Therefore, the original object and its reference to the same object.

Shallow copy: All variables of the copied object contain the same value as the original object, and all references to other objects still point to the original object.

Deep copy: The Reference object's variable points to the copied new object, not the original referenced object.

Class Diagram:

Example one: Shallow copy
public class Prototype implements cloneable {
private String name;

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

Public Object Clone () {
try {
return Super.clone ();
} catch (Clonenotsupportedexception e) {
E.printstacktrace ();
return null;
}
}

}

public class Testmain {

public static void Main (string[] args) {
Testprototype ();
}

private static void Testprototype () {
Prototype Pro = new Prototype ();
Pro.setname ("original object");
Prototype Pro1 = (Prototype) pro.clone ();
Pro.setname ("changed Object1");

System.out.println ("original object:" + pro.getname ());
System.out.println ("cloned object:" + pro1.getname ());

}

}
Results:
Original Object:changed Object1
Cloned Object:original Object


Example two: Shallow copy
public class prototype{
private String name;

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

}
public class Newprototype implements cloneable {

Private String ID;

Public String getId () {
return ID;
}

public void SetId (String id) {
This.id = ID;
}

Private Prototype Prototype;

Public Prototype Getprototype () {
return prototype;
}

public void Setprototype (Prototype Prototype) {
This.prototype = prototype;
}


Public Object Clone () {
try {
return Super.clone ();
} catch (Clonenotsupportedexception e) {
E.printstacktrace ();
return null;
}
}

}
public class Testmain {

public static void Main (string[] args) {
TODO auto-generated Method Stub
Testprototype ();
}

private static void Testprototype () {
Prototype Pro = new Prototype ();
Pro.setname ("original object");
Newprototype NEWOBJ = new Newprototype ();
Newobj.setid ("Test1");
Newobj.setprototype (PRO);

Newprototype copyobj = (newprototype) newobj.clone ();
Copyobj.setid ("Testcopy");
Copyobj.getprototype (). SetName ("changed object");

System.out.println ("Original Object ID:" + newobj.getid ());
System.out.println ("Original object name:" + Newobj.getprototype (). GetName ());

System.out.println ("Cloned object ID:" + copyobj.getid ());
System.out.println ("Cloned object name:" + Copyobj.getprototype (). GetName ());

}

}

Results:
Original Object Id:test1
Original Object Name:changed Object
Cloned Object Id:testcopy
Cloned Object Name:changed Object


Example three: deep copy
public class Prototype implements cloneable {
private String name;

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

Public Object Clone () {
try {
return Super.clone ();
} catch (Clonenotsupportedexception e) {
E.printstacktrace ();
return null;
}
}

}

public class Newprototype implements cloneable {

Private String ID;

Public String getId () {
return ID;
}

public void SetId (String id) {
This.id = ID;
}

Private Prototype Prototype;

Public Prototype Getprototype () {
return prototype;
}

public void Setprototype (Prototype Prototype) {
This.prototype = prototype;
}


Public Object Clone () {
NEWPROTOTYPE ret = null;
try {
ret = (Newprototype) super.clone ();
Ret.prototype = (prototype) this.prototype.clone ();
return ret;
} catch (Clonenotsupportedexception e) {
E.printstacktrace ();
return null;
}
}

}

public class Testmain {

/**
* @param args
*/
public static void Main (string[] args) {
Testdeepcopy ();
}

private static void Testdeepcopy () {
Prototype Pro = new Prototype ();
Pro.setname ("original object");
Newprototype NEWOBJ = new Newprototype ();
Newobj.setid ("Test1");
Newobj.setprototype (PRO);

Newprototype copyobj = (newprototype) newobj.clone ();
Copyobj.setid ("Testcopy");
Copyobj.getprototype (). SetName ("changed object");

System.out.println ("Original Object ID:" + newobj.getid ());
System.out.println ("Original object name:" + Newobj.getprototype (). GetName ());

System.out.println ("Cloned object ID:" + copyobj.getid ());
System.out.println ("Cloned object name:" + Copyobj.getprototype (). GetName ());

}

}

Results:
Original Object Id:test1
Original Object Name:original Object
Cloned Object Id:testcopy
Cloned Object Name:changed Object

Example four: Using serialization for deep replication
The process of writing an object in a stream is a serialization (serilization) process; Reading an object from a stream is a parallelization (deserialization) process. A copy of the object is written in the stream, and then it is read from the stream to reconstruct the object.
public class Prototypese implements Serializable {

private String name;

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

}

public class Newprototypese implements Serializable {

Private String ID;

Public String getId () {
return ID;
}

public void SetId (String id) {
This.id = ID;
}

Private Prototypese prototype;

Public Prototypese Getprototype () {
return prototype;
}

public void Setprototype (Prototypese prototype) {
This.prototype = prototype;
}

Public Object Deepclone () {
try {
Bytearrayoutputstream bo = new Bytearrayoutputstream ();
ObjectOutputStream oo = new ObjectOutputStream (bo);
Oo.writeobject (this);

Bytearrayinputstream bi = new Bytearrayinputstream (Bo.tobytearray ());
ObjectInputStream oi = new ObjectInputStream (BI);
return Oi.readobject ();
} catch (IOException | ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
return null;
}
}

}

public class Testdeepclone {

public static void Main (string[] args) {
TODO auto-generated Method Stub
Prototypese po = new Prototypese ();
Po.setname ("Test1");
Newprototypese se = new Newprototypese ();
Se.setprototype (PO);

Newprototypese Deepclone = (Newprototypese) se.deepclone ();
Deepclone.getprototype (). SetName ("Test2");

System.out.println ("Original name:" + Se.getprototype (). GetName ());
SYSTEM.OUT.PRINTLN ("Cloned name:" + Deepclone.getprototype (). GetName ());

}
}
Results:
Original Name:test1
Cloned Name:test2

Design Pattern-Prototype mode

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.