Understanding the Factory Design Pattern: A Beginner's Guide
Published: 2023-04-01 06:06:00

## 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**


![Coffee served in restaurant] (https://dev-to-uploads.s3.amazonaws.com/uploads/articles/43rojwjadzu2qy1u5z3l.PNG)



- 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





<u>**Every design patten is meant to solve a problem, we will see which problem factory design patten solves**</u>



- 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.

![Music Player class invoking Alexa device](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ppqz8irlt483olu1xul.PNG)


- We have also another class Video Player -> Invokes play method in Alexa.


![Video Player class invoking Alexa device](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ks2pse5vn485h14y25w.PNG)


- Alexa objects are created, and we are invoking play method.

![Play method in Alexa](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q5w5v65fc7yirvdeiajp.PNG)


- 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.


![Selection bases on device](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pqhhnpq6qh1d6xsw9ceq.PNG)


![Video Player Selection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w490x8yxcxucwqbb8fql.PNG)


![Google Assistant device](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nmv60m38ko1ocbqpqs6b.PNG)




**Assume there are plenty of classes & we have to continuously modify the changes in All classes. **

## **Problems** 

- Consumer logic has to take care of creating objects on some condition.

- Logic to create object should be duplicated in every consumer code.

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.


![Factory Method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ciq3w7746ecrswwqb6jl.PNG)


![Just calling factory method by passing device choice](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhylmvhgcj0xhhisz40w.PNG)

**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.