Prototype Design Pattern
The Prototype Design Pattern makes it easier to create new objects by copying an existing one. It simplifies the process of creating new objects by duplicating an existing one instead of creating a completely new one, which can save costs. The existing object serves as a prototype and contains the object's state.
The newly copied object may change the same properties only if required. This approach saves costly resources and time, especially when object creation is a heavy process.
One of the best available ways to create an object from existing objects is the clone() method. Clone is the simplest approach to implementing a prototype pattern.
Example:
We will be having one abstract class in which we will be implementing the cloneable interface. The cloneable interface is implemented to ensure that every subsequent class extending the base abstract class can use its method implementation. In the below-mentioned class we have the abstract class named as Shape, so we will implement clone method in that class.
We need to create each type of object and store it in something like HashMap or HashTable. Then, when needed, we can clone the stored object and return it. Let's see some practical example now.
Shape class:
public abstract class Shape implements Cloneable {
public Object clone() {
Object clone = null;
try {
clone = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return clone;
}
abstract String displayType();
}
Circle Class:
package com.design.pattern.prototype; public class Circle extends Shape { private String type; public Circle() { this.type = "Circle"; } public String displayType() { return "This is "+type+" of object"; }}
Rectangle Class:
package com.design.pattern.prototype; public class Rectangle extends Shape { private String type; public Rectangle() { this.type = "rectangle"; } public String displayType() { return "This is "+type+" of object"; }}
Triangle Class:
package com.design.pattern.prototype; public class Triangle extends Shape{ private String type; public Triangle() { this.type = "Triangle"; } public String displayType() { return "This is "+type+" of object"; }}
ShapeStore Class:
package com.design.pattern.prototype;
import java.util.HashMap;
import java.util.Map;
public class ShapeStore {
public static Map<String, Shape> cachedObjects=new HashMap<>();
public static void loadObjects() {
cachedObjects.put("circle", new Circle());
cachedObjects.put("triangle", new Triangle());
cachedObjects.put("rectangle", new Rectangle());
}
public static Shape getShape(String shape) {
return (Shape) cachedObjects.get(shape).clone();
}
}
PrototypeDemo Class:
package com.design.pattern.prototype;
public class PrototypeDemo {
public static void main(String[] args) {
ShapeStore.loadObjects();
Shape circle=ShapeStore.getShape("circle");
Shape triangle=ShapeStore.getShape("triangle");
Shape rectangle=ShapeStore.getShape("rectangle");
System.out.println(circle.displayType());
System.out.println(triangle.displayType());
System.out.println(rectangle.displayType());
}
}
Thanks for Reading :)
Read this post for more such articles — Backend Development Resources