Prototype Design Patterns or Copy Design Pattern

Prototype Design Patterns or Copy Design Pattern, Today’s programming is all about effective usage of resources and lower costs to application. Saving is a big issue when it comes to using computer resources, Copy of object is the best instead of creating a new one in case cost to object creation is huge then copy, in design patter you have to copy from couple of best solutions, as here we have two options to get the class object:

  1. Create a new Object
  2. Copy the Object.
Where to use ProtoType Pattern:
When creating an object is time consuming and a costly and you already have a most similar object instance in hand, then you go for prototype pattern(Copy the Object instance).

How to Implement Prototype Pattern?
Prototype Design Pattern is basically use to copy objects, and here we have to implement Cloneable interface so that with clone method we can have clone of object, Below is the example by which it can be more clear about it.

package com.tekhnologia.designpattern.prototype; class Bike implements Cloneable { private int gears; private String bikeType; private String model; public Bike() { bikeType = “Standard”; model = “Leopard”; gears = 4; } public Bike clone() { return new Bike(); } public void makeAdvanced() { bikeType = “Advanced”; model = “Jaguar”; gears = 6; } public String getModel(){ return model; } }
Class which will actually implementing the Prototype Design Pattern. which will call clone method and create instance.
package com.tekhnologia.designpattern.prototype; public class Workshop { public Bike makeJaguar(Bike basicBike) { basicBike.makeAdvanced(); return basicBike; } public static void main(String args[]){ Bike bike = new Bike(); Bike basicBike = bike.clone(); Workshop workShop = new Workshop(); Bike advancedBike = workShop.makeJaguar(basicBike); System.out.println(“Prototype Design Pattern: ” + advancedBike.getModel()); } }

Here check the code written in red which is mandatory to implement Prototype design pattern.

Applicability & Examples

Use Prototype Pattern when a system should be independent of how its products are created, composed, and represented, and:

  1. Classes to be instantiated are specified at run-time.
  2. Avoiding the creation of a factory hierarchy is needed.
  3. It is more convenient to copy an existing instance than to create a new one.

Example 1

In building stages for a game that uses a maze and different visual objects that the character encounters it is needed a quick method of generating the haze map using the same objects: wall, door, passage, room… The Prototype pattern is useful in this case because instead of hard coding (using new operation) the room, door, passage and wall objects that get instantiated, CreateMaze method will be parameterized by various prototypical room, door, wall and passage objects, so the composition of the map can be easily changed by replacing the prototypical objects with different ones.

Example 2

Suppose we are doing a sales analysis on a set of data from a database. Normally, we would copy the information from the database, encapsulate it into an object and do the analysis. But if another analysis is needed on the same set of data, reading the database again and creating a new object is not the best idea. If we are using the Prototype pattern then the object used in the first analysis will be cloned and used for the other analysis.

If you like reading please spare 5 seconds to share this else you can comment and provide feedback to improve it.