Prototype Design Pattern

What is Prototype Pattern:
Below are the main points

1. Creating new objects (instances) by cloning (copying) other objects
2. Allows for adding of any subclass instances of a known superclass at runtime.
3. When there are numerous potential classes that you want to only use if needed at runtime.
4. Reduces the need for creating subclasses

UML Diagram:

Let’s see it by an example

1. Create Animal Interface class which extends Cloneable interface

2. Create CloneFactory class which is response create copy of the object

3. Create sheep class which create clone of the sheep object

4. Create TestClone class which actually call CloneFactory makeCopy function to create the clone of an object

Below is the output

Sheet is made
Sheep is being made
Learning daily is way to success
Learning daily is way to success
Sally Hashcode: 1311053135
CloneSheep Hashcode: 1550089733

Leave a Reply

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