Factory Pattern (Part 1) — What It Is And How To Use It

Bhuvnesh Maheshwari
3 min readJan 1, 2021

--

It’s the 1st of January 2021, but most of us are still in party mode! I am sure you must have thought about the new year resolution to accomplish new goals and make this year be the best of your life. I wish you a very Happy New Year 😃. Since our party mode is still on, let’s learn the factory pattern by implementing a use case of an alcohol shop! There are three variants of the factory pattern.

  1. Simple Factory. (Not a pattern, it’s just a programming paradigm.)
  2. Factory Method.
  3. Abstract Factory.

We’ll learn simple factory and factory method in this blog post and abstract factory in the next one.

Question — What is a factory?

Answer — Well, it produces products in mass that are sold to the customers later on in a retail store.

Question — Why can’t we produce in the retail shop?

Answer — Are you dump! Producing products in mass is not that easy. It requires raw material, gigantic machinery, large space, many workers. How can you put all of them in your retail store!.

Let’s come back to the factory design pattern. From the above dialogue, one might get the context and usage of the factory pattern. Factory Method is used to instantiate the object of a class. Wait, we can do it by using a new keyword right? What’s the problem with that?

There are a couple of problems using that approach. Here are they,

  1. In the real-world, object creation often is not that simple. We may need to execute business logic or do some computation before creating a new object.
  2. There might be many places where we need to implement the same logic to instantiate a particular object. Do you want to repeat the same code multiple times? Of course not!

So, what’ the solution? Obviously, we can separate the logic of instantiation and encapsulate it in another class. That class will be the factory that will supply us with instances of an object. This idea is nothing but a Simple Factory.

Benefits

  1. We’ve put the logic of creation in a separate method. So now we have one source for the change. Whenever we need to change the logic, we can do it in a single place. We’ve removed code duplication.
  2. Our code looks a lot cleaner now. We’ve separated layers of concerns. The factory is responsible for the creation and the client is responsible for using it! The client does not need to know the creation process.

Factory Method

Let’s understand the Factory Method using an example. We are building a website for an alcohol brand. Of course, alcohol is restricted in certain geographic regions, so we can’t sell it in those regions. Additionally, there are also age restrictions for buying alcohol. Let’s see how we can implement it. First, let’s create an alcohol class and few concrete subclasses of alcohol.

Now let’s define an interface for the factory. This interface will be implemented by concrete factory classes.

Let’s use our factory to get our favorite drink.

This is how we can extract the implementation details from the client code and encapsulate it. We are deferring the instantiation of an object and letting the subclasses decide based on our custom logic which objects to instantiate.

One more powerful benefit of having a common interface — AlcoholFactory is that we can use polymorphism to swap the instances of one class with another class based on our logic. Assume that, a customer orders a beer but we do not have a beer, at that time we can put our logic such that since Vodka is also an alcoholic beer, we can recommend Vodka to our customers. Because VodkaFactory and BeerFactory implement a common interface — AlcoholFactory, we can swap the instance of a beer with vodka at runtime. Maybe I gave a silly example, but you get the idea. Okay, that was it, we’ve learned factory pattern. Let’s see the definition of the factory pattern according to the Gang of Four book.

Define an interface for creating an object, but let subclasses decide which class to instantiate. The Factory method lets a class defer instantiation it uses to subclasses.

See you in the next post about the Abstract Factory method.

Thanks for reading!

Happy Coding 😄

--

--

Bhuvnesh Maheshwari
Bhuvnesh Maheshwari

Written by Bhuvnesh Maheshwari

Let’s be an awesome developer together! I will be learning the tech and sharing it with the community.

No responses yet