Factory Design Pattern In Java

Chapter: Miscellaneous Last Updated: 11-05-2021 09:55:59 UTC


            /* ............... 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

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: ");

}// end of GenerateBill class.

                /* ............... END ............... */


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
Factory Design Pattern In Java


  • 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.


Factory Design Pattern, Factory Method in java

Similar Programs Chapter Last Updated
How To Create A Git Repository | Git repository commands Miscellaneous 31-07-2021
Currency Formatter In Java | How To Format Currency In Java Miscellaneous 19-07-2021
Data Types In Java Miscellaneous 09-06-2018
Java Program To Find Largest Element Of Array Miscellaneous 21-09-2017
Java Program To Check Whether Number Is Prime Or Not Miscellaneous 08-09-2017
Java Program To Find Power Of Number Using For Loop Miscellaneous 29-08-2017
Java Program To Find LCM Of Two Numbers Miscellaneous 29-08-2017
Java Program To Find GCD Of Two Numbers Miscellaneous 29-08-2017
Java Program To Check Whether An Alphabet Is Vowel Or Consonant Miscellaneous 25-08-2017
Java Program To Find ASCII Value Of Character Miscellaneous 25-08-2017
Java Object HashCode Method Miscellaneous 02-08-2017
Java Builder Design Pattern Example Miscellaneous 06-06-2017
Java Factory Design Pattern Example Miscellaneous 06-06-2017
Java Program To Print ASCII Values Miscellaneous 06-04-2017
Java Program To Find HCF LCM Of Two Numbers Miscellaneous 22-09-2018
Java String Array Iteration Miscellaneous 31-03-2017
Java Array Size Miscellaneous 30-03-2017
Java Integer toString Miscellaneous 30-03-2017
Java Sort Array Using Arrays.sort() Miscellaneous 28-03-2017
Java Print Array Using Arrays.toString Miscellaneous 28-03-2017
Java Nested Interface Miscellaneous 25-03-2017
Java Static Nested Class Miscellaneous 25-03-2017
Packages In Java Miscellaneous 24-03-2017
Java Strictfp Keyword Miscellaneous 24-03-2017
Java Call By Reference Miscellaneous 23-03-2017
Java Call By Value Miscellaneous 23-03-2017
Java Unboxing Example Miscellaneous 23-03-2017
Java Autoboxing Example Miscellaneous 23-03-2017
Java Format Currency Miscellaneous 15-02-2017
Java String To BigDecimal Conversion Miscellaneous 15-02-2017

1 2 3 4 5