Prototype pattern

Prototype pattern overview


It is one of the creational design patterns in Java as it is used to create an object.

This pattern helps in creating objects based on the template of an existing object using cloning.

Instead of creating an object from scratch each and every time, we can create the copies of original object.

We can use the copied object and modify it as and when required.

This pattern is already used by java in Cloneable interface.

Cloneable interface acts as a marker interface to indicate what objects are eligible for cloning

Object class has already provided a definition for clone () method and any class in java can use it or it can override this clone () method to provide its own implementation.

The prototype design pattern mandates that the class(whose objects needs to be created again and again) should provide the copying feature.
It should not be done by an external utility class.

While implementing this pattern, we may use clone method provided by Java,
But decide based on requirement whether we need to use shallow cloning or deep cloning and implement clone() method of your own accordingly.

Prototype_pattern

Let’s see the Pattern implementation


Step 1
First define a prototype interface by declaring a clone method
This is actually called as “Prototype”

Step 2
Define the multiple concrete prototype classes which implements the “Prototype” interface and define the clone() method as per the need

Step 3
Add all the prototypes to a prototype registry

Step 4
Let the client use the registry to access prototype instances

Requirement :
Create Permanent Employee and Contract employee instances based on the input passed and create these objects just by cloning as its creation is costly process.

Detailed Solution for the above requirement

Step 1

First define a prototype interface by declaring a clone method

  1. Public interface Employee{
  2. String name;
  3. String empId;
  4. Float salary;
  5. Employee clone();
  6. }
Public interface Employee{
String name;
String empId;
Float salary;
Employee clone();
}

Step 2

Define multiple concrete prototype classes which implements the “Prototype” interface and define the clone() method as per the need

  1. Public class PermanentEmployee implements Employee{
  2. float bonus;
  3. public Employee clone(){
  4. Employee e = new PermanentEmployee();
  5. e.name="John";
  6. e.empId="p10001";
  7. e.salary=24500.50f;
  8. e.bonus=10250.50f;
  9. return e;
  10. }
  11. @Override
  12.     public String toString() {
  13.         return empId;
  14.     }
  15.  
  16. }
Public class PermanentEmployee implements Employee{
float bonus;
public Employee clone(){
Employee e = new PermanentEmployee();
e.name="John";
e.empId="p10001";
e.salary=24500.50f;
e.bonus=10250.50f;
return e;
}
@Override
    public String toString() {
        return empId;
    }

}

  1. Public class ContractorEmployee implements Employee{
  2. int contractYears;
  3. public Employee clone(){
  4. Employee e = new ContractorEmployee();
  5. e.name="Peter";
  6. e.empId="c10001";
  7. e.salary=18500.50f;
  8. e. contractYears =2;
  9. return e;
  10. }
  11. @Override
  12.     public String toString() {
  13.         return empId;
  14.     }
  15.  
  16. }
Public class ContractorEmployee implements Employee{
int contractYears;
public Employee clone(){
Employee e = new ContractorEmployee();
e.name="Peter";
e.empId="c10001";
e.salary=18500.50f;
e. contractYears =2;
return e;
}
@Override
    public String toString() {
        return empId;
    }

}

Step 3

Add all the prototypes to a prototype registry

  1. Public class EmployeeRegistry{
  2. Private static Map<String,Employee> prototypes = new HashMap<>();
  3. Static{
  4. prototypes.put("pEmp",new PermanentEmployee());
  5. prototypes.put("cEmp",new ContractorEmployee());
  6. }
  7. Public Employee getEmployee(String type){
  8. try{
  9. return prototypes.get(type).clone();
  10.  
  11. }
  12. catch (NullPointerException e) {
  13.             System.out.println("Prototype with name: " + type + ", doesn't exist");
  14.             return null;
  15.         }
  16.  }  //close of method
  17. } //close of class
Public class EmployeeRegistry{
Private static Map<String,Employee> prototypes = new HashMap<>();
Static{
prototypes.put("pEmp",new PermanentEmployee());
prototypes.put("cEmp",new ContractorEmployee());
}
Public Employee getEmployee(String type){
try{
return prototypes.get(type).clone();

}
catch (NullPointerException e) {
            System.out.println("Prototype with name: " + type + ", doesn't exist");
            return null;
        }
 }  //close of method
} //close of class

Step 4

Let the client use the registry to access prototype instances

  1. public class PrototypePatternClient
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.             Employee  pEmp  = EmployeeRegistry.getEmployee("pEmp");
  6.             System.out.println(pEmp);
  7.  
  8.            Employee  cEmp  = EmployeeRegistry.getEmployee("cEmp");
  9.             System.out.println(cEmp);
  10.  
  11. //Modify the cloned object as per need
  12.            Employee  cEmp1  = EmployeeRegistry.getEmployee("cEmp");
  13. cEmp1.contractYears=3;
  14. //Modify any other fields if required for new object
  15.    
  16.     }
  17. }
public class PrototypePatternClient
{
    public static void main(String[] args)
    {
            Employee  pEmp  = EmployeeRegistry.getEmployee("pEmp");
            System.out.println(pEmp);
 
           Employee  cEmp  = EmployeeRegistry.getEmployee("cEmp");
            System.out.println(cEmp);

//Modify the cloned object as per need
           Employee  cEmp1  = EmployeeRegistry.getEmployee("cEmp");
cEmp1.contractYears=3;
//Modify any other fields if required for new object
    
    }
}



When to use this pattern?


When creating an object is a costly and time-consuming operation and, in that case, copying an object would be easier.

New objects required are almost similar to the existing objects.

Note:
We should always have good knowledge about the data of the object that is to be cloned and also make sure that cloned instance allows us to make changes to the data. If not, after cloning we will not be able to make required changes to the cloned object to get the object as per the need.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on