Home » Factory Design Pattern in Python

Factory Design Pattern in Python

by Online Tutorials Library

Factory Design Pattern

A factory design pattern is one of the types of Creational Design Pattern that allows us to use an interface or a class to create an object to instantiate. The factory method offers us the best way to create an object. In this method, objects are created without revealing the logic to the client. To create a new type of object, the client uses the same common interface.

Problem

Suppose we have planned to create a website which provides the books in the different part of the country. The website’s initial version only takes the order of the books, but as time passes, our website gains popularity now, we won’t add more items to sales such as clothes and footwear. It is a very good idea, but what about the software developers. Now they have to change the whole code because most parts of the code engaged with the book’s class, and developers have to change the entire codebase. It may lead to a messy code.

Let’s understand this concept with another example –

Example –

Let’s understand how we can solve such types of problems.

Solution

In the solution, we use the special factory method to call the construction object instead of using straight forward object construction. Both method of creating objects are quite similar but they are called within the factory method.

For Example – Our selling items such as Books, Mobile, Cloths, and Accessories classes should include the purchase interface which will declare a method buy. Each class will implement these methods uniquely.

Let’s see the following diagram to understand it more specifically.

Example –

Output –

voiture  book  libro  biclothtte  phone  teléfono  clothtte  cloth  paño  

Merits of using Factory Method

Below are the advantages of the factory method.

  • Factory methods are very useful in adding new types of product without distributing the existing client code.
  • It avoids the tight coupling between the products and the creator classes and objects.

Demerits of using Factory Method

The disadvantages of using factory methods are given below.

  • It will create the large number of small files or cluttering of the files.
  • The client might have the sub class to create a particular actual product object.

Applicability

Its concept is same as the polymorphism, where we don’t need to make changes in the client code. For example – Suppose we want to draw different shapes such as Rectangle, Square, Circle, etc. We can use the factory method to create the instance depending upon the user’s input.

In a cab application, we can book a 1- wheeler, 2- wheeler, 3-wheeler, and 4-wheeler. Here the user can book any of the rides which he wants to book. With the help of factory method, we can create a class named Booking which will help us to create the instance that takes the user’s input. So here the developer doesn’t need to change the entire code to add the new facility.

Factory method removes the complex logical code that is hard to preserve. It also prevents us to change in codebase because modifying existing code can introduce the subtle bugs and change in the behavior.

Where to use factory Methods

First we need to identify the situations where the factory method can be implemented. It can be used where an application depends upon the interface (product) to accomplish the task and there are multiple concrete implementations of that defined interface.

There are many problems that can be resolved using the factory method. We define few example which fits in this description.

Replacing complex logical code

Generally, the code consists of logic like if/else/elif that is hard to maintain due to adding new paths with some requirements change.

Using the factory method, we can put each logical path’s body into the various defined functions or classes with a common interface. The developer can provide the concrete implantation for the modification.

Combining similar features under a common interface

Suppose we need to apply the specific filter to an image. The factory method will find the specific filter according to the user input. The factory method can apply the actual implementation.

Supporting multiple implementations of the same features

A group of scientists need transformations of the satellite images one coordinate system to another. But a system has multiple algorithms to perform the different level of transformation. The application can allow the user to select an ideal algorithm. Factory method can implement firmly algorithm based on this option.

Integrating related external series

A video streaming application wants to integrate the multiple external services. The application allows to the users to know where their video comes from. The Factory method creates the right integration based on a user preference.

Conclusion

Factory method is a popular and widely used creational design pattern. It can be used in the various concrete implementation of an interface exist. Factory method removes the complex logic code that is hard to maintain. It also prevents us to modifying the existing code to support the new requirement.


Next Topic#

You may also like