Prototype Design Pattern
Design pattern is mainly concerned with designing a software effectively. So that the hustle to implements a new feature is less. It acts more like a blueprint for a software design which can be again customized with the specific requirement.
Design patterns are mainly divided into three types.
- Creational design patterns : — Deals with creation of a product or objects effectively.
- Structural Design Pattern : — It explains how a object and class can be assembled in together so that codebase will be more flexible.
- Behavioral Design: — it takes care of effective communication and assignments of responsibilities between objects.
Prototype Design Pattern is one of creational design pattern. By this type design pattern we can clone any object of any any any types of effectively. WE also can modify as per our requirement during the cloning of an object.
What is Prototype Design Pattern?
Cloning of an existing object instead of creating a new one .At the time of cloning we can also modify our cloned object.
Problems With normal Cloning Of An Object ?
Suppose Anyone wants an duplicate copy of the existing object. We can go through all the filed of that object and create a new one Problem Solved!!But there may be some private instances variable which are not visible to outside, So we can’t get their value. There There may be some interface which are being implemented in that class and it follows some of the variables so we can’t also get them. And lastly we have to dependent upon the parameters which are essential to build that object.
Solution with Prototype Design Pattern -
So we can make an GetClone() method which will return the new object of that class. And method will be defined in an interface and gets implemented by the respective class(Advantages : method body is not visible).
Where we can apply this?
- when a system is independent upon the product creation, composition and representation.
- when the details of class to instantiate are specified at runtime.
- When instances of a particular class have a few combination of state. Then it may be more convenient to not instantiate the class manually rather than cloning the object of that class.
- Consider an another situation where we take some input based upon some parameter from user. But we want to hide the whole object cloning or creation from user we also can use this.
Elements of Prototype Design Patterns : —
- Prototype :-
Just an interface for the cloning method.
- Concrete Prototype : —
implements the Prototype interface and defined the required method body.
- Client :-
create a new object by prototype cloning.
The main advantages of prototype pattern:
- It reduces the need of sub-classing, means that we can reduce the use of inheritance. Suppose a class A inherits the properties of an another class B. Now instead of declaring the inheritance relationship between A and B we can clone a object of class B inside the class A and reuse all the required properties of class B to make an object of class A.
2. It hides complexities of creating objects.
3. The clients can get new objects without knowing which type of object it will be It will also helps in data abstraction.
4. It lets you add or remove objects at runtime.
Interesting Facts : In case of java, Inheritance breaks another Object Oriented Program concept, Encapsulation. If we extends parents into child then all physical properties of parent is visible to the child.
Structure Of Prototype Design Pattern : —
Implementation Of Prototype Design Pattern : —
step 1 :- creating an interface for the cloning method. All class should implements this interface in order to do clone of an object.
step 2 :- creating the main class, from which object will be cloned.
Here , name of a student is public means visible to out side of the class but id and department of a student is private which is not visible from outside of class. So if we instantiate a object of this Student class and we want to make an exact clone of that object from this one we can not get the access for the id and department name of a student . So in formal way, an object can not be cloned from this instantiated object.
step 3 :- creating the main client size code, from where as per requirements objects can be instantiated.
This type of design pattern generally is not used standalone .It is used in some another type of design pattern to clone a particular object. One of them is Abstract Factory Design Pattern, In this pattern Prototype design pattern is used for creating dynamic object.
References : -
- Design Patterns: Elements of Reusable Object-Oriented Software.(GOF)
- https://www.javatpoint.com/prototype-design-pattern