The Prototype Design Pattern
The Prototype pattern creates new objects by copying an existing object, known as the ‘prototype’.
Use the Prototype pattern when you need to create an object that is similar to an existing object, especially if the cost of creating the object from scratch is more expensive or complex than duplicating an existing one.
- TypeCreational
- Time ComplexityO(1) Constant Time – The algorithm runs in constant time, irrespective of the input size. Operations like accessing an array element fall into this category.
- Efficiency8
-
Learning Effort
- Average4
- Conceptual4
- Debug and Maintain4
- Implementation3
- Prerequisites3
- Versatility5
Things you should know before you start
- Object Cloning Understanding of object cloning and object composition.
- Inheritance Understanding of inheritance in object-oriented programming.
Minimal Example
Creating multiple enemy AI objects in a game that share similar properties and behaviors.
class Enemy { clone() { return Object.create(this); } } const enemy1 = new Enemy(); const enemy2 = enemy1.clone(); console.log(enemy1 === enemy2); // Output: false
What’s happening in thie example
In this example, the Enemy class is a Prototype. We use the `clone` method to create a new Enemy object. The `clientCode` demonstrates that `enemy1` and `enemy2` are different instances but share the same properties and behaviors.
How the Prototype Works
The Prototype pattern involves creating new objects by copying an existing object, known as the ‘prototype’. This pattern is particularly useful when the construction of a new object is inefficient.
This pattern is beneficial when an object needs to be created that is a duplicate of an existing object, or when objects can share their state instead of defining their state from scratch.
Entities in the Prototype Design Pattern
-
PrototypeThe Prototype interface declares a method for cloning itself.
- Prototype has a one-to-many instantiates relationship with ConcretePrototype
What to watch out for when using or considering this design pattern.
Avoid using the Prototype pattern when the object’s construction process is not costly in terms of resources and complexity. Also, be cautious when cloning objects that have circular references or complex relationships.
Design patterns often confused with the Prototype Design Pattern
The following design patterns are often confused with the Prototype
for various resons.
If you’re exploring solutions, have a look through this list and see if
one of these might be a better fit for your problem.
-
Builder – The Builder pattern separates the construction of a complex object from its representation, allowing the same construction process to create different representations.
The Builder pattern is similar to the Prototype in the following ways.
- Both are creational design patterns.
- Both provide a way to construct an object.
- Prototype pattern creates a new object by copying an existing object, while Builder constructs a new object step by step.
- Builder allows for a more controlled and customizable object creation, whereas Prototype focuses on duplicating existing objects.
-
Factory Method – The Factory Method pattern provides an interface for creating an object in a super class but allows a subclass to alter the type of objects that will be created.
The Factory Method pattern is similar to the Prototype in the following ways.
- Both are creational design patterns.
- Both are concerned with object creation.
- Factory Method creates an object by calling a factory method, while Prototype creates an object by cloning an existing one.
- Prototype is better when object initialization is costly, whereas Factory Method is more straightforward for simple object creation.