1. What is the prototype model
The prototype pattern belongs to the creation mode of the object. The prototype pattern allows you to create a new instance by copying an existing instance.
The point of this pattern is that the client's code can create a new instance without knowing what particular class to instantiate. In Java, the method of clone () is generally used, or serialized. 2. The realization of prototype model
In Java, because there is a clone () method in the object class, it is very simple to use the prototype pattern, as long as the interface to the cloneable is implemented.
The Cloneable interface provided by the Java language only works by notifying the Java Virtual machine at runtime that it is safe to use the Clone () method on this class. You can get a copy of an object by calling this clone () method. Because the object class itself does not implement the Cloneable interface, calling the Clone () method throws a Clonenotsupportedexception exception if the class being considered does not implement the Cloneable interface.
The specific code is as follows:
public class Student implements Cloneable, serializable{private String name;
private transient int age;
Private book book;
Shallow clone public Student clone () {Student stu = null;
try {stu = (Student) super.clone ();
catch (Clonenotsupportedexception e) {e.printstacktrace ();
return Stu;
}//Deep clone public Student Deepclone () throws Exception {Student = null;
Bytearrayoutputstream bo = new Bytearrayoutputstream ();
ObjectOutputStream oos = new ObjectOutputStream (bo);
Oos.writeobject (this);
Bytearrayinputstream bi = new Bytearrayinputstream (Bo.tobytearray ());
ObjectInputStream oi = new ObjectInputStream (BI);
Stu = (Student) oi.readobject ();
return Stu;
Student (String name, int age, book book) {this.name = name;
This.age = age;
This.book = Book;
Public String GetName () { return name;
public void SetName (String name) {this.name = name;
public int getage () {return age;
public void Setage (int age) {this.age = age;
Public book GetBook () {return book;
The public void Setbook (book book) {this.book = book; }
}
public class book implements serializable{private String name;
Public book (String name) {this.name = name;
Public String GetName () {return name;
public void SetName (String name) {this.name = name; }
}
public class Client {public
static void Main (string[] args) {book Book
= new book ("Chinese");
Student s = new Student ("Tom", 12,book);
Student S1 = S.clone ();
System.out.println (S==S1);
System.out.println (S.getclass () ==s1.getclass ());
System.out.println (S.getbook () ==s1.getbook ());
System.out.println ("------------------------");
try {
Student s2 = S.deepclone ();
System.out.println (S==S2);
System.out.println (S.getclass () ==s2.getclass ());
System.out.println (S.getbook () ==s2.getbook ());
System.out.println ("-------------------");
System.out.println ("S.age:" +s.getage () + "S2.age:" +s2.getage ());
} catch (Exception e) {
e.printstacktrace ();}}}
The results of the operation are as follows:
As you can see in the example, using the Clone () method, we can easily copy a new instance.
However, such a copy, using a shallow clone, then what is a shallow clone. 3. Shallow cloning and deep cloning
Shallow clones:
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.
In other words, if there is a reference to another object in the cloned object, then only the reference to that object is copied, not the new object.
In the above example, the Student object has a reference to book, so S.getbook () ==s1.getbook () returns True when a shallow clone is used (using the Clone () method), the book of the copied S1 is the same object as the original book.
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.
Methods of deep cloning include, deserialize, rewrite the Clone () method, and so on. In the example above, the method of serialization and deserialization is used. From this, S.getbook () ==s1.getbook () returns false.
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.
When serializing an object, you should first implement the Serializable interface, then write the object (actually a copy of the object) into the stream, and then deserialize it from the stream, recreating an object.
The premise of using this method is that all objects referenced by the object being cloned should be serializable, and what to do if you encounter an object that is not serializable.
The transient keyword is used (note: Transient can only modify variables), and transient-modified variables are not serialized when serialized. In the above example, student's age variable uses the transient keyword modifier, so we can see that the value of S.getage () is 12, and S2.getage () has a value of 0 (the int type defaults to 0). 4. Advantages and disadvantages of prototype model
Advantages:
1, to hide the complexity of manufacturing new instances to customers.
2. Provides options that allow customers to generate unknown types of objects.
3. In some environments, copying objects is more efficient than creating objects.
Disadvantages:
1, the replication of the object is sometimes quite complex. Especially when a class reference does not support serialization of indirect objects, or when a reference contains a looping structure.
Use:
1. In a complex class hierarchy, when the system has to create new objects from many of these types, consider the prototype