Design Patterns - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Prototype Pattern

Prototype Pattern

shape Description

Prototype Pattern is a sub-category of creational pattern that provides a better pattern for creating an object. In prototype pattern, the object can be cloned, thus making it one of the effective means to create an object. Prototype pattern is primarily used when the object creation is costly and expensive. In prototype pattern, the use of "New" keyword can interfere with the expected result. Objects are created by copying the prototype. Prototype pattern can be used along with the factory pattern and abstract factory pattern. This is an unique pattern in creational pattern.

shape Advantages

  • Object can be cloned easily.
  • Object can be customized as per the requirement.
  • Objects can be added and removed at run time.
  • Objects can be created easily.
  • In most cases, sub-classing is not required.

shape Conceptual figure

shape Examples

Consider an example "fetch the employee details from database and manipulate the data" [java] import java.util.ArrayList; import java.util.List; public class Employees implements Cloneable { private ListStringempList; public Employees() { empList = new ArrayList;String();// creates a array of strings } public Employees(ListString list) { this.empList=list; } public void loadData()// loads a set of strings { empList.add("Adam"); empList.add("John"); empList.add("Rock"); empList.add("Adward"); } public ListString getEmpList() { return empList; } public Object clone() throws CloneNotSupportedException { ListString temp = new ArrayListString(); for(String this.getEmpList()) { temp.add(s); } return new Employees(temp); } } public class PrototypePatternTest { public static void main(String[] args) throws CloneNotSupportedException { Employees emps = new Employees();//object is created for Employees class emps.loadData(); Employees empsNew = (Employees) emps.clone(); Employees empsNew1 = (Employees) emps.clone(); ListString list = empsNew.getEmpList(); list.add("Adam"); ListString list1 = empsNew1.getEmpList(); list1.remove("John"); System.out.println("emps List: "+emps.getEmpList()); System.out.println("empsNew List: "+list); System.out.println("empsNew1 List: "+list1); } }[/java] When an Object heaps information from the database, making changes to this information in the project by using the "New" watchword and loading all the information again from the database is not a good practice. So, the better approach is to clone the current item into another article and then perform the information control. Model outline design mainly deals with the Object that is being replicated and indicates the duplicating highlight. However, whether to utilize the shallow or profound duplicate of the object properties depends on the requirement and configuration choice.

shape Output

[java]emps HashMap: [Adam,John,Rock,Edward] empsNew HashMap: [Adam, John, Rock, Edward,Austin] empsNew1 HashMap: [John, Rock, Edward][/java]

Summary

shape Key Points

  • The complexity of resources and time becomes expensive if the object cloning is not used.
  • Memory management can be done easily.