This is the 21st day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

I am xiao Hei, a programmer in the Internet “casual”.

Water does not compete, you are in the flow

preface

The prototype design pattern is one of the creative design patterns used to provide a mechanism for creating objects.

Other creative Design Patterns

  • Singleton design pattern
  • The factory pattern
  • Abstract Factory pattern
  • Builder model

When an object creation factory is complex, requires a lot of time and resources, becomes expensive each time an object is created, and when there is already a similar object, prototyping patterns can be used to reduce the cost of object creation and save resources.

The stereotype pattern provides a mechanism to copy original objects into new objects and then modify them as needed. The prototyping pattern in Java uses Clone () to copy objects.

scenario

It might be easier to understand the prototyping pattern, for example, if we have an object that is a resource object loaded from a database and now needs to be modified or used multiple times in the application. So it’s not a good idea to reload the object from the database every time or to recreate it using the new keyword.

A better approach is to clone an existing object into a new one and then manipulate the data.

The prototyping pattern requires that objects to be copied must provide replicable characteristics. Objects of any other class should not provide properties that can be copied. When cloning objects, the use of shallow or deep copies of object properties depends on the requirements and design of the scenario.

Prototype design pattern code structure

Here is an example of code that uses the prototyping pattern:

package com.heiz.design.prototype;

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

public class Employees implements Cloneable{

	private List<String> empList;
	
	public Employees(a){
		empList = new ArrayList<String>();
	}
	
	public Employees(List<String> list){
		this.empList=list;
	}
	public void loadData(a){
		// Simulate loading data from a database
		empList.add("Black says Java.");
		empList.add("Black says Python.");
		empList.add("Black says JavaScript.");
		empList.add("Hei says PHP");
	}
	
	public List<String> getEmpList(a) {
		return empList;
	}
	
    // The clone method is used to copy objects
	@Override
	public Object clone(a) throws CloneNotSupportedException{
			List<String> temp = new ArrayList<String>();
			for(String s : this.getEmpList()){
				temp.add(s);
			}
			return newEmployees(temp); }}Copy the code

Note the clone() method in the code for a deep copy of Employees.

Next I’ll look at using a simple test code to see how to use the prototyping pattern:

package com.heiz.design.test;

import java.util.List;
import com.heiz.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 clone a new object
		Employees empsNew = (Employees) emps.clone();
		Employees empsNew1 = (Employees) emps.clone();
		List<String> list = empsNew.getEmpList();
		list.add("Black says HTML.");
		List<String> list1 = empsNew1.getEmpList();
		list1.remove("Black says Go.");
		
		System.out.println("emps List: "+emps.getEmpList());
		System.out.println("empsNew List: "+list);
		System.out.println("empsNew1 List: "+list1); }}Copy the code

That’s all for prototyping mode, if it helps, give me a thumbs up!