Prototype pattern refers to creating duplicate object while keeping performance in mind. This type of design pattern comes under creational pattern as this pattern provides one of the best way to create an object.
This pattern involves implementing a prototype interface which tells to create a clone of the current object. This pattern is used when creation of object directly is costly. For example, a object is to be created after a costly database operation. We can cache the object, returns its clone on next request and update the database as as and when needed thus reducing database calls.
We're going to create an abstract class Mobile and concrete classes extending the Mobile class. A class MobileCache is defined as a next step which stores shape objects in a Hashtable and returns their clone when requested.
PrototypPatternDemo, our demo class will use MobileCache class to get a Mobile object.
Step 1
Create an abstract class implementing Clonable interface.
Mobile.java
public abstract class Mobile implements Cloneable {
private String id;
protected String type;
abstract void make();
public String getType(){
return type;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Object clone() {
Object clone = null;
try {
clone = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return clone;
}
}
Step 2
Create concrete classes extending the above class.
Sony.java
public class Sony extends Mobile {
public Sony(){
type = "Sony";
}
@Override
public void make() {
System.out.println("Inside Sony::make() method.");
}
}
Blackberry.java
public class Blackberry extends Mobile {
public Blackberry(){
type = "Blackberry";
}
@Override
public void make() {
System.out.println("Inside Blackberry::make() method.");
}
}
Samsung.java
public class Samsung extends Mobile {
public Samsung(){
type = "Samsung";
}
@Override
public void make() {
System.out.println("Inside Samsung::make() method.");
}
}
Step 3
Create a class to get concreate classes from database and store them in a Hashtable.
MobileCache.java
import java.util.Hashtable;
public class MobileCache {
private static Hashtable<String, Mobile> shapeMap
= new Hashtable<String, Mobile>();
public static Mobile getMobile(String shapeId) {
Mobile cachedMobile = shapeMap.get(shapeId);
return (Mobile) cachedMobile.clone();
}
// for each shape run database query and create shape
// shapeMap.put(shapeKey, shape);
// for example, we are adding three shapes
public static void loadCache() {
Samsung circle = new Samsung();
circle.setId("1");
shapeMap.put(circle.getId(),circle);
Blackberry square = new Blackberry();
square.setId("2");
shapeMap.put(square.getId(),square);
Sony rectangle = new Sony();
rectangle.setId("3");
shapeMap.put(rectangle.getId(),rectangle);
}
}
Step 4
PrototypePatternDemo uses MobileCache class to get clones of shapes stored in a Hashtable.
PrototypePatternDemo.java
public class PrototypePatternDemo {
public static void main(String[] args) {
MobileCache.loadCache();
Mobile clonedMobile = (Mobile) MobileCache.getMobile("1");
System.out.println("Mobile : " + clonedMobile.getType());
Mobile clonedMobile2 = (Mobile) MobileCache.getMobile("2");
System.out.println("Mobile : " + clonedMobile2.getType());
Mobile clonedMobile3 = (Mobile) MobileCache.getMobile("3");
System.out.println("Mobile : " + clonedMobile3.getType());
}
}
Step 5
Verify the output.
Mobile : Samsung
Mobile : Blackberry
Mobile : Sony