The Java language implements the Prototype Design Pattern.

The Prototype design pattern falls under the category of Creational Design patterns, offering a means of creating objects.

The Prototype Design Pattern is a widely used pattern for designing prototypes.

The Prototype design pattern is employed when creating an Object is expensive in terms of time and resources, but there is already a similar object available. This pattern allows us to duplicate the original object and make necessary modifications as per our requirements. The Java cloning technique is used in the Prototype design pattern for object duplication.

An example of the Prototype Design Pattern.

Understanding the prototype design pattern can be made easier through an example. Consider an Object that retrieves data from a database. If we need to make multiple modifications to this data in our program, creating a new Object using the “new” keyword and reloading all the data from the database each time is not efficient. A better approach would be to clone the existing object into a new object and then manipulate the data. The prototype design pattern requires that the Object being copied should provide the cloning feature, and it should not be done by any other class. However, whether a shallow or deep copy of the Object properties should be used depends on the requirements and a design decision. The following is a Java program that demonstrates an example of the prototype design pattern using the Employees.java class.

package com.scdev.design.prototype;

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(){
		//read all employees from database and put into the list
		empList.add("Pankaj");
		empList.add("Raj");
		empList.add("David");
		empList.add("Lisa");
	}
	
	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);
	}
	
}

Take note that the clone method has been redefined in order to create a thorough replica of the employees list. Below is an instance of a test program for the prototype design pattern that illustrates the advantages of the prototype pattern. The name of the file is PrototypePatternTest.java.

package com.scdev.design.test;

import java.util.List;

import com.scdev.design.prototype.Employees;

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<String> list = empsNew.getEmpList();
		list.add("John");
		List<String> list1 = empsNew1.getEmpList();
		list1.remove("Pankaj");
		
		System.out.println("emps List: "+emps.getEmpList());
		System.out.println("empsNew List: "+list);
		System.out.println("empsNew1 List: "+list1);
	}

}

The result of the program using the given prototype design pattern example is:

emps List: [Pankaj, Raj, David, Lisa]
empsNew List: [Pankaj, Raj, David, Lisa, John]
empsNew1 List: [Raj, David, Lisa]

If object cloning was not available, we would need to fetch the employee list from the database every time and perform resource-intensive manipulations. This concludes the prototype design pattern in Java.

 

 

more  tutorials

The Command design pattern.(Opens in a new browser tab)

Strategy Design Pattern in Java tutorial(Opens in a new browser tab)

Ensuring thread safety in Java Singleton Classes(Opens in a new browser tab)

How to include items to a list in Python(Opens in a new browser tab)

Server Configurations Frequently Used for Your Web Application(Opens in a new browser tab)

 

 

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds