Factory Design Pattern
- Factory method is a creational pattern that uses factories to instantiate objects without having to specify the exact class of the objects that will be created
Creational Pattern
- Pattern that is related to creation of new objects
- Alternative way of using objects than just a regular constructor.
- Not going to use new keyword to create objects
Factories
- Within the context of object-oriented programming we mean methods or functions or other objects that create new objects.
- And the return types from factories are going to be new instances of objects in themselves.
-
Factories isolates or encapsulates the behaviour of creation of some new object.
-
In short, the separation between the code that creates products from the code that uses products are factories
-
we need not modify the client code for creating new types of products.
-
Follows Open close principle in solid which means *the code should be open for extension and close for modification which means we can add to our code without changing existing code when we want to implement new factories. *
-
Fundamental benefits of factory method in terms of designing software – Different levels of abstraction
Real world example
-
When you order a cup of coffee in a restaurant, you do not see how the coffee is served, whether in a glass or ceramic cup.
-
Once the coffee is served you accept it and drink it.
-
No implementation is shared just like how the coffee is served.
-
We can think of software design in similar fashion.
-
We want to separate our code that takes care of implementation details.
-
Without changing our core business logic, we want to implement things
Every design patten is meant to solve a problem, we will see which problem factory design patten solves
-
Suppose we are building custom operating system which plays audio to remotely connected device.
Example: Alexa, Google Assistant device. -
We are going to create Music Player class -> which invokes play method in Alexa.
- We have also another class Video Player -> Invokes play method in Alexa.
- Alexa objects are created, and we are invoking play method.
-
Consider a scenario, we have to connect to Google Assistant device to play Audio.
-
The selection between Amazon Alexa & Google Assistant may vary on set of conditions or configuration.
-
To support selection, we have to modify Music Player as well as Video player class.
*Assume there are plenty of classes & we have to continuously modify the changes in All classes. *
Problems
whenever new type is introduced, we have to modify the consumer logic.
**
How the factory design pattern solves the above problems
**
-
Consumer has to place their decision, factory pattern will take care of all the decisions to produce products.
-
Factory Method exposes one public class through which we can pass our condition.
-
Rule driven logic is now abstract to consumer, just call factory and we have to place our choice.
How to decide if one should use Factory design Pattern
- If we have multiple derived classes and consumer can request either of them based on certain condition.