Wednesday, January 15, 2014

Creational DP : Prototype Design Pattern



When creating an object is time consuming and a costly affair and if you already have a most similar object instance in hand, then you go for prototype pattern. Instead of going through a time consuming process to create a complex object, just copy the existing similar object and modify it according to your needs.

You just have to copy the existing instance in hand. When you say copy in java, immediately cloning comes into picture. That’s why when you read about prototype pattern, all the literature invariably refers java cloning. Simple way is, clone the existing instance in hand and then make the required update to the cloned instance so that you will get the object you need.


1:  //In java, to make an object cloning supported, it takes two steps: 1) Implement Cloneable 2) Override clone()  
2:  public interface IIndianFilmIndustry extends Cloneable {  
3:       public IIndianFilmIndustry clone() throws CloneNotSupportedException;  
4:  }  
This pattern is used to:

  • avoid subclasses of an object creator in the client application, like the abstract factory pattern does.
  • avoid the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) when it is prohibitively expensive for a given application 

No comments: