Prototype Design Pattern
This(Prototype design pattern) is used in conditions where application needs to create or generate a number of instances of a class, which is having similar or slightly different state. Any details regarding Prototype design pattern please refer below:
In the below example, we are creating our own prototype pattern which is having createClone() method.
1. PublicSectorBanks.java
package com.gaurav.designpattern.prototype;
/**
* @author gaurav
*
*/
* @author gaurav
*
*/
public interface PublicSectorBanks extends Cloneable{
public PublicSectorBanks createClone() throws CloneNotSupportedException;
}
Below two classes which is BankOfIndia and StateBankOfIndia is implementing the PublicSectorBanks interface which is having createClone() method.
2. BankOfIndia.java
package com.gaurav.designpattern.prototype;
/**
* @author gaurav
*
*/
* @author gaurav
*
*/
public class BankOfIndia implements PublicSectorBanks{
private String bankName = null;
public String getBankName() {
return bankName;
}
public void setBankName(String bankName) {
this.bankName = bankName;
}
@Override
public PublicSectorBanks createClone() throws CloneNotSupportedException {
System.out.println("Clone creation for BankOfIndia - A Public Sector Bank.");
return (BankOfIndia)super.clone();
}
public String toString(){
return "Bank Of India";
}
}
3. StateBankOfIndia.java
package com.gaurav.designpattern.prototype;
/**
* @author gaurav
*
*/
* @author gaurav
*
*/
public class StateBankOfIndia implements PublicSectorBanks{
private String bankName = null;
public String getBankName() {
return bankName;
}
public void setBankName(String bankName) {
this.bankName = bankName;
}
@Override
public PublicSectorBanks createClone() throws CloneNotSupportedException {
System.out.println("Clone creation for State Bank Of India - A Public Sector Bank.");
return (StateBankOfIndia)super.clone();
}
public String toString(){
return "State Bank Of India";
}
}
In the demonstration class, First we are creating original object(i.e. boi1, sbi1) and then creating cloned object(i.e. boi2, sbi2) using that original object.
4. PrototypeDesignPatternDemo.java
package com.gaurav.designpattern.prototype;
/**
* @author gaurav
*
*/
* @author gaurav
*
*/
public class PrototypeDesignPatternDemo {
public static void main(String args[]){
try {
BankOfIndia boi1 = new BankOfIndia();
System.out.println("Original Object : "+boi1);
System.out.println("----------------------");
BankOfIndia boi2 = (BankOfIndia)boi1.createClone();
System.out.println("Cloned Object : "+boi2);
System.out.println("\n#########################################################");
StateBankOfIndia sbi1 = new StateBankOfIndia();
System.out.println("\nOriginal Object : "+sbi1);
System.out.println("----------------------");
StateBankOfIndia sbi2 = (StateBankOfIndia)sbi1.createClone();
System.out.println("Cloned Object : "+sbi2);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
Result:-
Original Object : Bank Of India
----------------------
Clone creation for BankOfIndia - A Public Sector Bank.
Cloned Object : Bank Of India
#########################################################
Original Object : State Bank Of India
----------------------
Clone creation for State Bank Of India - A Public Sector Bank.
Cloned Object : State Bank Of India
No comments:
Post a Comment