Introduction
Sometimes creating an object is not just new MyClass():
It may require many configuration parameters.
It may need an expensive setup (loading data, computing defaults).
Different parts of the system may need slightly different variants of a “template” object.
The Prototype Design Pattern helps by letting you create new objects by cloning an existing instance, instead of rebuilding everything from scratch with a complex constructor or factory logic.
This article explains prototypes in simple terms, walks through a C++ example, and shows when they can make your low-level designs cleaner and more flexible.
Intent of the Prototype pattern
The Prototype pattern has one main idea:
Create new objects by copying an existing object (a prototype), instead of constructing them directly from scratch.Key points:
A “prototype” object is fully initialized and configured.
When you need a similar object, you clone the prototype.
Each concrete class knows how to clone itself (deep copy when needed).
This is helpful when:
Construction is expensive or complex.
You want many similar objects with small differences.
You want to avoid large, complicated constructors or factories full of conditional logic.
Basic structure: clone() on a base type
Typical Prototype pattern structure:
A base interface or abstract class declares a
clone()Concrete subclasses implement
clone()to return a copy of themselves.Client code works with the base type and uses
clone()to get new instances.
Step 1: Define an abstract clone() method
Step 2: Implement concrete prototypes
Step 3: Use clones instead of new + configuration
Each clone() Call returns a new object with the same internal state as the prototype at that moment.
A more realistic example: pre‑configured documents
Imagine a system that generates documents with lots of settings:
Page size, margins, header/footer, fonts, language, etc.
Setting all this from scratch every time could involve many parameters.
With Prototype, you can define a base template and clone it.
Document prototype interface
Concrete document with configuration
Using a template prototype
Here:
baseTemplateIs set up once with layout details.Each new document starts as a clone of that template and then gets small changes (like the title).
This avoids repeating complex setup logic everywhere.
Prototype + Factory: registry of prototypes
A prototype is often used together with a simple factory:
Instead of the factory constructing complex objects from scratch,
It can keep a registry of prototype objects and return clones.
Conceptually:
Then you can:
Register a
ConcretePrototypeAandConcretePrototypeB.Ask
factory.create("A")orfactory.create("B")to get clones.
This keeps complex configuration in one place (the prototypes) while the factory provides a clean creation interface.
When to use Prototype (and when not to)
A prototype is most useful when:
Object creation is expensive or complex
The object has many configuration steps, default values, or heavy initialization.
You need many similar objects.
Templates, default configurations, pre‑built “starting points” for further customization.
You want to avoid large constructor or factory parameter lists
Cloning an already configured object can be simpler than passing 10 parameters.A prototype is less useful when:
Objects are cheap and simple to create.
A plain
newwith a small constructor is fine.
You don’t really need templates or copies.
Each object is unique and created with straightforward parameters.
Copying is complicated or dangerous (e.g., deep copies with shared resources).
In such cases, you must be very careful when implementing.
As always, KISS and YAGNI apply:
Don’t introduce Prototype just because it’s a pattern; introduce it when it clearly simplifies object creation in your context.
Summary
The Prototype Design Pattern is a creational pattern that:
Creates new objects by cloning existing, fully initialized objects, rather than constructing everything from scratch.
Encapsulates copying logic in each concrete class’s
clone()method.Works well when you have complex setups, templates, or many similar objects with small differences.
You saw:
A simple
Prototypeinterface withclone()in C++.Concrete prototypes that implement
clone()Using copy construction.A realistic “document template” example where new documents are created by cloning a base template.
How a prototype can be combined with a factory that returns clones from a prototype registry.
When is a prototype helpful, and when are simple constructors or factories enough.