Prototype Design Pattern
As a simple term, what prototype does is, provide a proposed model of solution to (not an actual solution) to stimulate real scenario. Then the real implementation can be done by quickly and cheaply by refining this already implemented prototype. Because, by using the prototype we can learn our faults and refine them by avoiding severe damages on real implementations.
The prototype design pattern is not that much famous though it’s very useful. We can use this pattern when the object creation is really expensive and time consuming. That means it avoids the object creation.
The Common Behavior of Prototype Design Pattern
🌟 Instead of object creation, prototype design pattern encourage us to clone or we can say copy the existing object. What here happen is, at the beginning you register a object and then others can clone it.
🌟By its nature, it avoids “new” keyword to create an object after initial instantiation.
Let’s we discuss about the real scenario to understand about the prototype design pattern.
Cloning in Prototype Design Pattern
💡Clone can be done by using “Cloneable” interface.
💡Cloneable interface does not have any implementation of methods.
💡Cloning can be done in 2 ways.
1. Shallow Copy: We can copy basic level references to our newly created object. Here there is a disadvantage as well. Because, since you get basis of object, if you alter your newly created object, it will affect for the original copy as well.
2. Deep Copy: We go inside and copy each and every objects and values for newly created object.
Let’s say John has a electrical shop called JElec in his city. With the increased revenue, he decided to open 2 branches in nearby 2 cities as well. As well, he needs to have own website to show all the products, brands of products he has and all other required information.
In that case, he should show all information on other 2 branches as well. In that case what we can do is implement all the details of one branch of shop and clone these details for other branches.
Let’s move to the implementation of Shallow copy.
As the first step, I created a Device class to store, get and set product details as Product Id and Product Name. To print the details, I used toString().
So, you can see that the shop has address which is intend to mentioned by city name. As well shop has products with product ids and product name. To get the device details we use array list which is referred by previously created Device class.
To clone the object, here we implemented the cloneable interface and override clone() which has Object type as a return type.
Below shows about my main method. In here, at the beginning I have created a sd object from ShopDevices to fetch the data. As well, I assigned city of the shop as City A.
Then the JElec shop located at City B has the same brands and same products. But to show these details also, I can clone the details from sd object insteada of loading data from the addDeviceDetails() in ShopDevices class as below.
The output: So as you can see it printed details of City B by cloning from City A.
Now, the owner of the shop requires to remove “Product 3” details from the City A since there has low demand for this product. So, I removed that product as below.
As you can see our object is achieved. That means “Product 3" is removed from the City A. But look at in City B, the “Product 3” is removed from the list which we have not intended to do. That is the draw back of sallow copy.
In order to avoid this draw back from the shallow copy, we can use deep copy for implementing prototype pattern 👏.
Let’s see how we can achieve this. In deep cloning, we have 2 different objects with different data.
Step1: Change the object type of clone() in to ShopDevices in ShopDevicesclass. So here you are saying that I do not want normal cloning but deep cloning.
Then fetch the data from previously created object and assign these details to new object called “d”.
Then let’s modify our main method to get results.
A s you can see, now the Product3 details are only removed from City A instead of both cities.
Benefits and Critics on Prototype Design Pattern
I hope you got a clear understand about prototype pattern and all these shallow and deep cloning concepts as well. Stay tuned and we are going to discuss about some other design patterns as well.
Stay Safe !!!