Prototype Method is a Creational Design Pattern which aims to reduce the number of classes used for an application. It allows you to copy existing objects independent of the concrete implementation of their classes. Generally, here the object is created by copying a prototypical instance during run-time.
It is highly recommended to use Prototype Method when the object creation is an expensive task in terms of time and usage of resources and already there exists a similar object. This method provides a way to copy the original object and then modify it according to our needs.
Problems we face without Prototype Method
Suppose we have a Shape class that produces different shapes such as circle, rectangle, square, etc and we already have an object of it. Now we want to create the exact copy of this object. How an ordinary developer will go?
He/She will create a new object of the same class and will apply all the functionalities of the original objects and copy their values. But we can not copy each and every field of the original object as some may be private and protected and are not available from the outside of the object itself.
Problems are not over here! you also become dependent on the code of other class which is certainly not a good practice in Software Development.
For better understanding, let’s understand the example of Courses At GeeksforGeeks that provides courses like SDE, DSA, STL, etc. Creating objects for similar courses, again and again, is not a good task to utilize the resources in a better way.
Note: Following code is written without using Prototype Method
Solution by Prototype Method:
To deal with such problems, we use the Prototype method. We would create separate classes for Courses_At_GFG and Course_At_GFG_Cache which will help us in creating the exact copy of already existing object with the same field properties. This method delegates the cloning process to the actual objects that are being cloned. Here we declare a common interface or class which supports object cloning which allows us to clone the object without coupling our code to the class of that method.
An object that supports cloning is called as Prototype.
UML Diagram of Prototype Design Pattern
- Less number of SubClasses : All the other Creational Design Patterns provides a lot of new subClasses which are definitely not easy to handle when we are working on a large project. But using Prototype Design Patterm, we get rid of this.
- Provides varying values to new objects: All the highly dynamic systems allows you define new behavior through object composition by specifying values for an object’s variables and not by defining new classes.
- Provides varying structure to new objects: Generall all the applications build objects from parts and subparts. For convenience, such applications often allows you instantiate complex, user-defined structures to use a specific subcircuit again and again.
- Abstraction: It helps in achieving the abstraction by hiding the concrete implementation details of the class.
- Waste of resources at lower level: It might be proved as the overkill of resources for a project that uses very few objects
- Independency from Concrete Class: Prototype method provides the way to implement the new objects without depending upon the concrete implementation of the class.
- Recurring problems : Prototype method is also used to solve the recurring and complex problems of the software development.
Further Read – Prototype Design Pattern in Java
- Observer method - Python Design Patterns
- Iterator Method - Python Design Patterns
- Flyweight Method - Python Design Patterns
- Composite Method - Python Design Patterns
- Decorator Method - Python Design Patterns
- Facade Method - Python Design Patterns
- State Method - Python Design Patterns
- Template Method - Python Design Patterns
- Command Method - Python Design Patterns
- Proxy Method - Python Design Patterns
- Memento Method - Python Design Patterns
- Factory Method - Python Design Patterns
- Builder Method - Python Design Patterns
- Strategy Method - Python Design Patterns
- Bridge Method - Python Design Patterns
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.