Introducing Prototype Design Pattern

Tram Ho


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 clone method.

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.

Cell division

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.


Basic Implementation

  1. Interface Prototype declares cloning methods. In most cases, only a single clone method is declared.
  2. 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.
  3. Client can make a copy of any object, as long as that object implment Interface prototype.

Fake code

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.


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.

Implementation method

  1. Create an interface prototype and declare a clone method in it. Or just add methods to all classes of the class hierarchy if any.
  2. 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 new .

  1. The replication method usually consists of only one line: running new with 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.
Share the news now

Source : Viblo