Wednesday, July 6, 2022

Creational Design Patterns # Prototype

Prototype is a creational design pattern that lets you copy existing objects without making your code dependent on their classes.


Say you have an object, and you want to create an exact copy of it. How would you do it? First, you have to create a new object of the same class. Then you have to go through all the fields of the original object and copy their values over to the new object.

Nice! But there’s a catch. Not all objects can be copied that way because some of the object’s fields may be private and not visible from outside of the object itself.

There’s one more problem with the direct approach. Since you have to know the object’s class to create a duplicate, your code becomes dependent on that class. If the extra dependency doesn’t scare you, there’s another catch. 

Sometimes you only know the interface that the object follows, but not its concrete class, when, for example, a parameter in a method accepts any objects that follow some interface.


The Prototype pattern delegates the cloning process to the actual objects that are being cloned. The pattern declares a common interface for all objects that support cloning. This interface lets you clone an object without coupling your code to the class of that object. Usually, such an interface contains just a single clone method.

The implementation of the clone method is very similar in all classes. The method creates an object of the current class and carries over all of the field values of the old object into the new one. You can even copy private fields because most programming languages let objects access private fields of other objects that belong to the same class.

An object that supports cloning is called a prototype. When your objects have dozens of fields and hundreds of possible configurations, cloning them might serve as an alternative to subclassing.

Here’s how it works: you create a set of objects, configured in various ways. When you need an object like the one you’ve configured, you just clone a prototype instead of constructing a new object from scratch.

Here’s how it works: you create a set of objects, configured in various ways. When you need an object like the one you’ve configured, you just clone a prototype instead of constructing a new object from scratch.


Basic implementation

Prototype registry implementation

Pros and Cons

Here is a sample program showing Prototype design pattern example in java.

It would be easy to understand prototype design pattern with an example. Suppose we have an Object that loads data from database. 
Now we need to modify this data in our program multiple times, so it’s not a good idea to create the Object usinnew keyword and load all the data again from database.
The better approach would be to clone the existing object into a new object and then do the data manipulation.
Prototype design pattern mandates that the Object which you are copying should provide the copying feature. It should not be done by any other class.
However whether to use shallow or deep copy of the Object properties depends on the requirements and its a design decision.
import java.util.ArrayList; import java.util.List; public class Employees implements Cloneable{ private List<String> empList; public Employees(){ empList = new ArrayList<String>(); } public Employees(List<String> list){ this.empList=list; } public void loadData(){ //read all employees from database and put into the list empList.add("Pankaj"); empList.add("Raj"); empList.add("David"); empList.add("Lisa"); } public List<String> getEmpList() { return empList; } @Override public Object clone() throws CloneNotSupportedException{ List<String> temp = new ArrayList<String>(); for(String s : this.getEmpList()){ temp.add(s); } return new Employees(temp); } }
import java.util.List; public class PrototypePatternTest { public static void main(String[] args) throws CloneNotSupportedException { Employees emps = new Employees(); emps.loadData(); //Use the clone method to get the Employee object Employees empsNew = (Employees) emps.clone(); Employees empsNew1 = (Employees) emps.clone(); List<String> list = empsNew.getEmpList(); list.add("John"); List<String> list1 = empsNew1.getEmpList(); list1.remove("Pankaj"); System.out.println("emps List: "+emps.getEmpList()); System.out.println("empsNew List: "+list); System.out.println("empsNew1 List: "+list1); } }

Follow on LinkedIn

You may also like

Kubernetes AWS Java Coding Question
Microservices Core Java Spring Boot
Spring Framework Kafka Miscellaneous