Factory Design Pattern In Java

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

Program:

            /* ............... 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
// information such as type(DOMESTICPLAN/COMMERCIALPLAN/INSTITUTIONALPLAN).

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: ");
		p.getRate();
		p.calculateBill(units);

	}
}// end of GenerateBill class.

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

Output

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

Notes:

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

Tags

Factory Design Pattern, Factory Method in java

Similar Programs Chapter Last Updated
Find Unique Elements In List Java Miscellaneous 07-10-2023
Java Program To Implement A Custom equals() and hashcode() In Java Miscellaneous 07-10-2023
Java Program To Find The Intersection Of Two HashSets Miscellaneous 07-10-2023
Java Program To Remove Duplicate Elements From List Miscellaneous 07-10-2023
Java program to parse a date and time string from a log file and store it in a database Miscellaneous 19-09-2023
Java Program To Print All The Dates In A Month That Fall On A Weekend Miscellaneous 19-09-2023
Java Program To Find Number Of Working Days In A Month Miscellaneous 19-09-2023
Java Program To Calculate Age From Year Of Birth Miscellaneous 16-09-2023
How To Check If Two Strings Are Anagrams In Java Miscellaneous 22-08-2023
Java Program To Make A Snake Game Miscellaneous 15-08-2023
Java Program To Find Repeated Characters Of String Miscellaneous 15-08-2023
String To Array In Java Miscellaneous 11-08-2023
Java Program To Convert Date To String Miscellaneous 11-08-2023
Java Program To Convert String To Date Object Miscellaneous 11-08-2023
Java Program To Find Number Of Days In A Month Miscellaneous 11-08-2023
Java Program To Print First And Last Day Of Month Miscellaneous 11-08-2023
Java Program To Find Leap Year Between Two Dates Miscellaneous 11-08-2023
Java Code To Find Difference Between Two Dates In Years Months And Days Miscellaneous 11-08-2023
Java program to calculate age from year of birth Miscellaneous 29-06-2023
Swap Two Numbers Without Using Third Variable In Java Miscellaneous 02-06-2023
Java Program To Find The Average Of An Array Of Numbers Miscellaneous 02-06-2023
How Do You Find The Factorial Of A Number In Java Miscellaneous 02-06-2023
Java Program That Takes Two Numbers As Input And Prints Their Sum Miscellaneous 27-05-2023
How To Get The Length Of An Array In Java Miscellaneous 27-05-2023
Java Add Element To List Example Miscellaneous 19-05-2023
Java Program To Square All Items In List Miscellaneous 17-05-2023
Java Program To Merge Two Lists Miscellaneous 17-05-2023
How To Reverse A List In Java Miscellaneous 17-05-2023
Java Program To Find Unique Elements In An Array Miscellaneous 14-05-2023
Java Program To List All Elements In List Miscellaneous 30-04-2023

1 2