Table of Contents
A Factory Design Pattern or Factory Method Pattern says that it only defines an interface or abstract class that creates an object. Subclasses are therefore responsible for creating the class case.
Factory Design Method is also known as Virtual Constructor.
We will create a plan that extends the Plan abstract class and concrete classes. The next move is to identify a GetPlanFactory factory class.
GetPlanFactory will be used to acquire a Project object in the GenerateBill package. It will transmit data to the GetPalnFactory to get the kind of object it needs. (DOMESTERS / COMMIRCIALPLAN / INSTITUTIONALPLEN).
Electricity Bill Calculate: A Global Factory Method Sample:
Phase-1: Construct an explicit class plan.
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.
Phase-2: Creating concrete classes to cover the abstract class of Plan.
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.
Phase-3: Build a GetPlanFactory to create artefacts of specific classes based on data.
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.
Phase-4: Generate the Bill by use of the GetPlanFactory to transmit data such as the DOMESTICPLAN, COMMERCIALPLAN or INSTITUTIONALPLAN plan form to the target in specific classes.
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.
Output:
You may be interested in reading about Machine Learning.
The digital revolution has propelled us into a cloud-based world. Businesses are increasingly migrating their… Read More
Serverless computing has become a game-changer in the cloud development landscape. By abstracting away server… Read More
The digital age has brought an explosion of data. From personal photos and documents to… Read More
In the ever-evolving landscape of cloud computing, selecting the right tools for your applications is… Read More
Cloud Computing is present and future of remote technologies. In today's dynamic business landscape, digital… Read More
NGINX Server Nginx, a popular open-source web server, excels at handling high traffic websites efficiently.… Read More