Factory Pattern: Design Patterns

Factory Pattern: Design Patterns

Workers working in factory
Factory

One of the most trusted and widely used Creational Design Pattern is the Factory pattern. It helps to solve the problem of having tight coupling with the client code by encapsulating the object creation logic in itself.

This might sound confusing so let’s break this to simpler words.

We usually have some architecture where we have 3 or more classes that needs to be used at different scenarios based on demand. Sometimes these scenarios are not known until runtime. If we design the program in the traditional way then we would have a lot of new keyword in the client code. This creates a tight link between our code and the client code. If we tend to change our classes sometime later, then all the client code which calls our class, needs to be updated.

We don’t want that! That will be too much of a hassle and we will be dead stuck fixing client codes rather than spending time to refactor and improve our code.

Coming to the rescue, the Factory pattern.

I can simply create my code and let the factory handle the other relevant stuff to help the clients.

Whenever a client wants to call our code, it requests the factory for it which in turn creates and returns the required object back to the client. The client does not have to worry about the implementation and the object creation.

Let us now answer some basic questions which everyone has in mind when it coms to this concept.

What is a factory in programming ?

In object-oriented programming (OOP), a factory is an object for creating other objects. A factory is a function or method that returns objects of a varying prototype or class from some method call, which is assumed to be “new”.

When should I use Factory pattern ?

  • In frameworks where library code needs to create objects of types which may be sub classed by applications using the framework.
  • Some or all concrete products can be created in multiple ways
  • We want to have an option where in the future there may be new ways to create the concrete product.
  • When client code does not need to know how they are created.
  • Where we have to create an object of any one of sub-classes depending on the data provided at runtime.

What are the basic concepts of implementing it ?

  • A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. Basically, the subclasses are responsible to create the instance of the class.
  • We can keep the Factory class Singleton or we can keep the method that returns the subclass as static.

 

So, in short speaking technically:

The factory pattern uses polymorphism, dependency injection and Inversion of control. The stated purpose of the Factory Patterns is to define an interface for creating an object, but let subclasses decide which class to instantiate.

 

Implementation:

Considering a simple problem statement.

We have a computer program that teaches kids the animal noises. The kid can enter any animal name and the program replies with the noise that particular animal has.

 

Let us see the technical aspects of the code:

  • We can have many animals and each will have its own noise.
  • The kid can enter any random animal name and he/she can access all the features of the animal like noise it makes.
  • Each animal can have a base representation of Animal interface or abstract class
  • The animal object that needs to be created is not known until runtime.

 

Lets see how we can code this in Java (All files available in Github project here):

We can first define an Animal interface that will be implemented by every class of Animal. This interface has just one function that can be overridden by its child classes.

 

Next we have the Animals like Dog, Cat, Duck that implement our Animal interface and override the methods as shown below:



 

Now we need to create the Factory. We have a simple class that has a static member which is responsible to create and return the Animal required to the client code. It checks the passed parameter to a list of if-else conditions or switch case. If an animal of the required name is found, it creates the instance of that class and returns it typecast to Animal interface. That’s the beauty!

 

Lets see how our Client code (main method in our case) would look like. The client creates an instance of the Factory class. Now it demands the type of object it needs and it can be referenced to the base class Animal type.

 

Generalising this further, we can get objects at runtime from the factory as follows:

 

Advantage of Factory Design Pattern

  • Factory Method Pattern allows the sub-classes to choose the type of objects to create.
  • It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code. Thus, the code interacts solely with the resultant interface or abstract class, so that it will work with any classes that implement that interface or that extends that abstract class.
  • It enables the subclasses to provide extended version of an object, because creating an object inside factory is more flexible than creating an object directly in the client. Since client is working on interface level any time you can enhance the implementation and return from Factory.
  • Code written using Factory design pattern in Java is also easy to debug and troubleshoot because you have a centralised method for object creation and every client is getting object from same place
Please follow and like:

Leave a Reply

Your email address will not be published.