Prototype Design Pattern
As you know this article will be another design pattern which is Prototype Design Pattern and it’s under the creational type. I have published two articles regarding design patterns. So if you are new to design patterns you can always refer those articles. Prototype Pattern says that cloning of an existing object instead of creating new one and can also be customized as per the requirement. So what does it means? I will explain it simply. let’s say we have a online store so when the user search a product it will show thousand of items right. So it’s easier to clone those items rather than creating. We can create a first instance and register that. Then when ever you need an instance you clone it from the original one. No need to create a new instance. As in my previous articles first let’s learn prototype design pattern in a very simple example. Then after we understand the basics we can go for a more advanced real world example. Take a look at the below UML diagram.
As in the above diagram we need to create Shape abstract class and other concrete classes(Sub classes in here Circle,Square,Rectangle). Then those classes should extend from the shape class. Next we need to create ShapeCache class. In our ShapeCache class we have method call get shape and it’s returning a instance of the shape which the user is requesting. User requesting is done through the main class. Our PrototypePatternDemo class which is have the main method will use ShapeCacshe to get a Shape object. It will pass information (CIRCLE / RECTANGLE / SQUARE) to ShapeCacshe to get the type of object it needs. As you can see this is the basic concept of the Prototype design pattern. I’m not going to implement this scenario because all the properties and methods are clearly mentioned in the UML diagram, so you can try that one.
Shallow Copy vs Deep copy
When you implement the prototype design pattern you need to make sure whether you need a shallow copy or deep copy. A shallow copy is accomplished by creating a new collection object and populating it with references to the original’s child objects. Because the copying operation does not recurse, the child objects will not be duplicated. Shallow Copy stores the copy of the original object and points the references to the original objects. It simply means that all modifications made to a copy of an object are reflected in the original.
The term “deep copy” refers to a recursive copying procedure. It means first creating a new collection object, then recursively populating it with copies of the original’s child objects. A copy of an object is copied into another object(Location) in a deep copy. Any modifications made to a copy of an object are not reflected in the original object.
Now let’s see an advanced real world example. There is a shop called Magic Watches which selling watches. In that shop there are different types, different models, different prices of watches are available. Customer can come to the store and buy or online purchased through the website. So huge variety is there. Mainly watches are divided into Analog watches, Digital watches and Smart watches. Analog watch contains only the basic features of a watch. So bascially it’s the traditional watch. Digital watches have alarms and stopwatch. Smart watches have some features like storage of the watch, watch OS which are not available in a traditional watch.
Let’s implement the scenario. First I have implement the Watch class and make it as abstract. Also implements the getters and setters. Since the objects are need to clone Watch class implements from the Cloneable interface.
Then I created the other classes which are Analog, Digital and Smartwatch and extend from the Watch class. Also I implemented the toString method.
Then you have to implement a enum as below.
Next in the WatchRegistry class I have implemented HashMap called Watches. Inside WatchRegistry class we have the constructor, getWatch method, and initialize() method as in the below.
In the main class first you have create a instance from the WatchRegistry class. Then you can create a instance from the sub classes in here Analog, Digital and Smart. Next you can call the getWatch method which is in the WatchRegistry class using watchRegistry instance and remember to cast it like in the below. Finally you can change any properties values according to your needs.
The output of the program is below.
Now let’s see what are the advantages and disadvantages of prototype design pattern.
Advantage of Prototype design Pattern
- It reduces the need of sub-classing.
- It hides complexities of creating objects.
- The clients can get new objects without knowing which type of object it will be.
- It lets you add or remove objects at runtime.
Disadvantages of Prototype Design Pattern
- Overkill for a project that uses very few objects and/or does not have an underlying emphasis on the extension of prototype chains.
- It also hides concrete product classes from the client
- Each subclass of Prototype must implement the clone() operation which may be difficult, when the classes under consideration already exist. Also implementing clone() can be difficult when their internals include objects that don’t support copying or have circular references.
When to use Prototype design Pattern
- When the classes are instantiated at runtime.
- When the cost of creating an object is expensive or complicated.
- When you want to keep the number of classes in an application minimum.
- When the client application needs to be unaware of object creation and representation.
You have reached to the end of this article. So I hope you learn about the prototype design pattern and I will be upload another design pattern in up coming blogs. I have uploaded the full code in to the GitHub and I put the link in the references so if you want you can refer that. Then time to say good bye and let’s meet from another article.