Overview Of Prototype Desing Pattern

Arshad Suraj
Geek Culture
Published in
5 min readMay 24, 2021
Image from — google

What is Prototype Design pattern?

prototypes design pattern allows you to create objects by cloning an existing object instead of creating a new object from scratch. This pattern is used when the process of object creation is costly. when cloning, the newly copied object contains the same characteristics as its source object. After cloning, we can change the values of the new object’s properties as required. This pattern comes under a creational pattern.

For easier understanding, Assume that a database operation should be done in order to create an object. This database call is both time-consuming and costly. we need to create multiple objects. As a result, when we create a new object, each time a database call will occur, this will give a performance flop, right? As a result, initially, we create one object and clone it each time when we required a new object. This will cut down the database calls for each object creation.

Implementation

Class Diagram of Prototype Design Pattern

Things to Keep in Mind:

  • The class of the object we are trying to clone or its one of parent class must implement Runnable Interface, else it will throw CloneNotSupportedException.
  • The class which implements Runnable Interface should override clone() method. the body of the clone() method should contain return super.clone() .
  • There should be a registry class that contains one object of each source object. The Register class must contain a method that will clone a new object from the source object and return that new object.

Let’s implement the code to see how the Prototype design pattern works.

Assume you’re in a clothing store that offers T-shirts and denim. Each time a customer places an order, a new Dress object should be created. If creating a dress object from scratch each time is too expensive and time-consuming, we can clone a source object instead. Take a look at the code below.

Above is a parent class which implements Runnable interface and override clone() method.

Above are the child classes of the parent class which we implemented earlier.

Above is the Registry class which is responsible for storing one object from each source object and returning the newly duplicated object.

Below is the main class.

The output of the above codes are below,

Output 1
Output 2

Therefore, by using prototype design pattern, to create a object, we can clone a source object instead of creating a new object when object creation really consume much resources.

Advantage of Prototype Pattern

  • By cloning, it cut down the resource consumption of object creation.
  • Clients can get new objects, without knowing which type it belongs to.
  • It allows you to add or remove objects at runtime.
  • It reduces the need for sub-classing.

When to use Prototype Pattern

  • When the process of making an object is expensive or takes a long time.
  • When the client application must be unaware of the creation of an object.
  • When you need to keep the number of classes in your application to a minimum level.
  • When the classes are instantiated at runtime.

Note:

In Prototype design pattern, since it uses default implementation of clone method we get shallow copy of source object.

What is shallow copy?

Shallow copy means, it creates a new instance and copies all the fields of the source object to it, then returns it as an object type.

If the source object contains reference type variable, the cloned object’s reference type variable also refers to the same object to which the source object’s reference type variable refers. Only the object references are copied in shallow copy, not the referred objects are also cloned.

As a result, there’s a chance that changing the value of a reference variable in a cloned object, will affect the source object as well. See the code below,

Output

the output of the shallow copy

We can see that changing the id of ‘d1’ has no effect on ‘d’ because they are two separate objects. However, if the ‘e.name’ of ‘d1’ is changed, the ‘e.name’ of ‘d’ is also changed because they both refer to the same ‘Employee’ object. because ‘d1’ is a shallow copy of ‘d’.

We can customize and have a deep copy to avoid these kinds of issues.

What is a Deep copy?

When we require a fully isolated duplicate of an object, we refer to it as a deep copy. When we require a deep copy of an object, we must implement it according to our requirements. So, in order to perform a deep copy, we must ensure that all of the member classes also have been cloned.

Note: There is no difference between shallow and deep copy in Java if source objects only contain primitive type fields or immutable objects.

Keep Learning ❤️

--

--