Understanding Prototype Design Pattern in Java
As the same suggest prototype means some form of cloning and in this design pattern we will understand how we can achieve cloning of an java object. Normally we create an object using “new” operator but with new operator we are creating the whole new object from scratch and so all the necessary operation performed while creating that object will be processed again. To ensure that we don’t want to perform that expensive task again we create an clone of that object. There are 2 types of cloning “Shallow Copy” and “Deep Copy”.
Shallow Copy:
In this technique we perform cloning for single level of object i.e. if there are nested objects than we do not perform the cloning for the nested objects instead we only do the cloning for the 1st object only. This technique is mainly used if you are want to implement cloning on single object.
Deep Copy:
In this technique we perform cloning for nested objects. This is one of the safest cloning technique as it ensures that we will be completely cloning the object. But it becomes complicated when there are multiple levels of nested objects. This technique is mainly used if you are want to implement cloning multiple objects in same object.
NOTE:
If we want to clone an object, that object needs to implement Cloneable Interface. If you are trying to clone an object that does not implement the Cloneable interface throws a CloneNotSupportedException.
To illustrate Prototype Design Pattern below are the examples along with the UML diagrams:
Shallow Copy:
As we have discussed earlier that in shallow copy we perform cloning for single object. On same lines we are now going to clone the Product object as shown below.
package designpattern.prototype; public class Product implements Cloneable { int price = 0; public Object clone() throws CloneNotSupportedException { Product newProduct = new Product(); newProduct.price = this.price; return newProduct; } }
Here the Product Interface implements Cloneable to ensure that we can clone this object, inside clone() method we create an new Product object and all the properties defined in Product viz. price is initialized with the parent price and finally we return the new Product() object. The advantage you see here is you do not have to use the new operator to create Product Object and assigning value to price attribute.
UML Diagram For Shallow Copy:

Shallow Copy Technique
Deep Copy:
As we have discussed earlier that in deep copy we perform cloning for multiple nested object. On same lines we are now going to clone the Product object which accepts Price Object as shown below.
package designpattern.prototype; class Price { public int amount; }
package designpattern.prototype; public class Product implements Cloneable { Price price = new Price(); public Object clone() throws CloneNotSupportedException { Product newProduct = new Product(); newProduct.price = new Price(); newProduct.price.amount = this.price.amount; return newProduct; } }
Here the Product Interface implements Cloneable to ensure that we can clone this object, inside clone() method we create an new Product object, but Product Class uses Price Object so we have also created an new object for Price class and assigned the amount to the new Price object.
UML Diagrams For Deep Copy:

Deep Copy Technique
Popular Articles:
- HTTP Form POST Request using AJAX and Servlet
- JSP – Create Custom Tags
- Ajax Programming with JSP and Servlets
- HTTP POST File Content in JAVA
- Sending Emails using Java
- Java Plugin detection using JavaScript
- Date Manipulation in JAVA
- Sending Exceptions Email Using Apache Log4J
- Factory Design Pattern in Java
- Reading New Emails from Java Applications


































