Java Prototype design pattern

Source: Internet
Author: User

Prototype mode of thinking: An object as a prototype, copy it, clone, and produce a new object similar to the original object.

By the visible, a prototype class, only need to implement Cloneable interface, replication Clone method.

In Java, there are two kinds of clones: the depth of the point, specific to see HTTP://BLOG.CSDN.NET/ZHANGJG_BLOG/ARTICLE/DETAILS/18369201/

The first is shallow copy: re-open the basic type of space, to the reference type, still point to the original object pointing to the

 Public class Implements cloneable{     public objec clone ()throws  clonenotsupportedexception{        = ( Prototype)Super. Clone ();         return Proto;}    }

Look again deep copy: Copy the object, the base type, and the reference type are all re-opening up space.

Deep replication needs to read the binary input of the current object in the form of a stream, and then write out the object corresponding to the binary data.

About input and output, see http://blog.csdn.net/zsw12013/article/details/6534619

 Public classProtopyteImplementscloneable,serializable{Privatestring string;  PublicObject Deepclone ()throwsioexception,classnotfoundexception{//writes the binary stream of the current objectBytearrayoutputstream BOS =NewBytearrayoutputstream (); ObjectOutputStream Oos=NewObjectOutputStream (BOS); Oos.writeobject ( This);//read out the new object generated by the binary streamBytearrayinputstream bis =NewBytearrayinputstream (Bos.tobytearay ()); ObjectInputStream Ois=NewObjectInputStream (bis); returnOis.readobject (); } Get,set}

Java Prototype design pattern

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.