Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Factory Method Pattern


A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. In other words, subclasses are responsible to create the instance of the class.

The Factory Method Pattern is also known as Virtual Constructor.

Advantage of Factory Design Pattern

  • Factory Method Pattern allows the sub-classes to choose the type of objects to create.
  • It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code. That means the code interacts solely with the resultant interface or abstract class, so that it will work with any classes that implement that interface or that extends that abstract class.

Usage of Factory Design Pattern

  • When a class doesn’t know what sub-classes will be required to create
  • When a class wants that its sub-classes specify the objects to be created.
  • When the parent classes choose the creation of objects to its sub-classes.

UML for Factory Method Pattern

  • We are going to create a Plan abstract class and concrete classes that extends the Plan abstract class. A factory class GetPlanFactory is defined as a next step.
  • GenerateBill class will use GetPlanFactory to get a Plan object. It will pass information (DOMESTICPLAN / COMMERCIALPLAN / INSTITUTIONALPLAN) to GetPalnFactory to get the type of object it needs.

factorymethod

Calculate Electricity Bill : A Real World Example of Factory Method

Step 1: Create a Plan abstract class.

  1. import java.io.*;
  2. abstract class Plan{
  3.          protected double rate;
  4.          abstract void getRate();
  5.          public void calculateBill(int units){
  6.               System.out.println(units*rate);
  7.           }
  8. }//end of Plan class.

Step 2: Create the concrete classes that extends Plan abstract class.

  1. class  DomesticPlan extends Plan{
  2.         //@override
  3.          public void getRate(){
  4.              rate=3.50;
  5.         }
  6.    }//end of DomesticPlan class.
  1. class  CommercialPlan extends Plan{
  2.    //@override 
  3.     public void getRate(){
  4.         rate=7.50;
  5.    }
  6. /end of CommercialPlan class.
  1. class  InstitutionalPlan extends Plan{
  2.    //@override
  3.     public void getRate(){
  4.         rate=5.50;
  5.    }
  6. /end of InstitutionalPlan class.

Step 3: Create a GetPlanFactory to generate object of concrete classes based on given information..

  1. class GetPlanFactory{
  2.    //use getPlan method to get object of type Plan 
  3.        public Plan getPlan(String planType){
  4.             if(planType == null){
  5.              return null;
  6.             }
  7.           if(planType.equalsIgnoreCase(“DOMESTICPLAN”)) {
  8.                  return new DomesticPlan();
  9.                }
  10.            else if(planType.equalsIgnoreCase(“COMMERCIALPLAN”)){
  11.                 return new CommercialPlan();
  12.             }
  13.           else if(planType.equalsIgnoreCase(“INSTITUTIONALPLAN”)) {
  14.                 return new InstitutionalPlan();
  15.           }
  16.       return null;
  17.    }
  18. }//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.

  1. import java.io.*;
  2. class GenerateBill{
  3.     public static void main(String args[])throws IOException{
  4.       GetPlanFactory planFactory = new GetPlanFactory();
  5.       System.out.print(“Enter the name of plan for which the bill will be generated: “);
  6.       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  7.       String planName=br.readLine();
  8.       System.out.print(“Enter the number of units for bill will be calculated: “);
  9.       int units=Integer.parseInt(br.readLine());
  10.       Plan p = planFactory.getPlan(planName);
  11.       //call getRate() method and calculateBill()method of DomesticPaln.
  12.        System.out.print(“Bill amount for “+planName+” of  “+units+” units is: “);
  13.            p.getRate();
  14.            p.calculateBill(units);
  15.             }
  16.     }//end of GenerateBill class.

download this Electricity bill Example

Output

factorymethodoutput

Comments

comments