Understanding Prototype Pattern

Jalitha Dewapura
Design Patterns with Java
4 min readMay 28, 2021

--

In some situations, creating a new object is very expensive. Because the application may be facing performance issues. Instead, the prototype design pattern encourages you to clone the existing object. Let’s see the importance of this design pattern.

Before getting into this article, I suggest you read my previous articles about design patterns. Because I walked through few important design patterns you must know.

Article series:- Design Patterns with Java

The Prototype pattern comes under the creational patterns. The specialty of creational patterns provides a way to create a new instance without using the typical new keyword. So. the mechanism inside the Prototype pattern is cloning objects from an existing object. Actually, this is not a very often used design pattern. But sometimes it is very useful.

For example, assume you create an application to an online shop. So, you may need to create thousands of customer objects at a time. Normally you have to create an instance of Customer class for each customer. If you use the prototype pattern, you can create a single instance of customer class and clone it. You may improve the application preference by saving the object creation time.

Before getting to implementation, let’s see two types of copying an object.

  • Shallow copy — Only fields of the primitive data type will be copied. All references are not copied.
  • Deep copy — Whole the object will be copied to another location.

The default clone() method is copied the object as a shallow copy. Therefore, you need to make sure that you will implement it with whether shallow copy or deep copy.

UML diagram for the previous example.

Here you can see The Customer class has two different sub-classes called RegularCustomer and LoyalCustomer. If we don’t use any design pattern, we have to create instances by new keyword.

ex:- RegularCustomer rc = new RegularCustomer();

But here we can clone the existing object. Let’s see the code now.

Customer,

The clone() method is a protected method. Therefore, we need to implement the Customer class by the Cloneable interface. This clone method is copying objects as shallow copies. If you need to make it as a deep copy, you have to override the clone() method.

LoyalCustomer class,

RegularCustomer class,

CustomerType enum,

I just implemented LoyalCustomer and RegularCustomer classes for demo purposes. LoyolCustomer class has three fields such as name, contactNo, discountRate. Let’s see how to create clone objects by Registry class.

Registry class,

This is the most important consideration in the Prototype pattern. Because we usually create the Customer class and its sub-classes to implement objects. There are four steps to implement this class.

  1. First, we need to create instances of all types of customers. For that, I implemented initialize() method.
  2. Then we need to implement the Registry class constructor. This initialize() method is called inside the constructor. So, when we create a Registry instance, it will immediately call the initialize() method.
  3. Then, we need to implement a collection of customers. It is used to map customerType and its default object.
  4. Finally, we need to implement getCustomer() method to return a clone of the particular object.

Application class,

Memory addresses of c1, c2, and c3 objects.

You can see that c1, c2, and c3 are three different instances of LoyalCustomer class. When you get the customer instance, that has default values. You need to implement it.

The output of the Application,

I have changed the c2 object but it will not affect to the c3 object.

Here it comes to the end of this article. I hope you understand the importance of the Prototype pattern. . Keep in touch. I will explain other design patterns as well. Happy learning.

--

--