Prototype Design Pattern: Cloning Objects for Efficiency

The Prototype Design Pattern is a creational design pattern that allows you to create new objects by cloning an existing object (prototype) instead of creating new instances from scratch. This is useful when creating similar objects is expensive or requires complex configuration.
Why Use the Prototype Pattern?
Efficiency: Avoids costly initialization by reusing an existing object.
Flexibility: Allows you to create new objects with slight variations.
Simplifies Object Creation: Reduces the need for complex configuration.
Example: Creating Customized Robots
Let’s say you’re building a robot manufacturing system. Each robot has a set of attributes like name
, color
, and weapon
. Instead of configuring each robot from scratch, you can use the Prototype Pattern to clone a pre-configured robot and customize it as needed.
Step-by-Step Implementation
Step 1: Create the Robot
Class
The Robot
class implements the Cloneable
interface to support cloning.
public class Robot implements Cloneable {
private String name;
private String color;
private String weapon;
public Robot(String name, String color, String weapon) {
this.name = name;
this.color = color;
this.weapon = weapon;
}
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getWeapon() {
return weapon;
}
public void setWeapon(String weapon) {
this.weapon = weapon;
}
// Step 2: Override the clone method
@Override
public Robot clone() {
try {
return (Robot) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError(); // Should never happen
}
}
@Override
public String toString() {
return "Robot [name=" + name + ", color=" + color + ", weapon=" + weapon + "]";
}
}
Step 3: Use the Prototype to Create Robots
Now, you can create a prototype robot and clone it to create new robots with slight modifications.
public class Main {
public static void main(String[] args) {
// Step 4: Create a prototype robot
Robot prototypeRobot = new Robot("RX-100", "Silver", "Laser Gun");
// Step 5: Clone the prototype to create a new robot
Robot robot1 = prototypeRobot.clone();
robot1.setName("RX-200"); // Customize the cloned robot
// Step 6: Clone the prototype again to create another robot
Robot robot2 = prototypeRobot.clone();
robot2.setColor("Gold"); // Customize the cloned robot
// Print the robots
System.out.println("Prototype Robot: " + prototypeRobot);
System.out.println("Robot 1: " + robot1);
System.out.println("Robot 2: " + robot2);
}
}
Output
Prototype Robot: Robot [name=RX-100, color=Silver, weapon=Laser Gun]
Robot 1: Robot [name=RX-200, color=Silver, weapon=Laser Gun]
Robot 2: Robot [name=RX-100, color=Gold, weapon=Laser Gun]
Real-Life Analogy
Think of the Prototype Pattern like a photocopier. You have an original document (prototype), and you make copies of it. You can then modify the copies as needed without starting from scratch.
When to Use the Prototype Pattern?
When creating an object is expensive or complex.
When you need to create multiple objects with similar configurations.
When you want to avoid repetitive initialization code.
Subscribe to my newsletter
Read articles from Niharika Maruvada directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Niharika Maruvada
Niharika Maruvada
JAVA Developer Passionate about crafting innovative and efficient solutions in the FinTech industry, I thrive on building cutting-edge applications that solve real-world problems. With a strong focus on clean, scalable, and maintainable code, I aim to drive business success through technology. Always eager to embrace new challenges and expand my skill set, I am committed to staying at the forefront of emerging technologies and best practices. My dedication to continuous learning and professional growth fuels my ability to deliver impactful results. Let’s connect and build the future of FinTech together! #Java #FinTech #Innovation #CleanCode #TechEnthusiast #ContinuousLearning