0% found this document useful (0 votes)
8 views

Factory Pattern Example

The document outlines a Java implementation of a factory method design pattern for calculating electricity bills. It includes an abstract class 'Plan' and concrete classes 'DomesticPlan', 'CommercialPlan', and 'InstitutionalPlan' that define different rates. A 'GetPlanFactory' class is used to create instances of these plans based on user input, and a 'GenerateBill' class facilitates the bill calculation process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Factory Pattern Example

The document outlines a Java implementation of a factory method design pattern for calculating electricity bills. It includes an abstract class 'Plan' and concrete classes 'DomesticPlan', 'CommercialPlan', and 'InstitutionalPlan' that define different rates. A 'GetPlanFactory' class is used to create instances of these plans based on user input, and a 'GenerateBill' class facilitates the bill calculation process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Calculate Electricity Bill : A Real World Example of Factory Method

Step 1: Create a Plan abstract class.

import java.io.*;
abstract class Plan{
protected double rate;
abstract void getRate();

public void calculateBill(int units){


System.out.println(units*rate);
}
}//end of Plan class.
Step 2: Create the concrete classes that extends Plan abstract class.

class DomesticPlan extends Plan{


//@override
public void getRate(){
rate=3.50;
}
}//end of DomesticPlan class.
class CommercialPlan extends Plan{
//@override
public void getRate(){
rate=7.50;
}
/end of CommercialPlan class.
class InstitutionalPlan extends Plan{
//@override
public void getRate(){
rate=5.50;
}
/end of InstitutionalPlan class.
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;
}
}//end of GetPlanFactory class.
Step 4: Generate Bill by using the GetPlanFactory to get the object of concrete
classes by passing an information such as type of plan DOMESTICPLAN or
COMMERCIALPLAN or INSTITUTIONALPLAN.

import java.io.*;
class GenerateBill{
public static void main(String args[])throws IOException{
GetPlanFactory planFactory = new GetPlanFactory();

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.

You might also like