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.
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.
Conceptual
figure
Client: Client is used to create a new object and sends a request to a prototype to clone by itself.
Prototype: Prototype is used to declare an interface and make it to clone by itself.
ConcretePrototype - Implements the operation for cloning itself.
The process of cloning starts with an initialized and instantiated class. The Client asks for a new object of that type and sends a request to the Prototype class. Based on the type of object that is needed, the ConcretePrototype will handle the cloning through the Clone() method, thus making a new instance for itself.
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.
Output
[java]emps HashMap: [Adam,John,Rock,Edward]
empsNew HashMap: [Adam, John, Rock, Edward,Austin]
empsNew1 HashMap: [John, Rock, Edward][/java]
Summary
Key Points
The complexity of resources and time becomes expensive if the object cloning is not used.