Prototype is a creational design pattern that allows you to copy existing objects without making your code dependent on their classes.
Let’s say you have an object and you want to make a copy of it. How would you do this? First, you must create a new object of the same class. Then you have to copy the values of all the fields from the original object to the new one.
OK, now you think the problem is solved, right? But the real problem is that not all objects can be copied that way since some object fields may be private and not visible from outside the object.
It is not always possible to copy an object “from outside”.
There is another problem with the direct approach mentioned above. Since you must know the class of the object to replicate, your code will depend on that class. If you’re OK with that dependency, there’s another problem. Sometimes you only know the interface that the object implements, but don’t know its specific class, for example, when a parameter in a method accepts any object that implements a certain interface.
Design pattern Prototype delegates replication to actual objects that are the object of replication. The design pattern declares an interface that is common to all replication objects. This interface allows you to copy an object without having to couple your code with the object’s class. Usually, such an interface contains only a single
The implementation of the
clone method is very similar in all classes. This method creates an object of the current class and converts all the field values of the old object to a new one. You can even copy private fields because most programming languages allow objects to access private fields of other objects of the same class.
An object that supports cloning is called a prototype . When your objects have dozens of fields and hundreds of combinations of fields, duplicating them can be an alternative to subclassing.
Pre-made prototypes can be alternatives to subclassing
How this design pattern works is as follows: You create a collection of objects, configured in a variety of ways. When you need an object like the one you configured, you just need to copy a prototype instead of building a new object from scratch.
Real world example
In the real world, prototypes are used to perform tests before they begin mass production of a product. In this case, however, the prototypes are not involved in any actual manufacturing process, instead playing a passive role.
Since industrial prototypes do not actually replicate themselves, a closer example to this Design pattern is infectious mitosis. After mitosis, an identical pair of cells is formed. The stem cell acts as a prototype and has an active role in replication.
- Interface Prototype declares cloning methods. In most cases, only a single
clonemethod is declared.
- Class Concrete Prototype implements the clone method. In addition to copying the original object’s data to the copy, this method can also handle some boundary cases of the replication process involving copying linked Objects, debugging recursive dependencies, etc.
- Client can make a copy of any object, as long as that object implment Interface prototype.
In this example, the Prototype design pattern allows us to make exact copies of shape objects without having to couple code with their classes.
Clone a set of objects that belong to a class hierarchy.
All form classes implement the same interface. This interface provides a cloning method. A child class can call the copy method of the parent class before copying the values of its own fields into the result object.
// Base prototype.
abstract class Shape is
field X: int
field Y: int
field color: string
// A regular constructor.
constructor Shape() is
// The prototype constructor. A fresh object is initialized
// with values from the existing object.
constructor Shape(source: Shape) is
this.X = source.X
this.Y = source.Y
this.color = source.color
// The clone operation returns one of the Shape subclasses.
abstract method clone():Shape
// Concrete prototype. The cloning method creates a new object
// and passes it to the constructor. Until the constructor is
// finished, it has a reference to a fresh clone. Therefore,
// nobody has access to a partly-built clone. This keeps the
// cloning result consistent.
class Rectangle extends Shape is
field width: int
field height: int
constructor Rectangle(source: Rectangle) is
// A parent constructor call is needed to copy private
// fields defined in the parent class.
this.width = source.width
this.height = source.height
method clone():Shape is
return new Rectangle(this)
class Circle extends Shape is
field radius: int
constructor Circle(source: Circle) is
this.radius = source.radius
method clone():Shape is
return new Circle(this)
// Somewhere in the client code.
class Application is
field shapes: array of Shape
constructor Application() is
Circle circle = new Circle()
circle.X = 10
circle.Y = 10
circle.radius = 20
Circle anotherCircle = circle.clone()
// The `anotherCircle` variable contains an exact copy
// of the `circle` object.
Rectangle rectangle = new Rectangle()
rectangle.width = 10
rectangle.height = 20
method businessLogic() is
// Prototype rocks because it lets you produce a copy of
// an object without knowing anything about its type.
Array shapesCopy = new Array of Shapes.
// For instance, we don't know the exact elements in the
// shapes array. All we know is that they are all
// shapes. But thanks to polymorphism, when we call the
// `clone` method on a shape the program checks its real
// class and runs the appropriate clone method defined
// in that class. That's why we get proper clones
// instead of a set of simple Shape objects.
foreach (s in shapes) do
// The `shapesCopy` array contains exact copies of the
// `shape` array's children.
1. Use the design pattern prototype when you don’t want the code to depend on the concrete classes of specific objects that you need to copy. This happens a lot when your code works with objects that are passed from 3rd party code through the interface. You don’t know the specific classes of these objects, so you can’t depend on them.
The design pattern prototype provides the client code with a common interface so you can work with all cloning objects. This interface makes the client code independent of the specific classes of the objects it copies.
2. Use this pattern when you want to reduce the number of subclasses that differ only in initialization. Someone may have created these subclasses to be able to create objects with a specific configuration.
The design pattern prototype allows you to use a set of pre-made objects, configured in many different ways, to prototype.
Instead of instantiating a subclass that fits some configuration, the client can simply find a suitable prototype and duplicate it.
- Create an interface prototype and declare a
clonemethod in it. Or just add methods to all classes of the class hierarchy if any.
- A class prototype must define an alternative constructor that takes an object of that class as an argument. The constructor must copy the values of all the fields defined in the class from the object passed into the newly created instance. If you are modifying a child class, you must call the parent constructor to allow the parent class to handle the duplication of its private fields.
If the programming language you use doesn’t support method overloading, you can define a special method to copy object data. Constructor is a good place to do this because it provides the resulting object as soon as you call operator
- The replication method usually consists of only one line: running
newwith the constructor version used to create the prototype. Note that every class must explicitly override the replication method and use its own class name when calling
new. If not, the replication method can create an object of the parent class.
Advantages and disadvantages
✔Can copy objects without a couple with their specific classes.
✔No need to repeatedly initialize code
✔ You can create complex objects more conveniently.
✔Is an alternative to inheritance when dealing with configuration presets for complex objects.
Copying complex objects with circular dependencies can be very difficult.
Relationship with other patterns
- Many designs start using the Factory Method (less complicated and more customizable through subclasses) and evolve towards Abstract Factory, Prototype, or Builder (more flexible but more complex).
- Abstract Factory classes are usually based on a set of Abstract Factory s, but you can also use Prototype to compose methods on these classes.
- Prototype can be useful when you need to keep copies of Command in history.
- Designs that use multiple Composite and Decorators can often be complemented by using Prototype. Applying a pattern allows you to copy complex structures instead of rebuilding them from scratch.
- Prototype is not based on inheritance, so it has no disadvantages of inheritance. Prototype, on the other hand, requires complex replication object initialization. The Factory Method is based on inheritance but does not require an initialization step.
- Sometimes Prototype can be a simpler alternative to Memento. This works if the object or state you want to store in history is quite simple and has no links to external resources or links that are easy to re-establish.
- Abstract Factories, Builders and Prototypes can all implement as Singleton.