Creational Design Patterns - 4- Prototype Design Pattern
Prototype design pattern should be followed, if the cost of creating a new object is expensive and resource intensive.
what is Prototype design pattern ?!
cloning of an existing object instead of creating new one and can also be customized as per the requirement.
Prototype design pattern is used in scenarios where application needs to create a number of instances of a class, which has almost same state or differs very little.
In this design pattern ,
an instance of actual object (i.e. prototype) is created on starting, and thereafter whenever a new instance is required, this prototype is cloned to have another instance. The main advantage of this pattern is to have minimal instance creation process which is much costly than cloning process.
Example of Prototype Design Pattern :
- first we will create interface called Prototype .
interface Prototype { public Prototype getClone();
}
- second create class called UserRecord:
class UserRecord implements Prototype{ private int id; private String name, designation; private double salary; private String address; public UserRecord(){ System.out.println("Employee Record"); System.out.println("Eid"+"\t"+"Ename"+"\t"+"Edesignation"+"\t"); } public UserRecord (int id, String name, String designation) { this(); this.id = id; this.name = name; this.designation = designation; } public void showRecord(){ System.out.println(id+"\t"+name+"\t"+designation; } @Override public Prototype getClone() { return new UserRecord(id,name,designation); }
}
- Now we will create demo class :
class PrototypeDemo{ public static void main(String[] args) throws IOException { int eid=11 ; String ename="Mohamed" ; String edesignation="Record description" ; UserRecorde1=(UserRecord) e1.getClone(); e1.showRecord(); }
}
see now , it is done :)
Advantages for Prototype design patten:
Reduced load of initialization:
Every new object created using the clone method reflects the exact object state of the original object. If we create many objects of the same class or structure by calling the class constructor and then initialize every property explicitly for each object, this will increase the number of repetitive lines needed to properly initialize every property of every object created.
This need of initialization can be reduced drastically by using this pattern. We can always create a clone of the existing object created in the application to have the objects readily initialized to the non-default/default state (Constructors are sufficient to initialize the object to default state).
Reusability - Optimized coding efforts:
One object created in the system and initialized to the either default or non-default state is sufficient to create the similar object copies again and again.
If there are only few properties that differ from object to object, we need not write the code again and again to initialize the rest of the properties. This will optimize the coding efforts of writing the code to initialize the properties that are different between objects of the same class or structure.
Simplified object copy process:
Since the object copying is done recursively by calling the Clone method on every member composition object, this makes the program structure easier to understand and maintain.
Thanks for Reading ......
See you in other Pattern .