What is Prototype Design Pattern?

Introduction

The prototype design pattern falls under the creational design pattern category. It uses the object re-use principle to gain the benefit of solving the problem. This pattern is most appropriate when object creations are costly and time-consuming. The already created clone helps to save the cost and the construction time of the required object.

GOF definition for prototype design pattern,

“Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype”

Prototype interface enables the object cloning via the concrete implementation. A class has to implement the prototype interface to achieve the cloning ability. Instead of creating the object from the scratch every time, the pattern provides a way to create exact copies of the original object instance to modify and use it as required.

How does Prototype Design Pattern work?

This pattern gets the help of the cloning principle to build the design. Pattern use an already created object instance to create the clone. Most appropriate way to create a copy of the existing object is to extend the ‘Cloneable’ interface in your own prototype interface. Then we can override the clone() method to create the copies of the object as required using the concrete class.

There are two approaches to copy the object using Cloneable interface:

  1. Directly use the cloning functionality of the object from its’ super object class. Create the raw copy of the object and modify it as per your requirement. This is the most independent approach where you can use the clone object at any level and do anything to take the required output.
  2. Another approach is to tweak the overriding clone method itself to get the updated object right at creation time. No post modification required for the cloned object. This is a more restricted approach and can use in very specific requirements

The cloning mechanism also contains some key principles, which you should be aware. It is better to know the key facts and features of object cloning before start to implement the prototype pattern.

What is Cloning in Java?

Cloning is the process of creating an exact same copy of another object. However, in fact, it is not 100% guaranteed. Anyhow, you can have two different objects with separate references in the heap. Most of the behaviours and features of the clone will be similar to the base object.

A Java class can achieve cloning ability by implementing the marker interface – Cloneable. The application should create a custom interface which implements the ‘Cloneable’ interface. Then, we can use the clone() method to get the copy of the required object.

What is the clone() Method?

This is a protected method in java.lang.Object class. If we need to get any clones from class instances, we should implement the java.lang.Cloneable interface in that class. The clone() method will throw CloneNotSupportedException exception if the Cloneable interface is not implemented inside the required class. The clone() method reduces the expensive and heavy processing effort to create the exact copy of an object. That is it saves the cost of instantiating objects using the ‘new’ operator.

Clone method has two approaches. Those are ‘Shallow Copy’ and ‘Deep Copy’. The shallow copy is the default cloning approach via the clone() method. The clone() method creates a new instance of the same class and the cloned object has copies of primitive values, but the object references points to the same objects as the original copy. That is the clone contains just a set of pointers to the same memory location as referenced by the original object and any change to the original object will reflect in the shallow copy. This does not create a new object on the heap.

Deep Copy makes a brand new object in a new memory location assigning all the properties of the original object. It will have the newly copied referenced objects as well. Any change to original copy won’t reflect in deep copy clone.

The selection of shallow copy and deep copy depends on your particular business need because both have different behaviours at run-time. Moreover, usage of cloning principle is completely a design decision when implementing the prototype pattern.

Steps to Implement the Prototype Pattern

  • Create the interface extending the Cloneable interface
    • We can name it as the ‘Prototype Interface’
  • Include the getClone() or similar named abstract method in the interface
  • Create the concrete class which implements the prototype interface
    • Instances of this concrete class will be cloned when required
  • The client will use the concrete class to instantiate the object once and getClone() method to reuse the copy of the created object

It’s essential to keep good understanding about each property and data of the object needs to be cloned and assure that new object is modifiable.

Participants in Prototype Design Pattern

There are mainly three participant types in this pattern.

  • Prototype: This is the base interface which extends the Cloneable marker interface and contains the abstract method clone()
  • ConcretePrototype: This is the class which needs to be copied and this should implement the prototype interface and override the clone() method
  • Client: This is the program which creates a new object by contacting the prototype to clone itself

Class Diagram for the Prototype Design Pattern

 

Prototype Design Pattern - Class Diagram

Example for Prototype Design Pattern

Let’s take a reality show. There are various competitors with different entertainment abilities like singing, dancing, acting etc. The application wants to instantiate competitors when required and the specific skill will be announced at the time of competing. Instantiating specific competitor with the competing skill is time-consuming and costly. Hence, the best approach is to create a clone and assign specific attribute later.

In this scenario, the original copy will be the ‘Competitor’. Therefore, ‘Competitor’ is the base type and we will implement it as an abstract class. ‘Competitor’ will implement the Cloneable marker interface and override the clone() method to achieve the cloning ability. Concrete classes are ‘Singer’, ‘Dancer’, and Actor. Each concrete class will implement the ‘Competitor’ interface and override the clone() method to achieve the specific featured skill of the competitor.

Competitor.java

Singer.java

Dancer.java

Actor.java

Client.java

Usage of Prototype Design Pattern

  • Object construction is costly we can use a clone of a cashed object for future use
    • Can reduce the costly database calls to construct new objects
  • Requires when the classes get instantiated at run-time as in dynamic loading
  • Requires when the object creation is complicated and time consuming
    • Reading data from a database or over a network
  • When needs to reduce the number of classes in an application.
  • When needs to decouple the object composition, creation and representation from the client application

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: