Prototype design pattern in Java

By | September 3, 2023

The prototype design pattern is a Creational design pattern that deals with object creation. The main idea behind the prototype design pattern is to create an object by copying an existing object, known as a prototype, instead of creating a new object from scratch.

When to use Prototype design pattern?

  • It is used when creating an object is costlier than cloning an existing object (involve complex initialization such as network connections etc.).
  • When we want to add or remove the features from the object dynamically.
  • If objects have similar shared properties, we can avoid duplicating data by creating their prototype.

Example

Let us understand the prototype design pattern with the help of a network connection example. In this example, we will setup a dummy network connection and use threads to make this connection take a few seconds of time. After this, we will create its prototype, which will take less time to create an object.

Steps to implement Prototype design pattern in Java

Step 1- Create NetworkConnection class

In this step, we will create a NetworkConnection class, implement the Cloneable interface, and override the clone() method to create the clone of the object.

Java
/**
 * Prototype Design Pattern
 * 
 * @author paulsofts
 * */
public class NetworkConnection implements Cloneable {
	
	private String ip;
	private String connection;
	
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	public String getConnection() {
		return connection;
	}
	public void setConnection(String connection) {
		this.connection = connection;
	}
	
	//this method is used to setup the connection
	//we will use Thread.sleep() to make this took a few seconds of time
	public void connectionInfo(String info) throws InterruptedException {
		this.connection = info;
		Thread.sleep(2000);
	}
	
	@Override
	public String toString() {
		return "NetworkConnection [ip=" + ip + ", connection=" + connection + "]";
	}
	
	//Overriding the clone method, In order to create the clone
	//or copy of the object
	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
}

Step 2- Create Main class

Now, we create our main class to execute our prototype design pattern. In our Main class, first we create a new instance of our NetworkConnection object, and we measure the time taken to create the object. Afterward, we create its copy and measure the time it took, and this way we can check, how prototype design patterns help us create complex objects.

Java
/**
 * Prototype Design Pattern
 * 
 * @author paulsofts
 * */
public class Main {
	
	public static void main(String[] args) throws CloneNotSupportedException, InterruptedException {
		
		double t1 = System.currentTimeMillis();
		NetworkConnection conn = new NetworkConnection();
		conn.setIp("192.168.1.1");
		conn.connectionInfo("Wi-Fi");
		System.out.println(conn);
		System.out.println("Time taken to create new NetwrokConnection object " + (System.currentTimeMillis() - t1));
		
		double t2 = System.currentTimeMillis();
		NetworkConnection conn_copy = (NetworkConnection)conn.clone();
		System.out.println(conn_copy);
		System.out.println("Time taken to create clone of NetwrokConnection object " + (System.currentTimeMillis() - t2));
		
	}
}

Below, we can see the difference between creating an object from scratch and making a copy from existing objects.

Prototype design pattern
Fig 1- Prototype design pattern

Leave a Reply

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