Prototype Design Pattern Implementation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Let us learn yet another creational design pattern in this article. Prototype design pattern implementation allows us to solve the problem of having multiple copies of objects those are expensive and resource intensive to create.

Prototype Design Pattern Implementation:

Gang of Four book defines prototype design pattern as below

Specifies kind of objects to created using prototypical instance and create new object by copying this object

Generic class diagram is as below

 

Prototype pattern class diagram

  • Client: references to Prototype and instantiates ConcretePrototype.
  • Prototype : An interface which declares methods for cloning the object.
  • ConcretePrototype: A concrete class which implements Prototype interface

Prototype design pattern implementation helps us get multiple copies of one object without having to worry about expenses of creating a new object. In java, prototype design pattern implementation can be achieved by using Cloneable interface. Although clone method is defined in Object class, we have to implement Cloneable for some reason which we will not discuss here.

Prototype Design Pattern Implementation:

If you have git installed, then you can directly clone the design-patterns repository. I have kept code for all design patterns at https://github.com/theJavaGeek/design-patterns. Each branch is a different design pattern.

Go to your working directory and then follow below steps:

  1. git clone https://github.com/theJavaGeek/design-patterns.
  2. cd design-patterns
  3. git checkout prototype

If you don’t have git installed, you can either directly download zip or copy paste from below.

 

Prototype.java

Bike.java

Client.java

This program outputs,

Creating a new object now
Time required = 2000
Object creation done
Creating clone now
Time required = 0
Clone created

  • We have created Bike constructor such that it takes 2000 milliseconds to simulate expensive object creation which takes lot of time. We are printing out time spent in constructing Bike object using constructor.
  • Then Client calls bike.clone() method which quickly creates another object without taking much time.
  • This helps us create multiple object clones where creation of object is expensive and resource intensive.

I hope prototype design pattern implementation helped.

Happy learning 🙂

 

Share Button

2 comments for “Prototype Design Pattern Implementation

  1. Nyng
    April 4, 2017 at 3:42 pm

    This is the best example I’ve ever seen, great jobs! @Prasad Kharkar

    • April 4, 2017 at 5:04 pm

      Thank you Nyng. Please feel free to provide any constructive/destructive feedback. I would like to take the opportunity to improve 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *