Prototype Design Pattern is used to create the objects by using clone method of object. In the enterprise application, when object creation is costly in terms of creating and initializing the initial properties of objects. If such type of object is already in your hand, then you go for prototype pattern. The prototype pattern comes under the creational design pattern family of GOF patterns in software development.
According to the Gang of Four:
Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.
This pattern involves implementing a prototype interface which tells to create a clone of the current object. This pattern is used when creation of object directly is costly. For example, an object is to be created after a costly database operation. We can cache the object, returns its clone on next request and update the database as and when needed thus reducing database calls.
Let’s see the following class diagram for the Prototype design pattern and it illustrates about the component classes:
The classes and objects participating in this pattern are:
Prototype (Account)
ConcretePrototype (SavingAccount, CurrentAccount)
Client (PrototypePatternMain)
also read:
There are following applicability where we have to Use the Prototype Pattern:
There are following pros of using the Prototype pattern:
There are following cons of using the Prototype pattern:
I am going to create an abstract class Account and concrete classes extending the Account class. A class AccountCache is defined as a next step which stores account objects in a HashMap and returns their clone when requested.
Account.java
package com.doj.patterns.creational.prototype; /** * @author Dinesh.Rajput * */ public abstract class Account implements Cloneable{ abstract public void accountType(); public Object clone() { Object clone = null; try { clone = super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return clone; } }
CurrentAccount.java
package com.doj.patterns.creational.prototype; /** * @author Dinesh.Rajput * */ public class CurrentAccount extends Account { @Override public void accountType() { System.out.println("CURRENT ACCOUNT"); } }
SavingAccount.java
package com.doj.patterns.creational.prototype; /** * @author Dinesh.Rajput * */ public class SavingAccount extends Account{ @Override public void accountType() { System.out.println("SAVING ACCOUNT"); } }
AccountCache.java
package com.doj.patterns.creational.prototype; import java.util.HashMap; import java.util.Map; /** * @author Dinesh.Rajput * */ public class AccountCache { public static Map<String, Account> accountCacheMap = new HashMap<>(); static{ Account currentAccount = new CurrentAccount(); Account savingAccount = new SavingAccount(); accountCacheMap.put("SAVING", savingAccount); accountCacheMap.put("CURRENT", currentAccount); } }
PrototypePatternMain.java
package com.doj.patterns.creational.prototype; /** * @author Dinesh.Rajput * */ public class PrototypePatternMain { public static void main(String[] args) { Account currentAccount = (Account) AccountCache.accountCacheMap.get("CURRENT").clone(); currentAccount.accountType(); Account savingAccount = (Account) AccountCache.accountCacheMap.get("SAVING") .clone(); savingAccount.accountType(); } }
CURRENT ACCOUNT SAVING ACCOUNT
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…