Factory Design Pattern falls under the category of Creational Design Pattern. Since the primary motive of the Creational Design Pattern is to create and initialize the objects. The same objective is passed onto the Factory Design Pattern. So, we can define the Factory Design Pattern as the creation of the objects without the exposition of the creational logic to its client.
Let’s suppose, I am a customer of the shop, where I arrive to buy soap. I ask the shopkeeper for either a Hamam soap, a Rexona soap or a Lux soap. The shopkeeper analyses my request and based on my request; he decides which soap needs to be returned. Here the shopkeeper is acting like a Factory.
Let’s suppose, I am a phone manufacturer, and I am looking for Operating Systems (OS) for various phones, but I don’t have any knowledge about a suitable OS for a particular phone. I will contact an OS factory for that. If I ask for an OS of an iPhone, OS factory would analyze the request and return the iOS, which is the recommended OS for iPhone. If the next time, a phone manufacturer approaches OS factory for an Operating System for a Samsung mobile, the OS factory will know that since the request is for Samsung, the suitable return will be an Android OS. Similarly, if the phone manufacturer asks for an OS for a Nokia phone, the OS factory would return the Windows OS based on the client’s recommendation. Here the phone manufacturer isn’t aware of the creational procedure of the OS because the object creation is hidden from him. It’s the responsibility of the OS factory to provide a suitable OS. Here the OS factory will act as a Factory where the object creation is done using the input like iPhone, Samsung, Nokia. Based on the input the best object will be returned.
If you are working in a software company, you’d have noticed this kind of a scenario. Let’s suppose; I have two clients, a Bank client and some owner of a small-scale shop. First, the bank client approaches the manager and asks for the best UI developer available for a contract purpose. The manager analyzes the resources that are the UI developers and picks the best option for the client’s objective. Now the small shop owner approaches and requests for a UI developer in a limited amount of money. Here the criterion is UI developer in less money. The manager will analyze the request based on this criterion and picks the suitable option. In this example, the clients don’t have any knowledge about the UI developers. They are requesting the manager for a UI developer suitable for their criteria. The manager here acts as a Factory.
The factory design pattern is used when we have a superclass with multiple sub-classes and based on the input; we need to return one of the sub-class.
Example: If a Restaurant serves Veg food, Non-Veg food, and Italian food.
Treat VegFood, NonVegFood, and ItalianFood as three classes whose superclass is Food.
If a customer asks “Veg” then Factory method would return the “VegFood” class.
public abstract class Food { protected double billPerPerson; protected Set items; public Food(double billPerPerson) { this.billPerPerson=billPerPerson; this.items=new HashSet<>(); } public double getBill() { return billPerPerson; } public Set getItems() { return items; } }
public class VegFood extends Food { public VegFood() { super(100); items.add("Idly"); items.add("Dosa"); items.add("Vada"); } }
public class NonVegFood extends Food { public NonVegFood() { super(120); items.add("Biriyani"); items.add("Chicken 65"); } }
public class FoodServer { public static Food getFood(String foodType) { if (foodType.equals("veg")) { return new VegFood(); } else if (foodType.equals("non veg")) { return new NonVegFood(); } else { System.out.println("We are not serving " + foodType); return null; } } }
public class Customer { public static void main(String[] args) { Food customer1Food = FoodServer.getFood("veg"); System.out.println("Customer1"); System.out.println("Items: "+customer1Food.getItems().toString()); System.out.println("Bill: "+customer1Food.getBill()); Food customer2Food = FoodServer.getFood("non veg"); System.out.println("Customer1"); System.out.println("Items: "+customer2Food.getItems().toString()); System.out.println("Bill: "+customer2Food.getBill()); } }