Professional Documents
Culture Documents
1) Creational design patterns: These are situations in taking decisions at the time of creating objects for
the classes.
1a) Factory method pattern: It is also called virtual constructor. It demonstrates defining of abstract
class or interface but let which subclasses are instantiated for calling parent's methods.
Example:
GetPlanFactory is the factory method pattern decides which subclass object for Plan abstract class to be
invoked to be determined based on input.
import java.io.*;
abstract class Plan{
protected double rate;
abstract void getRate();
class GetPlanFactory{
1b) Abstract Factory method: It is one on which many factory methods are to be created. It is also
called kit.
Example:
The abstract class AbstractFactory is defined over which few factory classes called BankFactory and
LoanFactory are defined.
Example of Abstract Factory Pattern
Here, we are calculating the loan payment for different banks like HDFC, ICICI, SBI etc.
*/
double EMI;
int n;
n=years*12;
rate=rate/1200;
EMI=((rate*Math.pow((1+rate),n))/((Math.pow((1+rate),n))-1))*loanamount;
System.out.println("your monthly EMI is "+ EMI +" for the amount"+loanamount+" you have
borrowed");
}
}// end of the Loan abstract class.
Step 4: Create concrete classes that extend the Loan abstract class..
}
Step 7: Create a FactoryCreator class to get the factories by passing an information such as Bank or Loan.
class FactoryCreator {
public static AbstractFactory getFactory(String choice){
if(choice.equalsIgnoreCase("Bank")){
return new BankFactory();
} else if(choice.equalsIgnoreCase("Loan")){
return new LoanFactory();
}
return null;
}
}//End of the FactoryCreator.
Step 8: Use the FactoryCreator to get AbstractFactory in order to get factories of concrete classes by
passing an information such as type.
import java.io.*;
class AbstractFactoryPatternExample {
public static void main(String args[])throws IOException {
System.out.print("Enter the name of Bank from where you want to take loan amount: ");
String bankName=br.readLine();
System.out.print("\n");
System.out.print("Enter the type of loan e.g. home loan or business loan or education loan : ");
String loanName=br.readLine();
AbstractFactory bankFactory = FactoryCreator.getFactory("Bank");
Bank b=bankFactory.getBank(bankName);
System.out.print("\n");
System.out.print("Enter the interest rate for "+b.getBankName()+ ": ");
double rate=Double.parseDouble(br.readLine());
System.out.print("\n");
System.out.print("Enter the loan amount you want to take: ");
double loanAmount=Double.parseDouble(br.readLine());
System.out.print("\n");
System.out.print("Enter the number of years to pay your entire loan amount: ");
int years=Integer.parseInt(br.readLine());
System.out.print("\n");
System.out.println("you are taking the loan from "+ b.getBankName());
1c) Singleton pattern: It is nothing but creating one object to the class. The examples are illustrated as
below:
Example1: Calling instance of a class using getObject() available in the root class.
class Pri_con{
private Pri_con(){
}
void show(){
System.out.println(" welcome to design patterns");
}
}
class SingletonPattern1{
private Box(){
}
void show(){
System.out.println(" it is our Box");
}
int volume(){
int v=x*y*z;
return v;
}
}
class SigletonPattern2{
public static void main(String[] klu){
b1.show();
int vol=b1.volume();
System.out.println("its volume="+ vol);
}
}
Output:
Box
It is our box
Its volume= 2000
1d) Prototype pattern: It is nothing but creating copy to the original but new object may have
modifications. Operations supported are cloning.
Example:
public string Id
{
get { return _id; }
}
// constructors/getters