Design Pattern (Part 03)— Prototype Design Pattern!

Sometimes the object created in java making too expensive and resource-intensive. For example, in a render-car application, you need to show more than ten-thousands vehicles hence that create more than ten thousand objects is too expensive. This is a massive problem when doing a large industrial project. Java provides the easiest solution which is Prototype patterns.

What is a Prototype Design Pattern?

Prototype patterns come under the creational pattern to provides one of the best ways to create an object. It also called a GoF design pattern. The intention is to a prototype design pattern is used to provide a clone method.

Prototype Pattern says that cloning of an existing object instead of creating a new one and can also be customized as per the requirement.

The cloning process objective is to avoid object creation using an “New” keyword. (No need to use a new keyword to create the object) hence that avoiding a lot of overheads and performance issues when the object is created. In Java, One of the best available ways to create objects from existing objects is by using the clone() method of Cloneable interface.

Usually implemented happened by a cloneable interface . It’s coming up after the java 1. version. When you have to clone the object, we have to be mindful of the Shallow Copy and Deep Copy.

Shallow Copy

We create a new object by copying all the fields of the original object(Copy the first level references of the new object). When the data is a reference type, only the reference is copied but not the referenced object itself. Because when copy the references from the original and modify something to change it, It will affect the original object.

Deep Copy

Copying all the fields of the original object. The object contains an object field so each object field will be copied. It clearly says to go inside the object and copy the object and each value.

// Shallow Copy: only top-level objects are duplicated

return (IEmployee)MemberwiseClone();

// Deep Copy: all objects are duplicated

return (IEmployee)this.Clone();

Clone is the simplest approach to implement a prototype pattern. There are two types of implementation that happened when using a clone,

● Basic Implementation

● Prototype Registry Implementation

In the Prototype Registry implementation of the pattern, we create a registry class to cache the objects for prototyping. In other words, we use a prototype collection and management service or class which makes all the prototype accessible and available for prototyping. The purpose of the cache registry is to maintain a pool of prototype-ready objects in memory.

1.The clients can get new objects without knowing which type of object they will be
2. It hides the complexities of creating objects.
3. Add or remove objects at runtime.
4. Save total operating cost by minimizing costly external API calls.

Prototype Design Pattern Implementation

  1. Create a Product class that implements the cloneable interface. List all the parameters and create a setter getter for them. Create appropriate constructors.
  2. If parameters contain all immutable fields, there is no need to override the clone() method.
  3. If any parameter is mutable, We need to override the clone() method and write appropriate deep cloning logic.
  4. The clone method will return a new object of the Product class with all field values as per the original product object.

Real-world Example

In this scenario, imagine we have different kinds of Flights in the airport as Malaysian Airways, Emirates Airways, Qatar Airways. All these three flight types are subclasses of the class Flight that implements the base functionalities and base attributes of different flights, such as numberOfSeats, type, businessclass, FuelType, engineCapacity. Registry class is used for when a user asks for flight type, they can pass that flight type they want and get a clone of the pre-created instance of Malaysian Airways, Emirates Airways, Qatar Airways classes. After obtaining cloned instance, if we like we can set different values to each class attributes.

If you want a clear explanation about this code please refer to my GitHub link which I want to mention below:


Software Engineer at Virtusa (Pvt) Ltd.