Although the prototype pattern is a pattern of creation, it has nothing to do with the engineering model, and the idea of the pattern is to copy and clone an object as a prototype, and create a new object similar to the original object. This summary will be explained by the copy of the object. In Java, the Copy object is implemented through clone (), and a prototype class is created first:
[Java]View Plaincopy
- public class Prototype implements cloneable {
- Public Object Clone () throws Clonenotsupportedexception {
- Prototype proto = (Prototype) super.clone ();
- return proto;
- }
- }
Very simple, a prototype class, only need to implement the Cloneable interface, overwrite the Clone method, where the Clone method can be changed to any name, because the Cloneable interface is an empty interface, you can arbitrarily define the method name of the implementation class, such as Clonea or CLONEB, Because the focus here is Super.clone (), Super.clone () invokes the Clone () method of object, whereas in the object class, Clone () is native, and I'll do it in another article, The call to interpret native methods in Java is no longer a drill down here. Here, I'll take a look at the shallow copy and deep copy of the object, and first we need to understand the concept of deep, shallow copying of objects:
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.
Here, write an example of a depth copy:
[Java]View Plaincopy
- public class Prototype implements Cloneable, Serializable {
- Private static final long serialversionuid = 1L;
- private string string;
- Private Serializableobject obj;
- /* Shallow copy */
- Public Object Clone () throws Clonenotsupportedexception {
- Prototype proto = (Prototype) super.clone ();
- return proto;
- }
- /* Deep copy */
- Public Object Deepclone () throws IOException, ClassNotFoundException {
- /* writes the binary stream of the current object */
- Bytearrayoutputstream BOS = new Bytearrayoutputstream ();
- ObjectOutputStream oos = new ObjectOutputStream (BOS);
- Oos.writeobject (this);
- /* read out the new object generated by the binary stream */
- Bytearrayinputstream bis = new Bytearrayinputstream (Bos.tobytearray ());
- ObjectInputStream ois = new ObjectInputStream (bis);
- return Ois.readobject ();
- }
- Public String getString () {
- return string;
- }
- public void SetString (string string) {
- this.string = string;
- }
- Public Serializableobject Getobj () {
- return obj;
- }
- public void Setobj (Serializableobject obj) {
- This.obj = obj;
- }
- }
- Class Serializableobject implements Serializable {
- Private static final long serialversionuid = 1L;
- }
To implement a deep copy, you need 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.
Five prototype patterns for Java Design patterns (Prototype)