Understanding Prototype Design Pattern

Photo by Amélie Mourichon on Unsplash

Prototype is a Creational design pattern, that lets you clone existing objects without making newer objects from the same classes. The main reason for that, some object creations may be very costly (time and resource consuming), so instead of making those objects from the scratch we can clone them from existing objects when we need one. Therefore, the existing object acts as a prototype and contains the state of the object. However, we can change properties of that newly cloned object whenever it required. Because of that, the Prototype approach will save you processing time and resources of your program.

As I mentioned above, the Prototype pattern mostly used when we need to create several instances of a class and each instance exactly same to each other or has minor differences. In the implementation process we used a method named as “clone()” and it’s the simplest approach to implement the Prototype pattern. However, based on your use case you can implement your own clone() method.

When you implement the Prototype pattern you must be aware about the copy of object you need, whether it’s a Shallow copy or a Deep copy.

In shallow copy, only attributes of primitive data type are copied from an object, without its reference. However, in the deep copy, it will copy each and everything from the object including its references.

Use case of Prototype Design Pattern

In this chapter, I will explain how to apply Prototype Design Pattern with a real-world example using Java.

The following application is built for a tour company, that offer different tour packages such as, Tour Sigiriya, Honeymoon, Hill Country tour, and so on. Assume if we don’t use Prototype Design Pattern, we may need to create new object (a Tour object) each time when it needs to create these type packages with few differences. But since we use Prototype Design Pattern in this application it’s not necessary.

The following illustrate will give you a general overview of this implementation

Image: General overview of the implementation

As the first step I have created an abstract class named as “Tour” that contains 3 attributes with getters and setters for each one. Inside the “Tour” class I have implemented the “clone()” method as a result of implementing “Cloneable” interface.

Then I have created 2 classes named as “TourSigiriya” and “HoneyMoon” that extends the “Tour” class. As a result, those classes will inherit all getters and setters from the “Tour” class. Additionally, these two classes also contain 2 attributes, that specific to each class.

After that I have created the “TourReg” class. In this class I have implemented a constructor, and whenever we create the instance of this class it will invoke the “initializingTours()” method.

When that method gets invoked, it will create separate instances from both of those classes (TourSigiriya and HoneyMoon). Moreover, I have used a HashMap to keep those instances.

At last I have created the “TourApplication” class.

Output:

TourSigiriya{destination='Sigiriya', accommodation=true}
TourSigiriya{destination='Sigiriya', accommodation=false}
TourSigiriya{destination='Sigiriya', accommodation=true}

If you are interested, you can use the following GitHub link to see my complete implementation of Prototype Design Pattern.

So, this is the end of my article and I hope you enjoyed it. Happy Coding👨‍💻.

Associate Software Engineer at Virtusa