Prototype Pattern

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(){
    
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天去看你人人了,突然在想,如果我们是在现实中彼此认识,会有什么样的故事?哈哈。一个灵魂,两个世界吧。也更觉得缘真...
    十来克阅读 184评论 0 1
  • 在这个浮躁的时代,快步骤的社会,会感觉自己有点力不从心,我想静静!习惯了忙碌,这两个月停了下来,看着自己的...
    谁从谁的全世界路过阅读 239评论 0 0
  • 相信我,这儿有你需要的工具。 作为深度互联网工具挖掘者,会遇到各种好玩有趣的网站,我会持续把自己发现的非常有趣或者...
    ruruii如蕤阅读 538评论 0 1
  • 终一天,梦将散,何时能把酒言欢 独相念,泪流满,昔人不见已成仙 天将欲雨天尝在 人亡落泪人无情
    一字马平川阅读 113评论 0 0