Prototype Pattern
Prototype Design Pattern in Java
The Prototype Pattern is a Creational Design Pattern that creates new objects by copying (cloning) an existing object instead of creating them from scratch using new.
It is useful when object creation is expensive or complex.
Real-World Example
Imagine a game where creating a character involves:
- Loading images
- Loading weapons
- Setting attributes
- Initializing skills
Instead of creating each character from scratch, you create one character and clone it whenever needed.
UML Diagram
+----------------+
| Prototype |
+----------------+
| clone() |
+----------------+
^
|
+----------------+
| Vehicle |
+----------------+
| model |
| color |
+----------------+
| clone() |
+----------------+Example:
Prototype Interface
public interface Prototype {
Prototype clone();
}Concrete Prototype
public class Vehicle implements Prototype {
private String model;
private String color;
public Vehicle(String model, String color) {
this.model = model;
this.color = color;
}
@Override
public Vehicle clone() {
return new Vehicle(model, color);
}
public void display() {
System.out.println(
"Model: " + model +
", Color: " + color
);
}
}Client
public class Main {
public static void main(String[] args) {
Vehicle car1 =
new Vehicle("BMW X5", "Black");
Vehicle car2 = car1.clone();
car1.display();
car2.display();
System.out.println(car1 == car2);
}
}Output
Model: BMW X5, Color: Black
Model: BMW X5, Color: Black
falseThe cloned object has the same data but is a different object in memory.
Using Java’s Cloneable Interface
Java provides the Cloneable marker interface and Object.clone() method.
class Vehicle implements Cloneable {
private String model;
public Vehicle(String model) {
this.model = model;
}
@Override
protected Vehicle clone()
throws CloneNotSupportedException {
return (Vehicle) super.clone();
}
}Usage:
Vehicle v1 = new Vehicle("BMW");
Vehicle v2 = v1.clone();However, many developers avoid Cloneable because it has design limitations and can be error-prone.
Shallow Copy vs Deep Copy
Shallow Copy
Copies primitive fields and object references.
Original Vehicle
|
+----> Engine ObjectCloned Vehicle
|
+----> Same Engine ObjectBoth vehicles share the same Engine.
Example
class Engine {
String type;
}class Vehicle implements Cloneable {
Engine engine;
@Override
public Vehicle clone()
throws CloneNotSupportedException {
return (Vehicle) super.clone();
}
}Deep Copy
Copies nested objects as well.
Original Vehicle
|
+----> Engine ObjectCloned Vehicle
|
+----> New Engine ObjectExample
class Engine {
String type;Engine(String type) {
this.type = type;
}
Engine cloneEngine() {
return new Engine(type);
}
}
class Vehicle {
private String model;
private Engine engine;
Vehicle(String model, Engine engine) {
this.model = model;
this.engine = engine;
}
public Vehicle clone() {
return new Vehicle(
model,
engine.cloneEngine()
);
}
}When to Use Prototype Pattern
Use it when:
Get Syed Nazeer’s stories in your inbox
Join Medium for free to get updates from this writer.
→ Object creation is expensive
→ Many similar objects are required
→ You want to avoid complex constructors
→ Runtime object copying is needed
Examples:
- Game characters
- Document templates
- Configuration objects
- UI components
- Microservice request templates
Advantages
- Faster than creating complex objects repeatedly.
- Reduces object creation cost.
- Hides complex initialization logic.
- Allows dynamic object creation at runtime.
Disadvantages
- Deep cloning can be difficult.
- Circular references can complicate cloning.
- Maintaining clone logic becomes harder as objects grow.
Difference from Factory Pattern
Simple Comparison
// Factory
Vehicle car = vehicleFactory.createVehicle();
// Prototype
Vehicle clonedCar = existingCar.clone();Key idea: Prototype says “copy an existing object”, while Factory says “create a new object.”






