Creational – Prototype Design Pattern
July 28, 2020 1 min read

Prototype design pattern is also belonging to the Creational Design Patterns. This is using when you want to avoid multiple object creation of same instance: instead you copy the object to new object & then we can modify as per our need.
Let’s check that using an example.

Here First I created an interface called “Prototype” and I made a method called “getClone()” inside the interface.
Next I created a class called “CustomerRecord” in line 1, by implementing the interface I made earlier and then I created the variables and the constructors. The most important line is the line 2. There I overridden the method “getClone()” that I created inside the interface “Prototype” and I returned the object. Let’s go to the main method.

Here I created a new object in the line 3 to get printed values that I inserted in the console as commanded in the 2 constructors of the class “CustomerRecord”. But in the line 4 you can see I called the “getClone()” method, so that I can print the same value that I called in the line 3. For that you don’t have to create a new object. See the output given below.

You can see here a copy of values from the object I created earlier to a new object by avoiding the creation of new object. This is where I used the Prototype Design Pattern.
Thank you.


