Prototype design pattern
The idea of prototype pattern is having a blueprint from which we can create multiple instances.
When to use Prototype design pattern
This pattern can be used when creation of object is costly. For example, Consider a class which required configuration data from a file / database / over a network for initializing. This kind of operations make the object creation very costly especially if you want to create multiple objects of such type. The best solution for this problem is to use the Prototype design pattern – Create the object once and create the subsequent objects by cloning this object.
What are the benefits of using prototype design patterns
Prototype pattern helps us mainly in Time and Memory saving.
- Time :
Creating the object once and create the subsequent objects by cloning this object helps reducing the time for creating the objects.
- Memory :
Consider an object which has more number of String objects, creating a new instance would need to create entirely new immutable strings . By Using the prototype pattern we can avoid this problem by cloning the already created object.
Implementation of prototype pattern:
Lets understand the implementation of prototype design pattern with a simple example. This example is just for understanding the concept. The benefit you are going to get by using the prototype design pattern in real time applications will be more than this example.
Here we have a Box class and we need to create multiple instances of the BOX class. These objects are different only in colors.
Box.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.j2eereference.designPattern.prototype; public class Box implements Cloneable { private int width; private int length; private int depth; String color; Box(int width,int length,int depth){ this.width=width; this.length=length; this.depth=depth; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getLenght() { return length; } public void setLenght(int lenght) { this.length = lenght; } public int getDeptht() { return depth; } public void setDeptht(int deptht) { this.depth = deptht; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Box clone() throws CloneNotSupportedException { Box cloned = (Box)super.clone(); return cloned; } @Override public String toString() { return "Width :"+width+" Length : "+length+" Color : "+color; } } |
PrototypeDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.j2eereference.designPattern.prototype; public class PrototypeDemo { public static void main(String[] args) { Box box=new Box(10,10, 10); try { Box redBox=box.clone(); redBox.setColor("RED"); Box greenBox=box.clone(); greenBox.setColor("GREEN"); System.out.println(redBox); System.out.println(greenBox); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } } |
Output:
Width :10 Length : 10 Color : RED
Width :10 Length : 10 Color : GREEN
Here you can notice that we created the blueprint of Box and did color property settings for creating different objects. This pattern will be more useful if the initialization is costly.