Prototype Design Pattern
The Prototype Design Pattern is one of the Creational Design Patterns. It allows new objects to be created by cloning an existing object instead of constructing a new one from scratch.
In many applications, creating an object may be expensive because it involves loading files, initializing complex data, or performing lengthy computations. Rather than repeating this initialization every time, the Prototype pattern creates one fully configured object (called the prototype) and produces new objects by copying it.
To implement this pattern, a base interface declares a clone() function, and every concrete class provides its own implementation that returns a copy of itself.
This document demonstrates two practical examples of implementing the Prototype Design Pattern in C++.
Example 1: Game Monster Spawner
This example demonstrates how a game engine can create multiple monsters by cloning a master prototype instead of rebuilding every monster from scratch.
Program
Explanation
The Zombie object acts as the master prototype.
Instead of creating every monster manually, the program simply calls the clone() function. Internally, the clone creates a copy of the current object using the copy constructor.
Each cloned object is completely independent. Changing the health of one zombie does not affect the master prototype or any other cloned instance.
This approach is useful when object initialization is expensive or when many similar objects need to be created quickly.
Example 2: Business Document Template
This example demonstrates creating customized invoices by cloning a standard document template.
Program
Explanation
The BusinessInvoice class represents a reusable invoice template.
Instead of constructing every invoice individually, the application clones the template and customizes only the client-specific information.
The company header and payment terms are copied automatically, reducing repetitive initialization code while ensuring every invoice begins with the same standard layout.
This technique is widely used in applications that generate many similar objects with only minor differences.
Characteristics of the Prototype Design Pattern
| Property | Description |
|---|---|
| Pattern Type | Creational Design Pattern |
| Core Principle | Create new objects by cloning existing ones instead of constructing them from scratch. |
| Implementation | A prototype interface declares a clone() method that each concrete class overrides. |
| Primary Benefit | Reduces expensive object initialization and simplifies object creation. |
| Object Independence | Every cloned object is a separate instance and can be modified independently. |
| Common Applications | Game object spawning, document templates, graphical editors, configuration profiles, and reusable object blueprints. |
Conclusion
The Prototype Design Pattern provides an efficient way to duplicate fully initialized objects while hiding the complexity of object creation. By allowing each object to clone itself, it reduces construction overhead, keeps client code independent of concrete classes, and makes creating similar objects both fast and maintainable. As demonstrated in the Monster Spawner and Business Invoice examples, the Prototype pattern is especially valuable when object creation is costly or when many objects share the same initial configuration.