FACTORY METHOD PATTERN
FACTORY METHOD PATTERN
=======================================
❉ Factory method pattern is a creational design pattern that aids in creating objects without exposing the instantiation logic to the client.
❉ In Factory method pattern, you always use parameters to determine which instance you need to get.
❉ The Factory Method Pattern defines a Virtual Constructor.
❉ Factory method pattern is usually used in higher levels of frameworks. Therefore, the end users, final developers in the lower level will be actually invoking it.
Difference Between Singleton & Factory Patterns
🟢 In Singleton, no parameters are passed.
🔵 In Factory method, parameters are passed.
🟢 When using Singleton, you exactly know the type of instance you are going to get.
🔵 When using Factory method, you probably have no idea what instance you will be getting. It depends on the input you enter. (It is possible to get sub instances rather than a parent instance)
🟢 You get to see instantiation logic when implementing using Singleton.
🔵 You do not see instantiation logic, you only see what you get when implementing using Factory method pattern. (You don’t know how instantiation happens)
🔵 You may have references to interfaces and other concrete classes etc. But as a user, you are not aware of, from what classes you have been inherited or implemented.
UML of Factory Method Pattern
How to Implement Factory Method Pattern?
✔︎ Make a common interface that declare methods which can be used in every product.
✔︎ Have a factory method that is empty inside the creator class where the return type is similar to the interface of the common product.
✔︎ Find all references to product constructors in the creator’s code.
✔︎ Create a set of subclasses for each product type mentioned in the factory method.
✔︎ Make the factory method abstract when it becomes empty.
Usage of Factory Method Pattern
❉ Factory method pattern can be used in following situations when;
- A class is not aware of what sub-classes to be instantiated.
- A class needs its sub classes to define what objects to be created.
- The parent classes select the creation of objects to its sub-classes.
Real World Example for Factory Method Pattern
📜 “Elegante” is an exclusive high end fashion boutique that sells women’s clothing items. They have introduced a seasonal offer recently that consists of 3 main types namely
- “Exclusive Deal”
- “Best Deal”
- “Doorbuster Deal”
Offer items include a Chiffon dress, a Comfy crop top, a wraparound skirt and a denim.
Following are the items that are included in each offer.
🎁 Exclusive Deal — A Chiffon dress & A Wraparound skirt
🎁 Best Deal — A Chiffon dress, A Comfy crop top & A Denim
🎁 Doorbuster Deal — A Chiffon dress & A Comfy crop top
When a customer chooses the offer type they need, the system will add the particular items according to the type of offer.
If we draw a sample UML diagram for the above scenario:
WomenClothing Class
📍 This is an abstract class.
Dress Class
📍Dress class is inherited from WomenClothing Class. Therefore, it is a IS-A Relationship.
Top Class
📍 Top class is inherited from WomenClothing Class. Therefore, it is a IS-A Relationship.
Denims Class
📍 Denims class is inherited from WomenClothing Class. Therefore, it is a IS-A Relationship.
Skirt Class
📍Skirt class is inherited from WomenClothing Class. Therefore, it is a IS-A Relationship.
✹ Now we have implemented all the sub classes that are extending from the WomenClothing parent class.
Offer Class
📍 Offer is a collection of womenclothing. Rather than selling items one by one, they have categorized into 3 different offers. In this Offer class, it has a protected List which is an ArrayList. We have invoked createOffer() method inside the constructor.
📍 createOffer() is an abstract method which is the key here. That is why we always give this to implement on implemented classes. Because, in a system, we just give the Offer. They can create any number of offers by putting different sort of womenclothing.
BestDeal Class
📍 BestDeal class is inherited from Offer Class. If a customer asks for a Dress, Top and a Denim, they get the BestDeal offer.
DoorbusterDeal Class
📍 DoorbusterDeal class is inherited from Offer Class. If a customer asks for a Dress and a Top, they get the DoorbusterDeal offer.
ExclusiveDeal Class
📍 ExclusiveDeal class is inherited from Offer Class. If a customer asks for a Dress and a Skirt, they get the ExclusiveDeal offer.
OfferCode Enum
SeasonalOffer Class
📍 This is where we are going to create real offers.
📍 Based on the offerCode, we direct which offer to return.
Application Class
Output:
✹ We get this because we have called only exclusive and doorbuster offers in the main class.
Advantages of Factory Method Pattern
♻️ Factory method pattern makes the code robust.
♻️ Permits you to hide implementation details of the core interfaces that are available in your application.
♻️ Loosely coupled, so that you do not need to bind application-specific classes with the code.
♻️ Factory method pattern is implemented in a way where sub classes are capable of choosing the types of objects to instantiate.
♻️ Classes used in the implementation are easy to extend.
♻️ Inheritance is used to support abstraction between implementation and client classes.
♻️ In Factory design pattern, it has provided approach to code for interface rather than implementation.
Disadvantages of Factory Method Pattern
♨️ Main disadvantage of using this Factory method pattern is that it is complicated when implementing because there are multiple sub classes and lots of coding involved compared to Singleton.
Important Facts to be remembered when creating a Factory class
🔔 Always pass arguments to constructor in order to determine your instance.
🔔 You have to create a factory and basic base elements when using this method pattern.
🔔 This Factory method pattern must switch the instances that return depending on the parameters that come in.
🔔 Unlike the Singleton design pattern, it does not return the same instance as it has multiple instances.