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