Java Design Pattern 13th bullet--prototype mode

Source: Internet
Author: User
Tags serialization
There are two ways to clone in Java: shallow cloning and deep cloning superficial cloning

It is only responsible for cloning data passed by value (such as base data type, string type) without copying the object it refers to, in other words, all references to other objects still point to the original object. Deep Cloning

In addition to the value of a shallow clone to be cloned, it is also responsible for cloning the data of the reference type. The variables that refer to other objects will point to the new object that was copied, not the original referenced object. In other words, a deep clone copies the objects referenced by the object being copied, and this replication of the referenced object is called indirect replication.

How many layers of deep cloning to go into is a problem that is not easy to determine. When deciding to copy an object in a deep clone, you must decide whether to take a shallow clone or continue with a deep clone of an object that is indirectly replicated. Therefore, in the case of deep cloning, it is necessary to decide how deep it is. In addition, in the process of deep cloning, the issue of circular references is likely to occur and must be handled with care. using serialization to achieve deep cloning

The process of writing an object into a stream is a serialization (serialization) process, while the process of reading an object from a stream is called a deserialization (deserialization) process. It should be noted that a copy of the object is written into the stream, and the original object still exists inside the JVM.

Deep cloning of an object in the Java language can often be done by enabling the object to implement the Serializable interface, then writing the object (actually just the copy of the object) into a stream (serialized), and then reading back (deserializing) from the stream, and then rebuilding the object.

Package com.lkx.sjms.protory;

Import java.io.Serializable;

public class Goldringedstaff implements serializable{
	private float height = 100.0f;
    private float diameter = 10.0f;
    /**
     * Growth behavior, each call length and radius increased by one times
     * * Public
    void Grow () {
        this.diameter *= 2;
        This.height *= 2;
    }
    /**
     * Shrink behavior, each call length and radius reduced by half
     * * public
    void shrink () {
        this.diameter/= 2;
        This.height/= 2;
    }

Package com.lkx.sjms.protory;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;
Import java.io.Serializable;

Import Java.util.Date;
	public class Mokey implements cloneable,serializable{private int height;
	private int weight;
	Private Date brithday;
	
	Private Goldringedstaff staff;
		Public Mokey () {this.brithday= new Date ();
	Staff = new Goldringedstaff ();
		/*** * Shallow clone/Public Mokey clone () {Mokey temp = null;
		try{temp = (Mokey) super.clone ();
		}catch (Exception e) {e.printstacktrace ();
	}finally{return temp. /*** * Deep Clone * @return * @throws IOException * @throws classnotfoundexception/public Mokey deepclone () t
		Hrows IOException, classnotfoundexception{//written from object to stream bytearrayoutputstream Bos = new Bytearrayoutputstream ();
		ObjectOutputStream oos = new ObjectOutputStream (BOS);
		
		Oos.writeobject (this); Read back from the streamto Bytearrayinputstream bis = new Bytearrayinputstream (Bos.tobytearray ());
		ObjectInputStream ois = new ObjectInputStream (bis);
		
		
	Return (Mokey) ois.readobject ();
	public int getheight () {return height;
	public void setheight (int height) {this.height = height;
	public int getweight () {return weight;
	The public void setweight (int weight) {this.weight = weight;
	Public Date Getbrithday () {return brithday;
	The public void Setbrithday (Date brithday) {this.brithday = Brithday;
	Public Goldringedstaff Getstaff () {return staff;
	public void Setstaff (Goldringedstaff staff) {this.staff = staff;
 }
	
	
	
	
}

Package com.lkx.sjms.protory;

Import java.io.IOException;

public class Protorytest {

	private Mokey Mokey = new Mokey ();

	public static void Main (string[] args) throws ClassNotFoundException, IOException {protorytest
		protory = new Protoryt EST ();
		Protory.change ();
	}
	
	
	public void Change () throws ClassNotFoundException, ioexception{
		mokey copmokey = Mokey.deepclone ();
		System.out.println ("The Birthday of the Holy Sage" +mokey.getbrithday ());
		System.out.println ("Clone The Birthday of the Holy Sage" +copmokey.getbrithday ());
		System.out.println ("Whether the clone of the Holy Sage and the Holy Sage is the same object" + (Mokey==copmokey));
		System.out.println ("Whether the divine of the clone sage and the Divine of the Holy Sage) is the same object" + (Mokey.getstaff () ==copmokey.getstaff ());
		
		
		
	}
	





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.