Prototype Design Pattern

What is Prototype Design Pattern?

  • The word “Prototype” itself conveys that real object is accessed with the help of virtual object.
  • For example, to build a house, you’ll need a prototype design first. So, it falls under the Creational Design Pattern as it plays its role by creating the new objects. 
  • The main feature of Prototype Design Pattern is that it creates objects by cloning or copying. 

Table of Contents

Book definition of Prototype Design Pattern

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

Real-time Examples of Prototype Design Pattern

Example 1:

dog photoLet’s suppose in Java application; you are trying to read the data in Java database like “name” and “weight” of a dog. The database will return the “name” and “weight” for it. Using this information, Java will create an object for a dog. That object will be name XYZ and the weight in kgs. If Java application still needs similar kind of data which means to create identical object requirements for 100 times. That would cost the operation in terms of time and competition. So, doing otherwise, we could use the initial object as a reference and clone it N number of times. So using the first dog object, we can create “Dog1”, “Dog2”, “Dog3” and N number of objects saving time, memory and resources.

Example 2:

Mr. Bean PhotoLet’s suppose if you are trying tto download the movie “Mr. Bean”. If the size of the film is 1 GB, it will take 30 minutes to download it. After watching that movie, you’ll recommend it to your friends. If your friends ask for that movie file, you’ll have to copy the file in a DVD or desktop rather than downloading it again. This is also called prototyping in which we are copying a source in many virtual things so that you can save the memory, time and data at the same time.

When to use Prototype Design Pattern?

  • When the object creation is a costly operation.

Example: An Object is to be created after reading data from a database. Reading data from the database is a costly operation.

Advantages of Prototype Design Pattern

If your application needs an N number of objects of the same type, then you’ll need to create the object for the first time, and you can copy or clone that object for the number of required times. So by using this pattern, you can save time, memory and computational power.