Prototype Design Pattern

  • Facebook
  • Google
  • LinkedIn
  • Twitter

The Prototype Pattern helps in creating new object instances by cloning existing object instances instead of creating new object using new keyword and can also be customized as per the application requirement.

Advantages of Prototype design pattern

  • In this way you can create object faster with its state already being initialized.
  • It is helpful for large and resource intensive object instantiation.
  • It reduced sub-classing unlike factory method.
  • You can add or remove objects at runtime.

When to use Prototype design pattern

  • When Classes to be instantiated are specified at run-time.
  • It is used in scenarios where construction of the object is costly affair using ‘new’ keyword is more than the cloning an existing regular object.
  • It is used when you have complex objects like in Trade, Bank so, then you can create a basic default object and on runtime can just clone it and do some changes as per requirement.
  • It is used if the objects to be created can only have one of a fixed number of states, then in this scenario making prototypes of these objects and cloning them will be more convenient

Prototype design pattern diagram:

prototype design pattern uml

Here is the Trading Account example which shows how to create protoyype design pattern

Create a prototype class TradingAccount.java

package com.javatutsworld.prototypedesignpattern;

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

public abstract class TradingAccount implements Cloneable {

	private String name;
	private long balance;
	private List<String> transactions;
	private long accountNo;

	TradingAccount(long accNo) {
		this.accountNo = accNo;
	}

	public long getAccountNo() {
		return accountNo;
	}

	public void setAccountNo(long accountNo) {
		this.accountNo = accountNo;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getBalance() {
		return balance;
	}

	public void setBalance(long balance) {
		this.balance = balance;
	}

	public List<String> getTransactions(long accNo) {
		// Here fetch account related info from DB for demo purpose you can set hard coding values.

		setName("Mohd Saaim");
		setBalance(10000);
		transactions = new ArrayList<String>();
		transactions.add("first transaction");
		transactions.add("second transaction");

		return transactions;
	}

	public void setTransactions(List<String> transactions) {
		this.transactions = transactions;
	}

	public Object clone() {
		Object clone = null;
		try {
			clone = super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}

		return clone;
	}

	public String toString() {
		return name + " " + balance + " " + transactions;
	}

}

Create concrete prototypes StandardAccount.java

package com.javatutsworld.prototypedesignpattern;

public class StandardAccount extends TradingAccount {

	StandardAccount(long accNo) {
		super(accNo);
	}

}

Create concrete prototypes MiniAccount.java


package com.javatutsworld.prototypedesignpattern;

public class MiniAccount extends TradingAccount {

	MiniAccount(long accNo) {
		super(accNo);
	}

}

Create PrototypePatternTestClient.java to test this Prototype Design Pattern

package com.javatutsworld.prototypedesignpattern;

import java.util.List;

public class PrototypePatternTestClient {

	public static void main(String[] args) {
		TradingAccount account1 = new StandardAccount(11111);
		List<String> list = account1.getTransactions(account1.getAccountNo());
		account1.setTransactions(list);
		System.out.println("Display account information of standard account1: "+account1.toString());

		// Here you can clone the object and can do some operation on current state of the object account1
		TradingAccount account2 = (StandardAccount) account1.clone();
		System.out.println("Display account information of account2 after cloning standard account1: "+account2.toString());
		
		// now you can do some manipulation on this object account2
		List<String> list1=account2.getTransactions(account2.getAccountNo());
		list1.clear();
		account2.setName("Mohd Aariz");
		account2.setBalance(20000);
		list1.add("updated first transaction");
		list1.add("updated second transaction");
		
		account2.setTransactions(list1);
		
		System.out.println("Display account information after update standard account2: "+account2.toString());
		
		// same thing you can clone and test for MiniAccount
	}
}

Output
Display account information of standard account1: Mohd Saaim 10000 [first transaction, second transaction]
Display account information of account2 after cloning standard account1: Mohd Saaim 10000 [first transaction, second transaction]
Display account information after update standard account2: Mohd Aariz 20000 [updated first transaction, updated second transaction]
Here is method which uses Prototype Design Pattern in JDK
  • java.lang.Object#clone() (the class has to implement java.lang.Cloneable)
For more details please see
Head First Design Patterns
      Advertisements