What is prototype Pattern?
When object creation is costly and time-consuming, Pick an object that is configured for either the default or in the ballpark of some specific use case and then you clone this object and configure to your exact needs.
When to use prototype Pattern?
- Prototype patterns are required when object creation is a time-consuming, and costly operation, so we create objects with the existing object itself.
- The Prototype Design Pattern allows you to create new objects by cloning existing ones without being bound to the specific classes of the objects being cloned. This promotes decoupling between the client code and the concrete classes, making it easier to introduce new types of objects without modifying the client code.
Where to use prototype Pattern?
- If you want to create an object but do not want to go through the expensive object creation procedure where network or database calls are made, then use the prototype pattern. Just create a copy of the object and do your changes on it.
How prototype Pattern is acheived?
- Create a Interface with clone method
- Create a Concrete prototypes which implements the interface. In the below example it is PersonPrototype. This Concrete class would be DB classes which consume lot of resource and the one which you dont want to create again and again
- Classes use the above Concrete prototype to add their own attributes and create new classes EmployeePrototype, StudentPrototype, SoftwareEmployee(Concrete Class)
- Prototypes are added to Registry which is nothing but an HashMap to Store String and Prototype classes
- Concrete classes which want to extend the Prototype uses the above hashmap registry to fetch the protype and add their custom attributes.
There are two things to note in code
- One is Clone method which makes the extending class mandatory to implement clone method. The same can be done by implementing Clone interface
- Second thing is how the Concrete Classes implements the clone and creates copy of itself at the same time calling the parent prototype class
. . public StudentPrototype(StudentPrototype studentPrototype) { super(studentPrototype); this.institution = studentPrototype.institution; this.rollNo = studentPrototype.rollNo; } .
- PersonPrototype is Concrete Prototype which is costly to create
- We add Custom attributes at each level and create prototype and concrete class from PersonPrototype
Prototype.java
public interface Prototype<T> { T clone(); }
ConcretePersonPrototype.java
public class ConcretePersonPrototype implements Prototype<ConcretePersonPrototype>{ private String name = "Mugilvannan G"; private String aadhar = "1452-5874-5124-847"; public ConcretePersonPrototype() { } @Override public ConcretePersonPrototype clone() { return new ConcretePersonPrototype(this); } public ConcretePersonPrototype(ConcretePersonPrototype concretePersonPrototype) { this.name = concretePersonPrototype.name; this.aadhar = concretePersonPrototype.aadhar; } public String getName() { return name; } public String getAadhar() { return aadhar; } }
EmployeePrototype.java
public class EmployeePrototype extends ConcretePersonPrototype { private Integer pfAccountNo; private String startDate; private String endDate; public EmployeePrototype() { } public EmployeePrototype(EmployeePrototype employeePrototype) { super(employeePrototype); this.pfAccountNo = employeePrototype.pfAccountNo; } public EmployeePrototype clone() { return new EmployeePrototype(this); } public Integer getPfAccountNo() { return pfAccountNo; } public void setPfAccountNo(Integer pfAccountNo) { this.pfAccountNo = pfAccountNo; } public String getStartDate() { return startDate; } public void setStartDate(String startDate) { this.startDate = startDate; } public String getEndDate() { return endDate; } public void setEndDate(String endDate) { this.endDate = endDate; } @Override public String toString() { return "Career Details : {" + '\'' + " Employee Name ='" + super.getName() + '\'' + " Employee Aadhar ='" + super.getAadhar() + '\'' + " Employee pfAccountNo='" + this.getPfAccountNo() + '\'' + '}'; } }
StudentPrototype.java
public class StudentPrototypeConcrete extends ConcretePersonPrototype { private String institution; private Integer rollNo; public StudentPrototypeConcrete() { } public StudentPrototypeConcrete(StudentPrototypeConcrete studentPrototype) { super(studentPrototype); this.institution = studentPrototype.institution; this.rollNo = studentPrototype.rollNo; } public Integer getRollNo() { return rollNo; } public void setRollNo(Integer rollNo) { this.rollNo = rollNo; } public String getInstitution() { return institution; } public void setInstitution(String institution) { this.institution = institution; } public StudentPrototypeConcrete clone(){ return new StudentPrototypeConcrete(this); } @Override public String toString() { return "Education Details : {" + " Student Name='" + super.getName() + '\'' + " Student Aadhar='" + this.getAadhar() + '\'' + " Student Institution='" + this.getInstitution() + '\'' + " Student Rollno='" + this.getRollNo() + '\'' + '}'; } }
EmployeePrototype.java
public class EmployeePrototype extends PersonPrototype { private Integer pfAccountNo; private String startDate; private String endDate; public EmployeePrototype() { } public EmployeePrototype(EmployeePrototype employeePrototype) { super(employeePrototype); this.pfAccountNo = employeePrototype.pfAccountNo; } public EmployeePrototype clone() { return new EmployeePrototype(this); } public Integer getPfAccountNo() { return pfAccountNo; } public void setPfAccountNo(Integer pfAccountNo) { this.pfAccountNo = pfAccountNo; } public String getStartDate() { return startDate; } public void setStartDate(String startDate) { this.startDate = startDate; } public String getEndDate() { return endDate; } public void setEndDate(String endDate) { this.endDate = endDate; } @Override public String toString() { return "Career Details : {" + '\'' + " Employee Name ='" + super.getName() + '\'' + " Employee Aadhar ='" + super.getAadhar() + '\'' + " Employee pfAccountNo='" + this.getPfAccountNo() + '\'' + '}'; } }
SoftwareEmployee.java
public class SoftwareEmployee extends EmployeePrototype { public Integer empId; public String designation; public String practice; public String organisation; public SoftwareEmployee() { } public SoftwareEmployee(SoftwareEmployee softwareEmployee) { super(softwareEmployee); this.empId = softwareEmployee.empId; this.designation = softwareEmployee.designation; this.practice = softwareEmployee.practice; this.organisation = softwareEmployee.organisation; } public SoftwareEmployee clone(){ return new SoftwareEmployee(this); } public Integer getEmpId() { return empId; } public void setEmpId(Integer empId) { this.empId = empId; } public String getDesignation() { return designation; } public void setDesignation(String designation) { this.designation = designation; } public String getPractice() { return practice; } public void setPractice(String practice) { this.practice = practice; } public String getOrganisation() { return organisation; } public void setOrganisation(String organisation) { this.organisation = organisation; } @Override public String toString() { return "Present Work Details: {" + "Software Engineer Name ='" + super.getName() + '\'' + " Aadhar No ='" + super.getAadhar() + '\'' + " EmpId ='" + this.empId + '\'' + " Designation='" + this.designation + '\'' + " Organisation='" + this.organisation + '\'' + " Practice='" + this.practice + '\'' + '}'; } }
Client.java
public class Client { public static void fillRegistry(PersonRegistry registry) { StudentPrototype objStudPrototype = new StudentPrototype(); objStudPrototype.setRollNo(15462); objStudPrototype.setInstitution("Mowbarys"); registry.register("StudentPerson", objStudPrototype); EmployeePrototype objEmpPrototype = new EmployeePrototype(); objEmpPrototype.setPfAccountNo(4151542); objEmpPrototype.setStartDate("2012"); objEmpPrototype.setEndDate("NA"); registry.register("EmployeePerson", objEmpPrototype); SoftwareEmployee objSoftwareEmployee = new SoftwareEmployee(); objSoftwareEmployee.setPfAccountNo(4151542); registry.register("SoftwareEmployee", objSoftwareEmployee); } public static void main(String[] args) { PersonRegistry registry = new PersonRegistry(); fillRegistry(registry); EmployeePrototype objEmployee = (EmployeePrototype) registry.get("EmployeePerson").clone(); StudentPrototype objStudent = (StudentPrototype)registry.get("StudentPerson").clone(); objStudent.setRollNo(15425); objStudent.setInstitution("Mowbarys"); SoftwareEmployee objSoftEmployee1 = (SoftwareEmployee) registry.get("SoftwareEmployee").clone(); objSoftEmployee1.setStartDate("2011"); objSoftEmployee1.setEndDate("2012"); objSoftEmployee1.setEmpId(1001); objSoftEmployee1.setOrganisation("Fuchsia Software"); objSoftEmployee1.designation = "Programmer"; objSoftEmployee1.practice = "Classic ASP, HTML, CSS"; SoftwareEmployee objSoftEmployee2 = (SoftwareEmployee) registry.get("SoftwareEmployee").clone(); objSoftEmployee2.setStartDate("2012"); objSoftEmployee2.setEndDate("2015"); objSoftEmployee2.setEmpId(754185); objSoftEmployee2.setOrganisation("Infosys"); objSoftEmployee2.designation = "Technology Analyst"; objSoftEmployee2.practice = "Java, Spring Boot"; SoftwareEmployee objSoftEmployee3 = (SoftwareEmployee) registry.get("SoftwareEmployee").clone(); objSoftEmployee3.setStartDate("2018"); objSoftEmployee3.setEndDate("NA"); objSoftEmployee3.setEmpId(152); objSoftEmployee3.setOrganisation("Cognizant"); objSoftEmployee3.designation = "Technology Lead"; objSoftEmployee3.practice = "Java, Devops"; System.out.println(objStudent); System.out.println(objEmployee); System.out.println(objSoftEmployee1); System.out.println(objSoftEmployee2); System.out.println(objSoftEmployee3); } }
Output
Education Details : { Student Name='Mugilvannan G' Student Aadhar='1452-5874-5124-847' Student Institution='Mowbarys' Student Rollno='15425'} Career Details : {' Employee Name ='Mugilvannan G' Employee Aadhar ='1452-5874-5124-847' Employee pfAccountNo='4151542'} Present Work Details: {Software Engineer Name ='Mugilvannan G' Aadhar No ='1452-5874-5124-847' EmpId ='1001' Designation='Programmer' Organisation='Fuchsia Software' Practice='Classic ASP, HTML, CSS'} Present Work Details: {Software Engineer Name ='Mugilvannan G' Aadhar No ='1452-5874-5124-847' EmpId ='754185' Designation='Technology Analyst' Organisation='Infosys' Practice='Java, Spring Boot'} Present Work Details: {Software Engineer Name ='Mugilvannan G' Aadhar No ='1452-5874-5124-847' EmpId ='152' Designation='Technology Lead' Organisation='Cognizant' Practice='Java, Devops'}
Another example using prototype pattern
NiosPrototype.java
import java.util.Objects; //1.Create a Interface or abstract class with clone method abstract class NiosPrototype { public String engineCapacity; public String transmission; public NiosPrototype() { } public NiosPrototype(NiosPrototype niosPrototype) { if(niosPrototype != null) { this.engineCapacity = niosPrototype.engineCapacity; this.transmission = niosPrototype.transmission; } } public abstract NiosPrototype clone(); @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; NiosPrototype that = (NiosPrototype) o; return Objects.equals(engineCapacity, that.engineCapacity) && Objects.equals(transmission, that.transmission); } }
Asta.java
//2.Create Concrete prototype which implements the interface.This class is resource intensive which you dont want //to use new operator again and again rather call clone method to create it public class Asta extends NiosPrototype{ public String sideBags; @Override public NiosPrototype clone() { return new Asta(this); } public Asta() { } public Asta(Asta target) { super(target); if(target != null){ this.sideBags = target.sideBags; } } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; if (!super.equals(o)) return false; Asta asta = (Asta) o; return Objects.equals(sideBags, asta.sideBags); } }
CarModel.java
public class CarModel { public static void main(String[] args) { Asta objAsta1 = new Asta(); objAsta1.engineCapacity = "1196"; objAsta1.transmission = "Manual"; objAsta1.sideBags ="Available"; Asta objAsta2 = (Asta)objAsta1.clone(); if(objAsta1 == objAsta2){ System.out.println("Both objects are same"); } if(objAsta1.equals(objAsta2)){ System.out.println("Both objects are different but has same value"); } } }
Note:
- To create a copy of you the best person who can tell about you is none other than you, just like autobiography. So constructor takes instance of itself as parameter arguments
- There would be two constructor. Default constructor and cloning constructor
- Default constructor would be called first time object is created and cloning constructor would be called from clone method when the created object needs to be cloned
- There wont be any person who can tell your ancestral origin, likewise constructor would call super constructor at time of cloning
- The cloning constructor itself would be called from clone method