Python – The Factory Design Pattern


Understanding factory design pattern

In OOP the term factory means a class that is responsible for creating objects of other types. Clients can call this factory class, with client parameters and object of desired types are created and returned to the client through the factory.

Understanding the factory pattern with a simple example

Let us suppose a factory is responsible to manifacture pens. Their could be many number of types of pens, the factory manifactures. The factory will consist of some departments that handle the manifactures of types of pens. Let us assume Department A produces ball pens & gel pens, and Department B produces ink pens. A client in order to purchase pens first visits the factory and asks for his requirements. The client will be unknown about the departments and the client would not be able to see the manifacture process. The factory provides the client with anytype of pens the client requires. Either the pen could be from department A or deprtment B. But the process of purchase from the clients perspective will be the same for any reqirement they ask for. Only the factory will be respponsible to assign the manifacture either to department A or B.

Benifits Obtained
  • Logic of the object creation will be hidden

  • Object creation can be independent of the class implementation

  • Adding support of new type of object can be easily done.

There are three variants of the Factory Pattern:

1. Simple Factory Pattern

2. Factory Method Pattern

3. Abstract Factory Pattern

Simple Factory Pattern

Simple Factory Pattern can be easily understood with the help of the following diagram. Here the client uses the Factory class which has the create_type() method. When the client calls the create_type() method with the type parameters. Based on the parameters obtained, The factory class returns ProductA and ProductB. Let us dig deeper into the simple factory pattern with the help of the following code:
Code Description : In the above code we create a Abstract class called Employee. It is also refereed to as abstract base class. ABCMeta is python’s special metaclass to make class Abstract and has a create() method.We create two products manager and accountant from the Employee interface and implement create() method. PersonFactory is a factory that has createPerson()  method. First the inputs is passed on to the factory , the createPerson method consist of the code eval(designation)().create(name). The eval is resposible to convert string to object name and if object is available in the module then it creates name with the create(name). Based on the type of argument passed by the client, an appropriate Person instance is created.

The factory method pattern

In the factory method pattern we define and interface to create objects, but instead of the factory being responsible for the object creation, a subclass decides the class to be instantiated.Let us understand factory method with a UML diagram. Here we have an abstract class Creator, which consists of factorymethod() . It is the responsibility of factorymethod() to create objects of certain type. The ConcreteCreator() has factoryMethod() that implements the Creator abstract class, and this method cna change the created object at runtime. ConcreteCreator creates ConcreteProduct and makes sure that the object it creates implements the Product class and provides implementation for all the methods in the Product interface. Let us take a real world example to show the working of the factory method pattern. Consider we would like to create social media accounts like linkedIn and facebook. Each of them will have different features or sections. Linkedin has a feature of storing Experience. On facebook you will see sections in an album of your recent pictures. They both will have a common section i.e personal information. let’s take a look at the implementation with a python code. We start by defining the Product interface. A section abstract class is created and a describe() abstract method is defined. Multiple concreteProduct classes, PersonalInfoSection, AlbumSection, ExperienceSection. These classes implement the describe() abstract method and print their respective section names:
We create a Creator abstract class “Profile”. The profile Creator abstract class provides a factory method, createProfile(). The createProfile() method should be implemented by ConcreteClass to actually create the profiles with appropriate sections. The Profile abstract class is not aware of the sections that each profile should have. We create two concreteCreator classes, Facebook and Linkedin. Each classes implements the createProfile() abstract method that actually creates(instantiates) multiple sections (ConcreteProducts) at runtime. Finally we write the client code that determines which creator class to instantiate to create a profile of the clients choice. After running the code. it will first ask you to enter the name of the profile that you want to create. Let us enter facebook. It then instantiates facebook ConcreteCreator class. This internally creates ConcreteProducts(s), i.e it instantiates PersonalInfoSection and AlbumSection. If linkedin is chosen then it instantiates PersonalSectio and ExperienceSection.

Abstract Factory Pattern

Abstract Factory patterns make sure that the client is isolated from the creation of the objects but allowed to use the objects created. Client will have the ability to access the object only through an interface. If products of one family are to be used, abstract factory pattern helps the client use the object from one/family at a time.

Let us implement the abstract factory pattern with an example.

View the code as well as output in the github source:

We will go through facade design pattern pattern in the next topic of design patterns in python. 

You may also like...

1 Response

  1. July 29, 2020

    […] will go through factory design pattern in the next topic of design patterns in […]

Leave a Reply

Your email address will not be published. Required fields are marked *