CREATIONAL PATTERN
Prototype pattern deals with encapsulating cloning process. As the name suggests, it is used to create prototypes by cloning, not real objects from scratch. This pattern can be used in situation where creating new object of class from scratch is costly operation, so instead it is cloned from existing objects. For example, for ome process need to prepare List of all account numbers associated with bank and customer are millions. In this case, it is costly to prepare such list object by reading through database, so such list is copied from existing list object.

Account.java
public class Account
implements Cloneable{
private String accountNo;
private String mailId;
private String firstName,lastName;
public String getAccountNo() {
return accountNo;
}
public void setAccountNo(String accountNo) {
this.accountNo = accountNo;
}
public String getMailId() {
return mailId;
}
public void setMailId(String mailId) {
this.mailId = mailId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Account(String accountNo, String firstName, String lastName, String mailId) {
// TODO Auto-generated constructor stub
this.accountNo=accountNo;
this.firstName = firstName;
this.lastName = lastName;
this.mailId = mailId;
}
}
AccountHolderList.java
import java.io.File;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class AccountHolderList implements Cloneable{
private ArrayList accounts;
public ArrayList getAccountsList(){
return accounts;
}
public AccountHolderList() {
// TODO Auto-generated constructor stub
accounts = new ArrayList();
}
public void loadListFromDatabase(){
//load accounts data from xml based database
try
{
//creating a constructor of file class and parsing an XML file
File file = new File("src/AccountsDatabase.xml");
//an instance of factory that gives a document builder
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//an instance of builder to parse the specified xml file
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
System.out.println("Root element: " + doc.getDocumentElement().getNodeName());
NodeList nodeList = doc.getElementsByTagName("Account");
// nodeList is not iterable, so we are using for loop
for (int itr = 0; itr < nodeList.getLength(); itr++)
{
Node node = nodeList.item(itr);
System.out.println("\nNode Name :" + node.getNodeName());
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) node;
String accountNo = eElement.getElementsByTagName("accountNo").item(0).getTextContent();
String firstName = eElement.getElementsByTagName("firstname").item(0).getTextContent();
String lastName = eElement.getElementsByTagName("lastname").item(0).getTextContent();
String mailId = eElement.getElementsByTagName("mailId").item(0).getTextContent();
System.out.println("accountNo: "+ accountNo);
System.out.println("First Name: "+ firstName);
System.out.println("Last Name: "+ lastName);
System.out.println("mail Id: "+ mailId);
Account accountData = new Account(accountNo, firstName, lastName, mailId);
accounts.add(accountData);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("Loaded accounts from database ::"+accounts.size());
}
public Object clone() {
Object clone = null;
try {
clone = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return clone;
}
}
PrototypePatternDemo.java
import java.util.ArrayList;
import java.util.Iterator;
public class PrototypePatternDemo {
public static AccountHolderList accountHolderList;
static{
//load accounts from database at time of system initiliazation
accountHolderList = new AccountHolderList();
accountHolderList.loadListFromDatabase();
}
public PrototypePatternDemo() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
//clone accounts List for sending mail, instrad of reading from database
AccountHolderList clonedList = (AccountHolderList)accountHolderList.clone();
ArrayList accList =clonedList.getAccountsList();
Iterator listItr = accList.iterator();
Account acc;
while(listItr.hasNext()){
acc = (Account)listItr.next();
System.out.println("wished Happy New year to ::" + acc.getMailId());
}
}
}
Output
