Prototype Design Pattern Explained with Examples | Beginner to Advanced Guide
Reading Time: 15–20 minutes
The Prototype Design Pattern is a creational design pattern that allows object creation by copying an existing object instead of creating a new instance from scratch.
This pattern improves performance when object creation is expensive, complex, or resource-intensive. Instead of repeatedly constructing objects, the system clones an existing object and modifies it as needed.
This guide explains Prototype Design Pattern from beginner to advanced level with simple explanations, real-world examples, cloning techniques, shallow vs deep copy concepts, performance benefits, and best practices.
What is Prototype Design Pattern
The Prototype pattern creates new objects by copying an existing object (prototype) instead of creating new objects using constructors.
In simple words, Prototype solves one main problem:
Create objects efficiently by cloning existing objects instead of building them from scratch.
The copied object can then be modified without affecting the original object.
Why Do We Need Prototype
Some objects require heavy computation, database calls, or complex initialization logic. Creating them repeatedly reduces performance.
Common Use Cases
Object creation is expensive
Large object initialization
Game character creation
Document templates
Database record duplication
UI component duplication
Instead of creating objects repeatedly, cloning improves speed and reduces resource usage.
How Prototype Works
The Prototype pattern works using three main steps:
Create a prototype object.
Clone the prototype when a new object is needed.
Modify the cloned object if required.
Java provides cloning support using the Cloneable interface and clone() method.
Key Components of Prototype Pattern
Prototype Interface or Class → declares cloning method.
Concrete Prototype → implements cloning logic.
Client → creates objects by cloning prototype.
Basic Prototype Implementation in Java
This example demonstrates a simple object cloning using the Cloneable interface.
package com.codekatha;
class Employee implements Cloneable {
int id;
String name;
Employee(int id, String name) {
this.id = id;
this.name = name;
}
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
Using Prototype Object
package com.codekatha;
public class PrototypeDemo {
public static void main(String[] args) throws Exception {
Employee e1 = new Employee(1, "Advait");
Employee e2 = (Employee) e1.clone();
System.out.println(e1.name);
System.out.println(e2.name);
}
}
The second object is created by copying the first object.
Shallow Copy vs Deep Copy
Understanding copying types is very important in Prototype pattern.
Shallow Copy
Shallow copy duplicates primitive fields but shares object references.
Fast performance
Shared references
Changes may affect original object
Example of Shallow Copy
class Address {
String city;
Address(String city) {
this.city = city;
}
}
class Person implements Cloneable {
String name;
Address address;
Person(String name, Address address) {
this.name = name;
this.address = address;
}
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
Both objects share the same Address reference.
Deep Copy
Deep copy duplicates both primitive fields and referenced objects.
Independent objects
Safe modification
Better data isolation
Example of Deep Copy
class Person implements Cloneable {
String name;
Address address;
Person(String name, Address address) {
this.name = name;
this.address = address;
}
protected Object clone() throws CloneNotSupportedException {
Address newAddress = new Address(address.city);
return new Person(name, newAddress);
}
}
Now both objects are completely independent.
Prototype Registry Pattern
Sometimes prototypes are stored in a registry and reused when needed.
package com.codekatha;
import java.util.HashMap;
import java.util.Map;
class Shape implements Cloneable {
String type;
Shape(String type) {
this.type = type;
}
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class ShapeRegistry {
private static Map<String, Shape> shapes = new HashMap<>();
static {
shapes.put("circle", new Shape("circle"));
shapes.put("square", new Shape("square"));
}
public static Shape getShape(String type)
throws CloneNotSupportedException {
return (Shape) shapes.get(type).clone();
}
}
The registry stores prototypes and returns cloned objects.
Real-World Analogy
Imagine filling a form.
Instead of writing everything again,
You photocopy an existing form and update details.
This is exactly how Prototype works.
Advantages of Prototype Pattern
Improves performance for heavy object creation
Reduces subclassing
Faster object creation
Simplifies complex object initialization
Provides flexibility in object creation
Disadvantages of Prototype Pattern
Cloning complex objects can be difficult
Deep copy implementation is complex
Requires careful handling of object references
Clone method may break encapsulation
Prototype vs Factory Pattern
Factory creates objects using constructors.
Prototype creates objects by copying existing objects.
Prototype is faster when creation is expensive.
Factory is easier for simple object creation.
Real-World Usage
Spring Framework bean cloning
Game development object creation
Document templates
UI component duplication
Caching objects
Database record copying
Interview Questions on Prototype Pattern
What is Prototype Design Pattern?
What is shallow vs deep copy?
Why use Cloneable interface?
When should we use Prototype over Factory?
What problems does Prototype solve?
Conclusion
The Prototype Design Pattern improves performance by cloning existing objects instead of creating new ones. It is especially useful when object creation is expensive or complex.
Understanding shallow and deep copy is essential for implementing Prototype correctly. When used properly, it provides faster object creation, better performance, and flexible design.
Comments
Post a Comment