Prototype Design Pattern
The prototype design pattern is a part of the Creational Design Patterns. Its primary aim is to cut off the number of classes for an application. It provides the facility to create a copy of the existing object independent of the actual implementation of their classes, in simple words, the newly created object requires more resources than we want to use or have available so we create the exact copy of the available object.
For example - The file is downloaded from a large server, but it is already available in the memory, we could clone it and work on the new copy of the original.
Prototype design pattern plays an essential role when object formation is quite handy in terms of time and resource usage. We can get a copy of the original object and can modify it according to our needs.
Problems without using the Prototype Method\
Suppose we want to draw the different types of shapes such as circle, rectangle, square, and we have already an instance of it. Now, we decide to create a similar copy of the existing object. How can we achieve this? The ordinary developer will create a new object of the same class and add the entire object's functionality and copy the values. But here is a problem, he/she cannot derive all the functionality because some are private and protected and not available for the outside of the class.
There is another problem; we need the code of the other class to create its copy. This is a situation of dependency, which is certainly a good practice in Software development.
Let's understand the following example, where we will create the various courses classes that contain details regarding the course.
Name of Course: R and its type: R programming language classes Name of Course: Java and its type: Java Basics and Hibernate Name of Course: Python and its type: Python basics and advance
Solution using the Prototype method
To resolve the problem, we will use the Prototype method. We will create two separate classes to creating the exact copy of already existing object with the same field properties. Here, we will define a common interface or class which supports object cloning. With the help of object cloning, we can create the object without coupling.
An object that supports cloning is called Prototype. Let's understand the following example.
Advantages of Prototype Model
The advantages of prototype models are as follows.
Disadvantages of Prototype Model
The prototype model contains a few disadvantages, which are given below.