Factory Design Pattern In Java
Chapter:
Miscellaneous
Last Updated:
11-05-2021 09:55:59 UTC
Program:
/* ............... START ............... */
//Calculate Electricity bill a real world Example.
// Step1
//We are going to create a Plan abstract class.
/*we are using io concept in our class that's why we are importing io pacakage.*/
import java.io.*;
abstract class Plan
{
protected double rate;
abstract void getRate();
public void calculateBill(int units)
{
System.out.println(units * rate);
}
}
// Step 2
// We are going to create a Concrete classes that extends Plan abstract class.
class DomesticPlan extends Plan {
// @override
public void getRate() {
rate = 3.50;
}
}
class CommercialPlan extends Plan {
// @override
public void getRate() {
rate = 7.50;
}
}
class InstitutionalPlan extends Plan {
// @override
public void getRate() {
rate = 5.50;
}
}
// Step 3
// Create a GetPlanFactory to generate object of concrete classes based on given
// information.
class GetPlanFactory {
// use getPlan method to get object of type Plan
public Plan getPlan(String planType) {
if (planType == null) {
return null;
}
if (planType.equalsIgnoreCase("DOMESTICPLAN")) {
return new DomesticPlan();
}
else if (planType.equalsIgnoreCase("COMMERCIALPLAN")) {
return new CommercialPlan();
}
else if (planType.equalsIgnoreCase("INSTITUTIONALPLAN")) {
return new InstitutionalPlan();
}
return null;
}
}
// Step 4
// Use the GetPlanFactory to get the object of concrete classes by passing an
// information such as type(DOMESTICPLAN/COMMERCIALPLAN/INSTITUTIONALPLAN).
class GenerateBill {
public static void main(String args[]) throws IOException {
GetPlanFactory planFactory = new GetPlanFactory();
// get an object of DomesticPaln and call its getPlan()method.But we want to
// calculate the bill for one plan at time not all.for this we IO concept.
System.out.print("Enter the name of plan for which the bill will be generated: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String planName = br.readLine();
System.out.print("Enter the number of units for bill will be calculated: ");
int units = Integer.parseInt(br.readLine());
Plan p = planFactory.getPlan(planName);
// call getRate() method and calculateBill()method of DomesticPaln.
System.out.print("Bill amount for " + planName + " of " + units + " units is: ");
p.getRate();
p.calculateBill(units);
}
}// end of GenerateBill class.
/* ............... END ............... */
Output
Enter the name of plan for which the bill will be generated : commercialplan
Enter the number of units for bill will be calculated : 500
Bill amount for commercialplan of 500 units is : 3750.0
Notes:
-
First thing is that, when you are developing library or APIs which in turn will be used for further application development, then factory method is one of the best selections for creation pattern. Reason behind; We know that when to create an object of required functionality(s) but type of object will remain undecided or it will be decided ob dynamic parameters being passed.
- Below are the advantages of Java factory design method.
- 1.The object that you create can be used without duplication of code.
- 2.Factory pattern through inheritance provides abstraction between implementation and the client classes.
- 3.Factory method removes the instantiation of the implementation classes from the client code.
Tags
Factory Design Pattern, Factory Method in java