Intent
If you want a number of the same copies of specified class, use a prototypical instance, and clone the prototype.
Prototype is to copy byte stream from memory(heap memory, exactly) directly and get the same copy in diffferent momory address, not through new to get an instance. So it will not invoke constructor.
Steps:
1.implements Cloneable;
2.Override clone() method, which is in Object class.
3.call the overridden clone() method, when needing copies.
Here we separate the prototype class;
// Prototype class
/**Implementing Cloneable, lets Java JVM know the class can use clone() safely
*and can be able to be copied.
* */
public class ProtoType implements Cloneable {
@Override
public Prototype clone(){ // Override clone()
Prototype prototype = null;
try{
prototype = (Prototype) super.clone();
}catch(CloneNotSupportedException e){
}
return prototype;
}
}
For example, we want to send a email to many people, then we can use copy(clone).
public class Mail extends Prototype {
private int cnt = 0;
public void sendEmail(){
System.out.println("Send the email " + cnt);
}
}
// test
public class ClientTest {
public static void main(String[] args){
Mail mail = new Mail();
for(int i = 0; i < 10; i++){
//come to see here. It's to clone.
Mail clonemail = (Mail) mail.clone();
clonemail.sendEmail(); // all output: Send the email 0
}
}
}
The example above is shallow copy, another is deep copy.
So what's the difference between shallow copy and deep copy?
Shallow copy: clone() method in Object class is shallow copy. Only copy class instance, and String type(cope mechanism is different from int these basic data types), int, long, char these basic data types.
All the referenced objects, including arrays, containers objects, will not be copied. Cloned class objects and prototype object instance will share the the same one object. So it's not safe.
Attetion: clone() conficts with final. If use final, can not use clone().
deep copy: All are copied to a different memory address, including all the referenced objects.
One of the two ways is serialization and deserialization. Also you can use ObjectOutputStream to write to the stream and use ObjectInputStream to read from the stream, then you'll get a new deep copy.
Another way to realize deep copy is to copy(invoke clone() method) all the referenced objects one by one. There follows an example:
public class Prototype implements Cloneable {
private ArrayList<String> list = new Arraylist<String>();
@Override
public Prototype clone(){
Prototype prototype = null;
try{
prototype = (Prototype) super.clone();
prototype.list = (ArrayList<String>) this.list.clone(); // !!
}catch(){
}
}
}