Skip to content

Creational Design Pattern: Factory Pattern

Next up our sleeves is the factory pattern which is yet another and very popular object creation pattern. Factory Method Pattern just define an interface or abstract class for creating an object and a factory class or method will be responsible in creating object that inherited that abstract class or interface. No other class would ever be able to create an object of the class. The class will be self-responsible for the creation of the object. This will decouple the initiation logic.

Motivation

There can be multiple objects of same base class and every class that needs to create an object need to create a new object. For example, we are designing an Application that represent the Animal Kingdom that means there can be many animals. Each Animal class “Cat”, “Dog” etc. will extend the Animal Class. Now whenever we need to create a new Animal we need to call the constructor with new operator like “new Cat()”. This result in coupling of creation logic with the implemented class.

Intent

  1. Create a way to let sub-classes to choose the type of objects to create.
  2. Promote the de-coupling by making sure that any App logic will only work with abstract class thus eliminating the need to know the specific class and its behavior.

Example

Let’s checkout below example to understand the concept in a more practical way.

Factory Pattern UML diagram
Factory Pattern UML diagram

First we need to implement an abstract class that will define the behavior of our child classes.

Now we need few child classes that will implement the behavior defined by the “AbstractBase”. Let’s have three child classes that will implement the behavior.

Now we need to implement a good way to create new objects of these child class without exposing their type or exact behavior to other classes. We will create a Factory class that will define the types of objects available and will return objects of AbstractBase of the specific type mentioned while creating the object.

This summarizes how to implement a factory pattern. The only thing left now is to check how this call comes into play when we want to create and objects of above type. This happens in the main logic of the Application or client code. The other classes don’t know anything about what and how these child classes behave and work they just know about the Abstract class and the behavior defined with-in it and will only relay on those methods. This provides a layer of abstraction too so we can switch any child class without breaking the functionality of the actual logic of the application or other classes. Which in result give a lot more flexibility while implementing the logic and later while making modifications. Modifying any of the child class will not require any modification in the classes that are using it.

Now let’s see how the main application logic will use the above built Factory Pattern objects.

Practical Example

Let’s see one more example which is defining our Animal Kingdom.

Factory Pattern UML diagram
Factory Pattern UML diagram

Now let’s define the actual creation logic via Factory pattern. The Client logic will depend on the Factory class to provide it with the concrete implementation of the Animal class based on the types provided by the client logic. Client actually doesn’t know anything about the concrete class and will treat it as the behavior defined by the Abstract Animal class.

Advantages of Factory Pattern

  1. Reuse: When we want to instantiate a new object we don’t need to repeat the if-else conditions. We just call the Factory method.
  2. Unit-testability: When we are writing unit test for the application we just write few unit tests to test the factory method and once its pass then we never test the object creation in the test suits. We just test caller and its return values.
  3. Extensibility – Whenever in the future we want to add a new class to the factory we don’t need to change the client and unit test already written for the pre-existing code. We just go ahead and add the new class to the source and update the factory method and its unit test.
  4. Adds an Abstraction layer to keep client and Object creation separate making loosely coupled class structure.

Disadvantages of Factory Pattern

  1. This is one of the common disadvantage in almost all of the design pattern. You need to write quite a lot of boiler plate code.
  2. If there are too many child classes, if-else ladder or switch statements quite large to keep them in check.

 

This will be all about Factory Pattern.
Thanks for reading.
Cheers!!

Published inDesign Patterns

Be First to Comment

    Leave a Reply

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