Prototype Pattern Tutorial
The Prototype Design Pattern is a creational design pattern that allows you to create new objects by cloning existing instances rather than creating them from scratch. This pattern is particularly useful when object creation is expensive or complex.
What is the Prototype Pattern?
The Prototype pattern involves creating objects by cloning a prototypical instance. Instead of instantiating a class directly, you copy an existing object that serves as a prototype.
Key Concepts:
- Cloning: Creating a copy of an existing object
- Prototype: The original object that serves as a template
- Registry: Optional component to manage different prototypes
When to Use the Prototype Pattern
Use the Prototype pattern when:
- Object creation is expensive (requires database calls, network requests, complex calculations)
- You need many similar objects with slight variations
- The system should be independent of how objects are created
- Classes to instantiate are specified at runtime
- You want to avoid building a class hierarchy of factories
Structure and Components
┌─────────────────┐
│ Client │
└─────────────────┘
│
▼
┌─────────────────┐
│ Prototype │ ◄─────────────┐
│ (Interface) │ │
│ + clone() │ │
└─────────────────┘ │
△ │
│ │
┌─────────────────┐ │
│ ConcretePrototype│──────────────┘
│ + clone() │
│ + operation() │
└─────────────────┘
Components:
- Prototype: Interface declaring the clone method
- ConcretePrototype: Implements the clone method
- Client: Creates new objects by asking prototypes to clone themselves
🧩 Scenario
Lets take an example of a Document Management System which creates a template for different Document types eg. Legal Document/Resume Template and a Invoice Format. We will see how a Prototype design pattern can help creating different type easily.
In prototype pattern, instead of creating a new document from scratch every time, you:
- Copy a prototype template.
- Customize it.
✅ Benefits
- Avoids expensive object creation.
- Simplifies code — no need to know exact classes.
- Cloning is faster than re-instantiating.
1. Create Abstract Document Prototype
We create an interface for Document Prototype. Every document can have a certain metadata like Title and Content.
class Document:
def __init__(self, title, content):
self.title = title
self.content = content
def clone(self):
return copy.deepcopy(self)
def display(self):
print(f"Title: {self.title}")
print(f"Content: {self.content}")
2. Create Concrete Prototypes
# Prototypes
legal_template = Document("Legal Agreement", "Terms and conditions...")
resume_template = Document("Resume", "Name: \nExperience: \nSkills:")
3. Create New Document from cloning
# Client clones the resume
resume_john = resume_template.clone()
resume_vikalp.content = "Name: John Doe\nExperience: 10 years\nSkills: Python, AWS"
# Client clones the legal template
nda_doc = legal_template.clone()
nda_doc.content = "Terms and conditions for NDA between John and Worldkosh..."
# Display
resume_john.display()
print("------")
nda_doc.display()
🤔 Without Prototype?
You’d create each document from scratch:
doc1 = Document("Resume", "Name: ...")
doc2 = Document("Resume", "Name: ...")
Duplicate logic, error-prone, inefficient
🛑 Problem: Every time you instantiate, you repeat template logic.
✅ With prototype: Clone and modify only what you need.