Prototype Design Pattern

Thilini Weerasinghe
5 min readMay 29, 2021

--

Source: https://refactoring.guru/

Prototype design pattern is one of the creational design pattern and it is used when the creation of the object is very time consuming, very complex and huge cost operation. In case of that we are creating an object using existing objects without using new keyword. In this article here we going to discuss about the prototype design pattern, its application in real world and how it is implemented for a real world scenarios.

✴️ Prototype Design Pattern ✴️

In prototype design pattern we create a copy of the existing objects and do customization as our own requirements. In here we don’t use new keywords to create an object. What we do is cloning an available or already created object. If the creating a new object is used more resources or time consuming process, using prototype design pattern is the best fit pattern. Here is a simple explanation for that.

Think that you want to create a copy from and existing object. What do you do is creating a new object from that class and assign same values for the all variables in previous object and make a copy. Another thing is that if we want to do a small modification to the existing object and create a new version of that, in here also we follow the same method mention in before and do the customization that you want. But think is it the right way. We duplicate the all tasks what we have done in previous time. Sometimes it may be time consuming and waste of the money. Think if the objects has lot of variables, then you have to assign same values for the all fields. To solve and make the easy development in such a situation, we use the prototype design pattern.

What the actual pattern does is it creates a common interface to support the all objects which was cloned. It enables you to clone the objects without making coupling with your current code. How it makes this possible is keeping a single clone method in the interface. What that method is, it creates an object from the currently available class with the all the values related to the available fields and share them with the new object. In here you have capability to copy the private fields also. Because mostly available programming languages are supporting for accessing private variables of the objects in same class. Such kind of object is known as the prototype. There are main three members in the prototype design pattern.

  • Prototype — It is the prototype elements of the original object
  • Prototype Registry — It can be used as the registry service to access all the prototypes using a simple parameters
  • Client — It is responsible for make use of registry service to access the prototype instances

Another important thing that should developer concerns is type of the copy that you want to create from the object. Basically it has two types.

  • Shallow copy: — If we used super.clone() command, it means we are creating a shallow copy. What does it means is it copies all the fields value from the actual object to new object. If those value fields are primitive type, then it copies as the primitive type. But if the field value is reference type, then it copies only the reference and not the referred object. In case of that modifying in one object will affects the values in other objects too as the both original and clone objects are refer to same object. It may led your application into unexpected behaviors.
  • Deep copy: — In such a scenario that describes in the previous way, the better way is to create a deep copy from the object. What it does is copy the dynamically allocated memory pointed to the reference type fields. In here original and copied objects are working independently without affecting each others.

Advantages of prototype design pattern 👍

Here are the advantages of using this design pattern in developing.

  • It simplifies the developing tasks while hiding its complexity to create new objects
  • It makes easy to create a new objects with varying field values
  • It makes easy for client to create a new object without worrying or concerning its type
  • It decreases the neediness of creating sub classes
  • It provides you with the capability to add or remove the objects at the runtime

Disadvantages of prototype design pattern 👎

  • It hides the actual implementation from the client
  • Difficult to create sub classes from the prototype classes
  • It can overkill the projects which haven’t an underlying emphasis on the extension of prototype chains
  • cloning the complex objects may be very tricky task

Application of Prototype design pattern

Here are the few application for the above design pattern.

  • It is suitable for development of the code, which shouldn’t depend on the concrete classes of the objects that you need to copy.
  • In the time that you needs to reduce the sub classes when it contains a small different in the initialization of their objects.

Implementation

Use case: Think you have to design a system or a laptop selling shop and in here there are different types of laptops available like Asus, Acer, Dell and Lenovo. The shops has different color laptop with different properties like its capacity, processor type and size etc. How you going to implement a system for that. If we follows traditional development method, we have to create separate object for each type of laptop and then do the modifications. If we follow a prototype design pattern, we need to create only one object and then clone it and do modifications. So let’s see how we implement this based on the prototype design pattern.

In the implementation we basically consider the two types of laptop called gaming laptop and office laptop.

  • First you have to create a Laptop class Laptop.java and it should implements a cloneable interface
  • Next creates a GamingLaptop class GamingLaptop.java and OfficeLaptop class OfficeLaptop.java to add the new features to them.
  • Then you have to create an enum to store the types of laptop
  • Now you have to create a registry to store the initial object creation information.
  • Now you need a main application to manage object modifications.

Now you can run the program and see the output how its create a different objects without using new keywords. Here are the output values for the above implementation.

Gaming Laptop {Generation=9th generation,Graphic=NAVIDIA GeForce GTX1650 Ti,Refresh Rate=144Hz}Gaming Laptop {Generation=11th generation,Graphic=NAVIDIA GeForce GTX1650 Ti,Refresh Rate=122Hz}OfficeLaptop {Security Feature=enable security features,Finger Print=true}OfficeLaptop {Security Feature=enable security features,Finger Print=false}

When looking at the above output you can see it creates two different objects which contains different features in the same objects. Hope now you have clear idea about the prototype design pattern to get more clarifications you can refer the following references.

Stay Safe & Learn New Things!!! 😃

References

--

--

Thilini Weerasinghe

Currently working as an Associate Software Engineer at Virtusa. Has completed degree in B.Sc (Hons) Computing & Information Systems. After all I am a Human...