Prototype Design Pattern
Type : Creational Design Pattern
Summary
Prototype design pattern is useful when creating an Object is expensive. Prototype is based on the idea of having a blueprint object for creating other object instances by cloning it.In java Prototype Design pattern is implemented by using cloning an existing object.
Details
In Prototype design pattern, we create a new Object instance by copying (clone) existing object instance and changing required attributes. It is a good use case when Creating an object is complex and expensive. In those cases rather than creating a brand new instance though constructor it is much more efficient if we can clone an existing object.
Participants In Prototype Design Pattern
Client : It calls the prototype to get object instance.
Prototype : It Provides the clone interface.
Clones: The clones object that are being created.
Registry or Cache: It holds the master created objects.
Registry or Cache: It holds the master created objects.
Example
Let's take example of building a Car object from scratch. Car's can be categorized based on its engine, body, look etc. Creating Car object does the following (build engine, build body, configure trip etc. )
Now if we have a Car of color blue, and want to get Car of color red then we can just clone the blue car and change the other attributes like body color, seat color etc.
Class Diagram
Sample Code
// prototype interface
public interface VehiclePrototype extends Cloneable {
public VehiclePrototype clone() throws CloneNotSupportedException;
public void buildEngine(String engineType);
public void buildBody();
public void configureTrim();
public void setColor(String color);
}
// SportsCar which implements our prototype interface
public class SportsCar implements VehiclePrototype {
private String bodyColor;
private String engineType;
public SportsCar(String engineType) {
this.engineType = engineType;
this.buildBody();
this.buildEngine(engineType);
this.configureTrim();
}
@Override
public SportsCar clone() throws CloneNotSupportedException {
return (SportsCar) super.clone();
}
@Override
public void buildEngine(String engineType) {
this.engineType = engineType;
System.out.println("buildEngine");
}
@Override
public void buildBody() {
System.out.println("buildBody");
}
@Override
public void configureTrim() {
System.out.println("configureTrim");
}
@Override
public void setColor(String color) {
this.bodyColor = color;
System.out.println("setColor");
}
}
// vehicle registry which works like a cache
public class VehicleRegistry {
private static java.util.Map prototypes =
new java.util.HashMap();
static {
prototypes.put("SPORTS", new SportsCar("SPORTS"));
prototypes.put("SEDAN", new SportsCar("SEDAN"));
}
// method that the Client will call to get an instance of Vehicle
public static VehiclePrototype getExam(final String type, final String color)
throws CloneNotSupportedException {
// Here we are cloning an object and then updating required attributes
VehiclePrototype exam = prototypes.get(type).clone();
exam.setColor(color);;
return exam;
}
}
Github Code
Advantages
If the creation of new object is expensive then the prototype is the way to go to increase the performance.
Reducing the object initialization effort.
Reducing the need to Subclassing, by the Prototype Object to set the corresponding attributes differently.
Specifying new Object type in runtime.
Reducing the need to Subclassing, by the Prototype Object to set the corresponding attributes differently.
Specifying new Object type in runtime.
Disadvantages
One possible issue with Prototype design pattern is that the participating Objects needs to implement the common interface and override the clone () method.
Comments
Post a Comment