So, far we have looked upon the design pattern and its advantage. and also discussed the Singleton Pattern.
Today in this blog we will learn the Factory Design pattern with the example.
What is the Factory Design pattern?
- This pattern defines an interface for creating an object but lets subclass decide which class to instantiate.
- It is one of the creational design patterns which is used by all the wrapper classes like Integer, Boolean in the valueOf() method.
- It simply follows the Dependency Inversion Principle which states that
Depend upon abstraction, don't depend upon concrete classes
Why the Factory design pattern?
Consider having a pizza store and you end up in writing the following code.
We have one Pizza class which prepares Pizza for us and one client class PizzaStore which orders.
public class Pizza { public void prepare() { //TODO System.out.println("Prepare method"); } public void bake() { //TODO System.out.println("Bake method"); } public void cut() { //TODO System.out.println("Cut method"); } public void box() { //TODO System.out.println("Box method"); } }
public class PizzaStore { public Pizza orderPizza() { Pizza pizza = new Pizza(); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } }
Here, PizzaStore is our client class whose sole purpose is to order the pizza.
But now you need more than one type of pizza. And you end up updating the above client class PizzaStore in the following manner.
public class PizzaStore { public Pizza orderPizza(String type) { Pizza pizza = null; if (type.equals("cheese")) { pizza = new CheesePizza(); } else if (type.equals("pepperoni")) { pizza = new PepperoniPizza(); } else if (type.equals("clam")) { pizza = new ClamPizza(); } else if (type.equals("veggie")) { pizza = new VeggiePizza(); } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } }
What’s the problem with the above design?
1) What if all your rivals have added a couple of more trendy pizza. Obviously, you need to pace up with the competition and Then you will modify your client class. You just end up chaining a new else if to create object depending upon the new type. So each time a new change is made at the library side, the Client needs to update the code.
2) It would be more problematic when class Pizza has some more clients like Pizza Store, then we have to write the same if-else ladder code for creating an object () in other Clients also. And when the implementation(adding or removing pizza type) changes at the pizza side then we have to update all our Client classes too.
Whats the solution?
Let’s keep this object creation code in some separate class known as factory(refer SimplePizzaFactory in our scenario) and abstract it from the client. So, the factory only handles object creation and supplies to the client whenever they need it.
So, we have added one more class SimplePizzaFactory, and updated the PizzaStore class in the following manner.
public class PizzaStore { SimplePizzaFactory factory; public PizzaStore(SimplePizzaFactory factory) { this.factory = factory; } public Pizza orderPizza(String type) { Pizza pizza; pizza = factory.createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } }
public class SimplePizzaFactory { public Pizza createPizza(String type) { Pizza pizza = null; if (type.equals("cheese")) { pizza = new CheesePizza(); } else if (type.equals("pepperoni")) { pizza = new PepperoniPizza(); } else if (type.equals("clam")) { pizza = new ClamPizza(); } else if (type.equals("veggie")) { pizza = new VeggiePizza(); } return pizza; } }
So, long story short, Factory handles the object creation. So, any time orderpizza() needs pizza, it requests the SimplePizzaFactory to make one.
Conclusion
So in this blog, we have understood the factory design pattern and how this design pattern makes our code robust and less coupled by actually removing instantiation of actual implementation from client code. And simply abstracts the object creation logic from the client.
Please feel free to provide your suggestions. 🙂 Please comment if you have some queries and want to add some points.
References:
A book: Head First Design Patterns