Factory Method Design Pattern

Thilini Weerasinghe
4 min readMay 24, 2021
Source: https://refactoring.guru/

In this article it will discuss about the factory method design pattern. When it compare with previous discussed Singleton Pattern, it totally opposite for that. Factory method is suitable for the scenario that needs to create multiple instance from one class. This article consists with basic information of factory method, its application and how it is created with real world example.

🏭 What is factory method 🏭

It is a creational design pattern that enable multi object creation. It allows for creating objects from super class while allowing sub classes to alther the object type that wants to create. It is used for following scenarios.

  • When the class doesn’t know what sub classes should be created
  • When the parent class wants that its sub classes specify the objects should be created
  • When the parent classes choose the creation of objects to its sub-classes

Some design patterns like Abstract Factory, Prototype and Builder are starts from the Factory Method and eveolve towards. Developers can use factory method along with the Iterator to return various types of iterator collections. Though this pattern based on inheritence, it doesn’t require any initialization. It can be introduced as the specilization of Template method.

💡 Application of Factory Method

🌠 It can be used to provide capability for the users to extend its components by providing a framework or a library

🌠 It is suitble for when the developers don’t know the exact types and dependencies of the objects related to your scenario

🌠 If you want to save the system resources, factory method is the suitable design pattern. Because it allows for code reusability

Pros 😃

🌠 It provides loose coupling between the classes

🌠 It follows single responsibility principle. It means product creation rules can be isolated

🌠 It allows for open/closed principle. It means adding new products and removing existig products is very easy. It will not effects the exisiting program

Cons 😟

🌠 As you want to implement lots f sub classes, code become more complicated

Use Case of Factory Method

Use Case Scenario: Think there is a cake factory that has different branches like ABC bakers, PQR bakers and XYZ bakers. In the factory they make different types of cake like butter cake, chocolate cake, birthday cake, wedding cakes, and cup cakes. When you select each branch it sell few types of cakes and not all the products. So you have to develop a system to order the cake and choose the correct branch depend on your requirements.

when looking at the following diagram you can clearly understand the structure of the cake factory implementation.

class diagram for the cakefactory

Implementation 💻

To implement the application for above scenario we have to follow few steps.

  • Create an abstract class Cake.java to store the common variables and methods of cakes.
  • Create seperated classes for store the different types of cake implementation. ButterCake.java , ChocolateCake.java , BirthDayCake.java , WeddingCake.java and CupCake.java and they should extends the Cake.java class .
  • Now you need a class to store the branch information. So create another abstract class CakeStore.java to store the common branch information.
  • Then create seperate classes [ ABCBakery.java , PQRBakery.java , XYZBakery.java] to store the each branch information and it should extends the CakeStore.java class.
  • To simplify the implementation we create a enum to store the branch information.
  • Above steps are general implementation. Now we move to the Factory Method implementation.To manage the Cake Store information, you need a factory for that. So now we create the CakeStoreFactory.java class.
  • Finally we create the main application [ MainApp.java ] to get the branch details.
  • Here are the output values for above factory method implementation
Bakery has {cake=[ Birth Day Cake,  Wedding Cake,  Cup Cake]}Bakery has {cake=[ Butter Cake,  Chocolate Cake,  Cup Cake]}Bakery has {cake=[ Butter Cake,  Birth Day Cake,  Cup Cake]}

Hope you have clear understand about factory methods, its application and how it can be implemented in real world scenario. Complete code implementation for above scenario can get here.

Stay Safe & Learn New Things!!! 😃

--

--

Thilini Weerasinghe

Currently working as an Associate Software Engineer at Virtusa. Has completed degree in B.Sc (Hons) Computing & Information Systems. After all I am a Human...