Prototype Pattern
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Intent & Objective
Delegate the cloning process to the actual objects that are being cloned. The pattern declares a common interface for all objects that support cloning, allowing you to clone an object without coupling your code to its concrete class.
"Instead of inventing an entire baking formula from scratch, a baker copies a master brioche dough recipe and tweaks spices or chocolate chips."
"Replicating a base dress pattern and cutting it with slight variations in fabric, color, or hemline."
"Copying an existing calibrated project setup and dependencies to quickly bootstrap a new microservice repository."
"Duplicating a complex, calibrated laboratory test setup with minor adjustments for parallel experiments."
The Problem
Creating an exact copy of an object from the outside is often impossible because some fields are private. Also, the client code would have to know the object’s concrete class to instantiate the copy.
The Solution
Add a `clone()` method to the object’s interface. The object creates an instance of its own class and copies all fields (including private ones) into the new instance.
Key Participants & Architectural Roles
Code Samples (Traditional OOP vs Functional Programming)
Available in C++, Python, and JavaParadigm Contrast: Traditional OOP vs Functional / Data-Oriented
In OOP, Prototype requires explicit `clone()` implementations with deep-copy logic. In Functional Programming, all data structures are immutable by default, meaning every object can be shared freely without copying, and modifications produce new copies with structural sharing.
Advantages
- ✓ Allows cloning objects without coupling to their concrete classes
- ✓ Avoids repeated costly initialization code (e.g. heavy DB queries or parsing)
- ✓ Convenient alternative to inheritance when producing presets
Trade-offs
- ✗ Cloning complex objects that have circular references can be tricky
When to Apply
- → When your code shouldn’t depend on the concrete classes of objects that you need to copy
- → When you want to reduce the number of subclasses that only differ in how they initialize their objects
When to Avoid
- ! When objects are shallow or cheap to instantiate via standard constructors