Brief Introduction of Factory Method Pattern

Posted By : Avnish Pandey | 31-May-2019

Introduction:
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.

 

Prerequisites:
For creating the sample program of Observer design pattern you should install the Java 1.7+.

 

Usage of the Factory Design Pattern:
1. When a class doesn't know what sub-classes will be required to create
2. When a class wants that its sub-classes specify the objects to be created.
3. When the parent classes choose the creation of objects to its sub-classes.

Get ready to make some loosely coupled object-oriented designs. There is more to making objects than just using the new operator. You’ll learn that object creation is an activity that shouldn’t always be done in public and can often lead to tight-coupling problems. And you don’t want that, do you?

 

Find out how Factory Patterns can help save you from embarrassing dependencies:

1. Factory Method Design Pattern is also known as "Virtual Constructor".
2. This Pattern allows the child-classes to choose the type of objects to create.
3. Promotes loose-coupling by eliminating.

 

Step 1: Create a Planning abstract class.

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

    public void calculateBill(int units){
        System.out.println(units*rate);
    }
}

 

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

class  DomesticPlanning extends Planning{
    //@override  
    public void getRate(){
        rate=3.50;
    }
}

class  CommercialPlanning extends Planning{
    //@override   
    public void getRate(){
        rate=7.50;
    }
}

 

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

class GetPlanningFactory{
public static final String DOMESTIC_PLANNING="DOMESTICPLANNING";
public static final String COMMERCIAL_PLANNING="COMMERCIALPLANNING";
    //use getPlanning method to get object of type Planning   
    public Planning getPlanning(String planningType){
        if(planningType == null){
            return null;
        }
        if(planningType.equalsIgnoreCase(DOMESTIC_PLANNING)) {
            return new DomesticPlanning();
        }
        else if(planningType.equalsIgnoreCase(COMMERCIAL_PLANNING)){
            return new CommercialPlanning();
        }
        return null;
    }
}

 

Step 4: Generate Bill by using the GetPlanningFactory to get the object of concrete classes by passing an information such as type of plan Domestic Planing or Commercail Plainning.

import java.io.*;
class GenerateBill{
    public static void main(String args[])throws IOException{
        GetPlanningFactory planningFactory = new GetPlanningFactory();
        System.out.print("Enter the name of plan for which the bill will be generated:");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String planningName=br.readLine();
        System.out.print("Enter the number of units for bill will be calculated: ");
        int units=Integer.parseInt(br.readLine());
        Planning p = planningFactory.getPlanning(planningName);
        //invoke getRate() method and calculateBill()method of DomesticPalnning.  
        System.out.print("Bill amount for "+planningName+" of  "+units+" units is: ");
        p.getRate();
        p.calculateBill(units);
    }
}

 

 

Thanks

 

 

About Author

Author Image
Avnish Pandey

Avnish has a good knowledge in core & advance Java, Spring and Hibernate Framework. He loves to learn new technologies.

Request for Proposal

Name is required

Comment is required

Sending message..