The Prototype design pattern is a creational design pattern used to
create objects based on a template or prototype instance. This pattern is
particularly useful when creating new instances of objects is expensive or
complicated, and you need to clone existing objects instead of creating new
ones from scratch.
When to use this pattern?
- If the cost of creating the object is expensive or complicated.
- When trying to keep the number of classes in an application to a minimum
- When adding or removing objects at runtime
- When the client application needs to be unaware of the object creation, composition and representation.
- Objects are required which are similar to the existing objects
Prototype Design Pattern Example
It would be easy to understand prototype design pattern with an example. Suppose we have an Object that loads data from database. Now we need to modify this data in our program multiple times, so it’s not a good idea to create the Object using new keyword and load all the data again from database.
The better approach would be to clone the existing object into a new object and then do the data manipulation.
Prototype design pattern mandates that the Object which you are copying should provide the copying feature. It should not be done by any other class. However, whether to use shallow or deep copy of the Object properties depends on the requirements and it’s a design decision.
Here is a sample program showing Prototype design pattern example in java.
Employees.java
public class Employees implements Cloneable{
private List empList;
public Employees(){
empList = new ArrayList();
}
public Employees(List list){
this.empList=list;
}
public void loadData(){
//read all employees from database and put into the list
empList.add("Sachin");
empList.add("Dravid");
empList.add("Laxman");
empList.add("Ganguly");
}
public List getEmpList() {
return empList;
}
@Override
public Object clone() throws CloneNotSupportedException{
List temp = new ArrayList();
for(String s : this.getEmpList()){
temp.add(s);
}
return new Employees(temp);
}
}
Notice that the clone method is overridden to provide a deep copy of the employees list.
Here is the prototype design pattern example test program that will show the benefit of prototype pattern.
PrototypePatternTest.java
public
class PrototypePatternTest {
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 list = empsNew.getEmpList();
list.add("Yuvaraj");
List list1 = empsNew1.getEmpList();
list1.remove("Sachin");
System.out.println("emps List:
"+emps.getEmpList());
System.out.println("empsNew
List: "+list);
System.out.println("empsNew1
List: "+list1);
}
}
Output of the above prototype design pattern example program is:
emps HashMap: [Sachin, Dravid, Laxman, Ganguly]
empsNew HashMap: [Sachin, Dravid, Laxman, Ganguly, Yuvaraj]
empsNew1 HashMap: [Dravid, Laxman, Ganguly]
If the object cloning was not provided, we will have to make database call to fetch the employee list every time. Then do the manipulations that would have been resource and time consuming.
Real-Time Use Cases
1. Configuration
Objects
Context: Applications that
require configuration objects with different settings can use the Prototype
pattern to clone a base configuration and then modify the clone as needed.
Example: In a software
configuration system, a default configuration object can be cloned and
customized for different environments (e.g., development, testing, production).
2. Document
Management Systems
Context: Document management
systems that deal with various templates can use the Prototype pattern to clone
documents or templates with predefined structures.
Example: An email client that
uses a prototype email template and clones it for sending multiple similar
emails with different content.
No comments:
Post a Comment