Creational PatternsPrototype PatternEasy⏱️ ~2 min

What is the Prototype Pattern?

Definition
Prototype Pattern is a creational design pattern that creates new objects by cloning existing instances (prototypes) rather than constructing them from scratch. The pattern delegates the cloning responsibility to the objects being cloned.
Problem It Solves:

Creating complex objects from scratch can be expensive when initialization involves heavy computation, database calls, or deep object graphs. When you need multiple instances with similar configurations, reconstructing each one is wasteful. The Prototype pattern addresses this by allowing you to copy existing instances.

Real-World Analogy:

Think of photocopying a document. Instead of rewriting the entire document by hand each time you need a copy, you use a copier that creates duplicates. The original document is the prototype, and each copy is a clone.

Interview Tip: Always mention that cloning is more efficient when object creation is costly, but emphasize you must handle deep vs shallow copy correctly to avoid shared mutable state bugs.
When to Consider:

First, when creating an object is expensive (database queries, file loading, complex calculations). Second, when you need many objects that differ only slightly from each other. Third, when you want to avoid subclass explosion just for different initial states. However, if object creation is cheap and configuration is simple, direct instantiation with a factory is clearer.

💡 Key Takeaways
Creates objects by copying existing prototypes rather than using constructors
Delegates cloning logic to the object itself through a clone method
Useful when object creation is expensive or requires complex initialization
Requires careful handling of deep vs shallow copying for nested objects
Avoids tight coupling between client code and concrete classes
📌 Examples
1Document editor creating shapes by cloning template shapes
2Game engine spawning enemies from prototype configurations
3Form builder duplicating configured form fields
← Back to Prototype Pattern Overview
What is the Prototype Pattern? | Prototype Pattern - System Overflow