Javatpoint Logo
Javatpoint Logo

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.

Example -

Output:

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.

Example -

Advantages of Prototype Model

The advantages of prototype models are as follows.

  • All types creational design pattern provides us a lot of hard to maintain classes, especially when we are working on a large project. But the Prototype design pattern helps us to get rid of it.
  • With the help of a prototype model, we can provide the various values to the new object. The dynamic behavior helps us to define new behavior through the object composition.
  • General all the application build objects from parts and subparts. The user-defined structure is used a specific again and again.

Disadvantages of Prototype Model

The prototype model contains a few disadvantages, which are given below.

  • It hides the concrete implementation details of the class using abstraction.
  • It might be showed as the excess of resources for a project that uses very few objects.

Applicability

  • The prototype model provides independence from the concrete class. We can implement the new object without depending upon the existing class.
  • It is also helpful to solve the recurring and complex problem of software development.





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA