I. prototype mode implementation:
1. Cloning:A. provides a class as the clone prototype, which implements the clonable interface B. overwrite the clone () method of the object class in this class, and use super. clone () method to complete cloning C. in external use, a prototype object is generated first, and then the clone () method is called to generate the cloned object.
2. Deep clone:A. provides a class as the clone prototype, which implements the serializable interface B. this class overwrites the clone () method of the object class, and uses the serialization method to complete the clone (Object stream read/write) C. in external use, a prototype object is generated first, and then the clone () method is called to generate the cloned object.
Ii. prototype mode and deep and light cloning:
Use object. the clone () method is a "shortest clone". All the variables of the Copied object contain the same value as the original object, all references to other objects still point to the original objects. In other words, the shortest copy only copies the objects to be considered, rather than the objects referenced by the shortest copy.
Follow these steps:
① To obtain a copy of an object, we can use the clone () method of the object class.
② Override the clone () method of the base class in the derived class and declare it as public.
③ Call Super. Clone () in the clone () method of the derived class ().
④ Implement the cloneable interface in the derived class.
Why must we call Super. Clone () When overwriting the object's clone () method in a derived class? During the runtime, the clone () in the object should identify which object you want to copy, allocate space for the object, and copy the object, copy the content of the original object to the bucket of the new object one by one.
To implement "Deep clone"-all variables of the Copied object contain the same value as the original object, except those variables that reference other objects. Variables that reference other objects will point to new objects that have been copied, instead of the original referenced objects. In other words, deep replication copies all the objects referenced by the objects to be copied.
Deep cloning requires the object serialization function provided in Java-writing the object to be copied to a buffer stream, and then reading the object through the input stream to complete object replication.
Example:
Try {
// Open a buffer in the memory for writing itself
Bytearrayoutputstream bout = new bytearrayoutputstream ();
Objectoutputstream out = new objectoutputstream (bout );
// Write itself into the buffer through the serialization Mechanism
Out. writeobject (this );
Out. Close ();
// Find the buffer zone you just opened to prepare for reading
Bytearrayinputstream bin = new bytearrayinputstream (bout
. Tobytearray ());
Objectinputstream in = new objectinputstream (BIN );
// Read the content just written into a new object
Object ret = in. readobject ();
In. Close ();
// Returns this object. The copy is complete.
Return ret;
} Catch (exception e ){
E. printstacktrace ();
Return NULL;
}
Iii. Advantages and Disadvantages of prototype mode and applicable scenarios:
One advantage of using the prototype mode is that it can save a lot of writing interface implementation classes. If the factory mode is used, if a user interface factory class is provided for each scenario specified by the user, it will bring us heavy workload. In the future, the prototype mode can be used to avoid the increasing number of user interface factory classes.
A fatal disadvantage of the prototype mode is that the prototype method in Java does not allow the new object to have different methods than the parent object. At this time, before using the prototype method, you need to carefully consider the advantages and disadvantages of the prototype method, or even try whether the prototype mode meets the requirements.
To sum up, the prototype mode is most applicable when the classes of several objects only differ in attributes, and the behavior is completely the same. After copying a prototype object, you can fine-tune its attributes to achieve customization.