java prototype design pattern object creation

2.5k views Asked by At

I was going through the Prototype design pattern and had some questions.

I have understood the Prototype design pattern is used for the creation of objects that are costly in terms of memory or resources. In that case we use a clone of the object which is already available.

So what is the difference between creating a new object and clone()? Where is the object stored in memory?

3

There are 3 answers

1
Sergey Kalinichenko On BEST ANSWER

Prototype design pattern offers costs savings of two kinds - time savings and space savings.

Time savings come in situations when creating an object requires a costly access to auxiliary information - say, requesting configuration data from a file, a database, or over a network. For example, if you are building lots of pages from a template that is stored on a web server, it is cheaper to read the template once and clone it to get the starting point for each new page, rather than querying the web server separately for each page.

Memory savings come from reusing immutable objects: if your original contains lots of strings, creating a new instance would need to either create entirely new immutable strings, or deal with string interning manually. Using the prototype pattern gracefully avoids this problem by letting the clone share the immutable parts of the template.

0
Ernest Friedman-Hill On

The Java clone() method just creates a new object and copies member variable values into it. In general, it's neither more nor less expensive than creating a new object. The only time clone() might be cheaper than creating an object with new would be when the constructor for an object does something expensive: for example, what if the constructor took the arguments and used them as part of a database query? In that case, using clone() would be cheaper, as the expensive query operation would not happen.

There are other reasons to use this design pattern though: mostly, when objects need complicated setup before use which can't be conveniently done in a constructor. Imagine that an object had 20 properties that needed to be set. If you set them with constructor parameters, that constructor would be horribly ugly -- imagine a constructor with 20 parameters! Instead, you could construct an object with perhaps no parameters, set the 20 values using mutator methods, then clone() the object to make ready-made copies when needed. clone() needs no parameters at all, so it's obviously less ugly. If you needed multiple copies of several different versions of this object, then the prototype pattern becomes attractive.

0
Elmzabi Hassan On

Prototype design pattern is used when the creation of objects take too much of system ressources and performances, and we use this design pattern exactly when we want to have many instances of a class, and those instances are similar, so we don’t really want to use for example the operator “new” because it will be so costly, all that we need is to instantiate those objects basing on the first one already created.

the advantage is that the new object will be independent and it will not take too much of ressources to be created as the first one. here an example of using this concept in java:

  import java.util.Vector;

 public class Samsung implements Cloneable{
    private Vector<String> models; 

    public Samsung(){
        models=new Vector<>(); 
        //we suppose in this comments we access to a data Base to get models
        //and then we get a full list of Samsung models
        //... and finish
        //Sadly we took to much of time to fetch the database 
        //we don't want to waste our time again because Samsung rarely update its database
        models.add("Samsung S1"); 
        models.add("Samsung S2"); 
        models.add("galaxy note"); 
        models.add("galaxy star");
    }
    public Samsung(Vector<String> models){
        this.models=models;
    }

   public Samsung clone()  {

      Vector<String> modelsCopy=new Vector<>();
      Samsung samsungCopy=null;
    //here we don't need to access the database again, we will just copy the previous list
      try{
          for(String model:this.models){
              modelsCopy.add(model);
          }
          samsungCopy=new Samsung(modelsCopy); 
          return samsungCopy;
      }
      catch(Exception e){
          return null;
      }
}

}

the main program :

  public static void main(String[] args) {
        Samsung usa_Samsung=new Samsung();
        Samsung morocco_Samsung=usa_Samsung.clone(); 
        System.out.println("original = " + usa_Samsung);
        System.out.println("copy = " + morocco_Samsung);
    }

output :

original = Samsung@6d06d69c

copy = Samsung@7852e922

like you see these objects have not the same address because they are different .

Note ! i used the name “Samsung” only as an example.