DEV Community

Cover image for Understanding the Factory Design Pattern: A Beginner's Guide
Abhiram
Abhiram

Posted on

Understanding the Factory Design Pattern: A Beginner's Guide

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

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

Music Player class invoking Alexa device

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

Video Player class invoking Alexa device

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

Play method in Alexa

  • 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

Video Player Selection

Google Assistant device

*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

Just calling factory method by passing device 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.

Top comments (0)