Search This Blog

Saturday 18 March 2017

Prototype Design Pattern in Java

Prototype Design Pattern in Java

Intent
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

Motivation
When we talk about object creation there is a better way to have new objects. If the cost of creating a new object is large and creation is resource intensive, we clone the object. Prototyping allows an object to create customized objects without knowing their class or any details of how to create them. This pattern involves implementing a prototype interface which tells to create a clone of the current object. 
The best example to understand Prototype Patter is, the mitotic division of a cell — resulting in two identical cells —that plays an active role in copying itself and thus, demonstrates the Prototype pattern. When a cell splits, two cells of identical genotype result. In other words, the cell clones itself.

Applicability
Use the Prototype pattern when a system should be independent of how its products are created, composed, and represented; and
·         When the classes to instantiate are specified at run-time.
·         To avoid building a class hierarchy of factories that parallels the class hierarchy of products.
·         When instances of a class can have one of only a few different combinations of state.

Consequences
The Prototype pattern has several benefits:
·         Adding and removing products at run-time.
·         Specifying new objects by varying values.
·         Specifying new objects by varying structure.
·         Reduced subclassing.
·         Configuring an application with classes dynamically.



Implementation
import java.util.ArrayList;
import java.util.List;

public class Employees implements Cloneable{

      private List<String> empList;
     
      public Employees(){
            empList = new ArrayList<String>();
      }
     
      public Employees(List<String> list){
            this.empList=list;
      }
      public void loadData(){
//This part can be used to read data from the Database and add to List
            empList.add("Ravi");
            empList.add("Ajay");
            empList.add("Sumit");
            empList.add("Pooja");
      }
     
      public List<String> getEmpList() {
            return empList;
      }

      @Override
      public Object clone() throws CloneNotSupportedException{
                  List<String> temp = new ArrayList<String>();
                  for(String s : this.getEmpList()){
                        temp.add(s);
                  }
                  return new Employees(temp);
      }
     
}

import java.util.List;

public class TestPrototypePattern {

      public static void main(String[] args) throws CloneNotSupportedException {
            Employees emps = new Employees();
            emps.loadData();
           
            //Use the clone method to get the Employee object
            Employees empsNew = (Employees) emps.clone();
            Employees empsNew1 = (Employees) emps.clone();
            List<String> list = empsNew.getEmpList();
            list.add("Simran");
            List<String> list1 = empsNew1.getEmpList();
            list1.remove("Sumit");
           
      System.out.println("Existing Employee List: "+emps.getEmpList());
System.out.println("Employee List after adding an Employee : "+list);
System.out.println("Employee List after removing an Employee : "+list1);
      }
}

Output :
Existing Employee List: [Ravi, Ajay, Sumit, Pooja]
Employee List after adding an Employee : [Ravi, Ajay, Sumit, Pooja, Simran]
Employee List after removing an Employee : [Ravi, Ajay, Pooja]

1 comment: