top of page
Writer's pictureSunil Kumar Yadav

Factory Method: Must Know Design Pattern (1/7)

Updated: Oct 2, 2023


Image: Factory

Factory pattern is a type of Creational Design Pattern. Factory patterns define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method allows a class to defer instantiation to subclasses.


The design patterns that deal with the creation of an object. The first design pattern is the creational pattern. It provides ways to instantiate single objects or groups of related objects.

The factory pattern takes the responsibility of instantiating an object from the class to a Factory class.


Where is the factory method pattern used?

The factory pattern can prove valuable in various application scenarios. Software where the concrete products to be created are unknown or are not defined in advance benefits from the alternative approach for subclass management.


Typical use cases include frameworks or class libraries, which have become virtually indispensable as a basic framework for the development of modern applications.

Authentication systems also benefit from the advantages of the factory design pattern: Instead of a central class with various parameters that vary according to user authorization, the authentication process can be delegated to factory classes that make independent decisions about the handling of the respective user.



Understanding factory method pattern

Let's try to understand this with the help of a simple example. Consider we are working for a burger chain and it has few types of burgers. Now instead of creating a separate class for each type of burger, we can create a burger factory class which based on the user's selection will create the burger.


Below is a simple example where the Python class Burger is instantiated based on the class BurgerFactory. Based on the user's requirement BurgerFactory returns an instantiated object of class Burger.


class Burger:
    def __init__(self, ingredients):
        self.ingredients = ingredients

    def print(self):
        print(self.ingredients)

class BurgerFactory:
    def createCheeseBurger(self):
       ingredients = ["bun", "cheese", "chicken-patty"]
       return Burger(ingredients)

    def createDeluxCheeseBurger(self):
       ingredients = ["bun", "tomato", "lettuce", "cheese", "chicken-patty"]
       return Burger(ingredients)

    def createVegDeluxBurger(self):
       ingredients = ["bun", "tomato", "lettuce", "special-sauce", "veg-patty"]
       return Burger(ingredients)

burgerFactory = BurgerFactory()
burgerFactory.createCheeseBurger().print()
burgerFactory.createDeluxCheeseBurger().print()
burgerFactory.createVegDeluxBurger().print()

In the factory design pattern, the user/client does not have full details of what all going on inside and has less control over the created object.


Pros and Cons

The key advantages of factory method patterns are

  • Modular expandability of the application

  • Good testability

  • Significant method names

The key disadvantages of factory method patterns are

  • High number of required classes

  • The extension of the application is very elaborate


Recent Posts

See All

Comments


bottom of page