Prototype Design Pattern

Rasika Weragoda
4 min readSep 19, 2020

--

I hope you guys read my introduction article of design pattern in java. If not then the link is available here Design Pattern in Java. This article does not totally depend on the previous article (Design Pattern in Java). But it has some basic understanding of the background of design patterns. Just remember the Prototype design pattern belongs to the category of creational design pattern

When an interviewer might ask you that sometimes creating objects might be costly. So what is the solution for that in java?. In that case, You have some idea about java, but you don’t have proper knowledge about design patterns. So what did you say? if I am being interviewed, I would say just a word `cloneable interface`. The answer is partially correct but there is more. Suppose that you have some background in design pattern, Then you might say prototype interface. Then the interviewer really satisfies. But actually there is more 🤷‍♂️. I will explain that later of the article.

So what is actually prototype design pattern? The first place in the prototype design pattern what we do is creating the prototypical instance in the registry. when we need another object what we basically do is, clone the prototypical instance from that registry and get a unique instance. The prototypical instance is just the original instance that is created using the new keyword. We use new keyword only for creating a prototypical instance. The registry is just an interface (not java interface though 😜) using that we can retrieve the prototypical instance. Then How we create other objects without using the new keyword? 🤔 For avoiding keyword new, we implement the cloneable interface. since we use the cloneable interface to create copies, each instance still unique. For creating these copies, we have two architectures

  1. Shallow copy — copy the immediate properties. (we have one object and two separate pointers that reference to one object)
  2. Deep copy — copy the object references (we have two pointers and two objects each pointer reference its particular object).

The shallow copy is a dangerous copy because of changing one copy may reflect another copy. Then You might ask how do we choose which architecture to implement? 🤦‍♂️ It is according to the requirements. Typically what developers do is, they choose Shallow copy If an object has only primitive fields or immutable objects. When the object has references to other mutable objects, then either choose a shallow copy or a deep copy. Alright, lots of details now let’s get hand dirty. 😍

Scenario

Suppose we are in school and Teachers, Students and administrators are members of the school right... So you have to create a dashboard which is displaying 500 students at once. So creating 500 instances kind of costly creation. Instead of roughly creating an instance, we can do a cloning mechanism. So at this point, we have to think about using the prototype design pattern.

Implementation

First, we have to create an abstract class called SchoolMember and which is implemented by the cloneable interface.

Here is nothing but setter and getter method and there is an abstract method called cloneSchoolMember(). This method is implemented by its subclasses. According to our scenario, we have 3 subclasses. They are Student, Teacher and Administrator. But let’s consider only the Student classes. The Student class is as follow.

So here we implemented the cloneSchoolMember(). Let’s look at it. since we inherited the cloneable interface to SchoolMember abstract class, we can use the clone method of its subclasses. That is why we use super.clone() statement here. This will return the Shallow copy of the Student object at run time.

So the final piece is creating the Registry Class. Before that create Enum class that contains the SchoolMember types

Now let’s create the final piece. So the Registry is class using that we can get the clone of our types.

So dive into one by one. In this Registry class, we have HashMap which holds the prototypical instances. At the first place we initialize the static loadPrototypicalInstance() function. This function is creating its first prototypical instances and put them into the HashMap data structure. The getCloneSchoolMember() is responsible for getting the copy of the object as their type. Here we only have student type. you can implement more than that. So this is how we clone the stuffs.

So you might have a question like why you implement the shallow copy instead of deep copy. well, Because we use immutable data types (String). That is why I used a shallow copy. If you indent to implement this design pattern, you cannot implement the prototype design pattern purely. What I mean by that You need to implement this with the use of another design pattern. If you see closely, here also we used the factory method cloneSchoolMember() just like that. But It is not the factory design pattern. factory design pattern always gets the fresh instances. on the other hand prototype design pattern always get the cloned instances.

At very being of this article, I said there is more solution for avoiding costly creation objects. Well, there is a design pattern called ‘Object Pool’. It is a pool that creates objects and this object pool is responsible for assign pool instance that is already being instantiated when the user asks it rather than creating a new object. This pool can be scalable and most of the time it used to boost the performance by caching.

See you in another article ✌

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Rasika Weragoda
Rasika Weragoda

No responses yet

Write a response