PROTOTYPE DESIGN PATTERN

Hansini Rupasinghe
6 min readMay 24, 2021

PROTOTYPE DESIGN PATTERN

=======================================

❉ The Prototype Design Pattern is one of the creational design patterns in which the main objective is to avoid creating new instances from scratch because there are situations where creating a new object can be very expensive.

❉ This design pattern encourages to clone an existing object.

❉ Cloning from original instance will only be needed after first instance is created and registered. Creation of new instances will not require.

❉ The existing initial object that we created at very first time contains the state of the object and it will act as the prototype. The newly cloned object is a copy of the original object.

❉ We can change values in the newly copied object whenever required.

❉ This Prototype Design Pattern would be ideal in situations where the object creation process is costly and if there are lots of things to be done when creating a new object.

Clone can be known as the simplest way to implement prototype pattern.

❉ In Java, we usually implement these from Cloneable Interface since this needs to clone the objects.

❉ There are some weaknesses in Cloneable Interface too.

➢ It always return object type.

➢ Does not know about Generics. (This is because Generics did not exist at the time of implementing Cloneable )

❉ However, we need to be careful when implementing Prototype design pattern because we need to realize whether we need a Shallow Copy or a Deep Copy in your architecture when cloning.

  • Shallow Copy — In Shallow copy, an object is created by copying all the fields of the original object. You just copy in the first level objects and references to the new object.

Shallow Copy is quite dangerous sometimes because;

Assume there is one object and you take it. If you copied some reference to your new object and modified those references in new object, that could affect your original object too.

  • Deep Copy — In Deep copy, an object is created by copying all the fields of the original object. You clone each and every object and value to the new object.

❗️ ❗️ ❗️ We need to be careful when using Shallow and Deep copies.

UML of Prototype Design Pattern

Ref: https://images.app.goo.gl/53Qvc69nkBaBaR928

Implementation of Prototype Design Pattern

❉ There are two approaches that we can use in order to implement the Prototype design pattern.

  1. Basic Implementation
Ref: https://images.app.goo.gl/HQnBfsvgfjrt7xGq6

2. Prototype Registry Implementation

Ref: https://images.app.goo.gl/J2eTgmReG5DMZm4k6

❉ In this Prototype Registry implementation, a registry classis created to aid caching of the objects for prototyping.

❉ Prototype collection and management service or class helps to make all the prototypes accessible and available to prototype.

Participants of the Prototype Design

  1. Prototype

💠This is the actual object’s prototype.

2. Prototype registry

💠 Prototype registry is used as a service of registry that keeps all prototypes accessible with the use of simple string parameters.

3. Client

💠 Registry service will be used by clients in order to to access prototype instances.

Usage of Prototype Design Pattern

The Prototype pattern is used in the below mentioned instances when;

❉ We have to create a number of instances of a class and all those instances have almost the same state.

❉ The creation of an object is expensive or complicated.

❉ A prototypical instance determines the type of objects to create

❉ The number of classes should be maintained application minimum.

❉ The client application does not need to know object creation and representation.

❉ The creation of an object is resource intensive.

Real World Example for Prototype Design Pattern

📜 There is a system that has created for a university that manages the records of the people such as employees, students who are associated with it. The system needs to keep track of their details through the system.

Let us take a class as ‘person’ to denote people associated with the university. Therefore;

Person consists sub classes such as Students and Employees.

Person class consists of attributes such as NIC, Name, DateOfBirth

Apart from the common attributes that the person class has, Student has a Student ID where the Employee Class has an Employee ID and salary.

Let us see an UML diagram for the above scenario:

Let us implement this using Java.

💫 Person Class

This is an abstract parent class that implements Cloneable.

💫 Student Class

NIC, Name and Dob is already there apart from studenID since this inherits Person class.

💫 Employee Class

NIC, Name and Dob is already there apard from EmployeeID and Salary since this inherits Person class.

💫 PersonType Enum

💫 PersonRegistry Class

This is the place where you create objects initially.

“New” keyword is used only when you create initial objects.

💫 Application Class

This is the class that includes main method.

OUTPUT

Displayed below is the output we will be getting once the above program is executed.

Advantages of Prototype Design Pattern

♻️ Hides object creation complexity.

♻️ Decreases the need for sub-classing.

♻️ Save expensive resources and time.

♻️ Allows to add or remove objects at runtime.

♻️ Produces complex objects more conveniently.

♻️ Enhances the system performance.

♻️ Easy to Clone objects without coupling to their concrete classes.

Disadvantages of Prototype Design Pattern

♨️ Management of cloned objects will be hard sometimes.

♨️ Implementation of the clone() operation for each subclass of prototype may be difficult carry out.

♨️ Might be overkilling for a project which uses very few objects.

Important Facts to be remembered

🔔 Rather than going through an expensive instantiation process, we just have a template object already created in the registry and clone when needed.

🔔 When you have deep copy, you need to implement your own clone method.

🔔 ‘New’ keyword is used only when you create initial objects.

🔔 It does not need a class-only an object.

🔔 Prototype design pattern is ideal to use because you can avoid lots of overheads and performance issues.

References

--

--