Java Design Pattern Rookie series (16) modeling and implementation of prototype model

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/lhy_ycu/article/details/39997337


Prototype mode (Prototype): The idea of this mode is to use an object as a prototype, copy it, clone it, and produce a new object similar to the original object. There are two types of replication here: Shallow copy, deep copy.

Shallow copy : After copying an object, the variables of the base data type are recreated, and the reference type, pointing to the original object.

deep copy : After copying an object, both the base data type and the reference type are recreated. In short, deep replication is completely replicated, and shallow copying is not exhaustive.

The following is illustrated by an example:

First, shallow copy 1, UML modeling:



2. Code implementation

/** * Prototype mode: Use an object as a prototype, copy it, clone it, and produce a "new object" similar to the original object. * And there are two types of replication here: Shallow copy, deep copy * * Example (a) shallow copy: After copying an object, the variables of the base data type are recreated, * * and the reference type, pointing to the original object, is not re-created. */class Prototype implements cloneable {private int age;private int[] array = new int[] {1, 2, 3};p ublic Prototype () {}p Ublic Prototype (int age) {this.age = age;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public int[] GetArray () {return array;} public void SetArray (int[] array) {This.array = array;} /** * Because the Cloneable interface is an empty interface * * The emphasis here is Super.clone (), Super.clone () is called the Clone () method of object, whereas in the object class, Clone () is the native * This involves JNI, about JNI and the NDK later on, where you just have to remember that the core of the shallow copy is Super.clone (). */public Object Cloneobject () throws clonenotsupportedexception {Prototype Prototype = (Prototype) super.clone (); return Prototype;}} /** * Client Test class * * @author Leo */public class Test {public static void main (string[] args) throws Clonenotsupportedexceptio n {Prototype Prototype = new Prototype (20); Prototype Cloneproto = (Prototype) prototype.clOneobject ();/** * by printing you can see: Prototype and Cloneproto the two variables of the same type point to two different memory addresses * * This indicates cloning succeeded */system.out.println ("prototype =" + prototype); System.out.println ("Cloneproto =" + Cloneproto);/** * To completely copy an object, then its reference type variable array points to a different memory address * * And here the reference type variable array, Point to or point to the original object. You can see that the printed memory address is the same. * This indicates that the object copy is not completely */system.out.println ("prototype.getarray () =" + Prototype.getarray ()); System.out.println ("cloneproto.getarray () =" + Cloneproto.getarray ());/** * can be seen through this example: shallow copy does not copy the object completely */}}

Second, deep replication

1. UML Modeling:



2. Code implementation

/** * Example (b) deep copy: After copying an object, both the base data type and the reference type are re-created. * * In short, the deep copy is completely copied, and the shallow copy is not complete. * * Because this involves the reading and writing of the object, so here is the serialization of the object-implemented the Serializable interface */class Prototype implements Cloneable, Serializable {private static Final long serialversionuid = 1l;private int age;private int[] array = new int[] {1, 2, 3};p ublic Prototype () {}public P Rototype (int age) {this.age = age;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public int[] GetArray () {return array;} public void SetArray (int[] array) {This.array = array;}  /* Deep Copy */public Object Deepclone () throws IOException, ClassNotFoundException {/* Write binary Stream */bytearrayoutputstream BOS for current object = new Bytearrayoutputstream (); ObjectOutputStream oos = new ObjectOutputStream (BOS); Oos.writeobject (this);/* read out the new object produced by the binary stream */bytearrayinputstream bis = new Bytearrayinputstream (Bos.tobytearray ()); ObjectInputStream Ois ObjectInputStream (bis); return Ois.readobject ();}} /** * Client Test class * * @author Leo */public class Test {public static voidMain (string[] args) throws Ioexception,classnotfoundexception {Prototype Prototype = new Prototype (20);  Prototype Cloneproto = (Prototype) prototype.deepclone ();/** * can be seen by printing: Prototype and Cloneproto These two variables of the same type point to two different memory addresses * * This demonstrates cloning success */system.out.println ("prototype =" + prototype); System.out.println ("Cloneproto =" + Cloneproto);/** * by printing you can see that the reference type variable of two objects array points to a different memory address * * This indicates that the object has been completely replicated */system . Out.println ("prototype.getarray () =" + Prototype.getarray ()); System.out.println ("cloneproto.getarray () =" + Cloneproto.getarray ());/** * Of course, we can also try to print the contents of the reference variable, * can see: The content is unchanged (1 2 3) , only the memory address that the reference variable points to is changed. */int[] Proarray = Prototype.getarray (); int[] Cloneprotoarray = Cloneproto.getarray (); for (int p:proarray) {System.out. Print (p + "\ T");} System.out.println (); for (int p:cloneprotoarray) {System.out.print (p + "\ T");}}}

Iii. Summary

1. The core of the shallow copy is Super.clone (), which invokes the Clone () method of object, whereas in the object class, Clone () is native.

2, to achieve deep replication, you need to use the form of a binary stream to write the current object, and then read it.




Java Design Pattern Rookie series (16) modeling and implementation of prototype model

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.