An Introduction to Prototype Design Pattern

Hasini Sandunika Silva
Nerd For Tech
Published in
3 min readMay 23, 2021
An Introduction to Prototype Design Pattern

The prototype design pattern is responsible for creating new objects without using the new keyword and this allows cloning the previously created objects. This is categorized under the creational pattern. The prototype design pattern is used when the creation of objects directly is very costly or expensive.

When we implement the prototype pattern we must be aware of whether we need a shallow copy or a deep copy of the object.

Shallow Copy

This implies the cloned object is just simply a copy of the exact resource.

Deep Copy

Here we need to copy everything inside the object into the new object. This implies all variables, objects, etc. declared inside the class should be copied into the new object.

The following example describes how we can implement the prototype design pattern in a practical scenario.

Assume there is a video shop and mainly, they sell movies and video games to the client.

Here the main purpose is to create copies of the Video class. Figure 1 illustrates the general overview of the application.

Figure 1. The General Overview of the Application.

Here I have implemented the Video class with 2 attributes and implemented a method to calculate the price of the video based on the video type (movie or video game). Inside the Video class, I have overridden the clone() method while implementing the Cloneable interface. Refer to the following code.

After that, I have implemented Movie and VideoGame classes which are inherited from the Video class. Here I have overridden the calculatePrice() method. Refer to the following codes.

Next, inside the VideoResigtry class, I have implemented the constructor as, when the user creates an object of the VideoResigtry, this directly creates 2 separate instances of both Movie and VideoGame classes and adds them to a HashMap with their class types. Here, I have declared another method to return the Video object when the video type is given. Usually, the clone method returns the objects in Object type, because of that we have to cast the return object into the type we want. Refer to the following code.

Apart from that, I have implemented an Enum to hold the types of Video classes. Refer to the following code.

Finally, I have implemented the main class and named it as MainApplication. Refer to the following code.

The following defines the output received.

Movie{moviePrice=250.0, quality='HD', time=180}
Movie{moviePrice=250.0, quality='1080p', time=200}
Movie{moviePrice=250.0, quality='HD', time=180}

When we refer to the outputs, I have changed the time, quality and printed the output. After that, again I have accessed a copy of the Movie class but this didn’t return the instance with the changes I have applied previously. This happens because, here, this always holds a temporary object of Movie (as I have created in VideoRegistry class) and returns this temporary object when the getVideo() method is called.

Again I have implemented the prototype pattern using a small sample example in javascript. Refer to the following code.

Here I have added 2 sets of attributes to the video object on prototype space and after that, I have created another object of video called newvideo. In the console log, print whether there is an attribute called time in the video. The following defines the output received.

true
true
true
false

As per this, first, it checks whether the time attribute is in both object and the video prototype space. But the second log only checks the attribute on the object newvideo.

Conclusions

As per the things discussed above.

  • Prototype design pattern allows cloning the previously created objects.
  • This is categorized under the creational pattern.
  • This can be used when the creation of objects directly is very costly or expensive.

References

--

--