Image Credit Pexels

Factory vs Factory Method vs Abstract Factory

Saurav Satpathy
bitMountn
Published in
5 min readJun 10, 2019

--

Introduction

What’s in the name? Answer is “Lots of confusion!” Name of these patterns are quite related to each other, they all work in similar way, like all are responsible for creating objects, yet there are some fine differences in how they are implemented.

Factory, Factory Method and Abstract Factory design pattern all does the same thing - takes care of object creation but differs in how they do it.

These are major differences between Factory, Factory Method and Abstract Factory:

Factory — Consists of Factory Class which can produce one or more types of objects.

Factory Method — Consists of a Factory Class with a Create Method which can produce only one kind of objects.

Abstract Factory — Creates abstraction of Factory Class using interface, which can produce different kind of objects.

You might be knowing about factory design pattern but don’t get the difference between Factory and Factory Method or Difference between Factory Method and Abstract Factory, then you have landed in the right article. Will see in a moment, what’s common and what’s different along with the use cases, it’s easy to follow.

Hope you have basic idea on these Design Patterns, in case you don’t, I encourage you to have a look on my articles Factory, Factory Method and Abstract Factory.

What’s Common?

— All these patterns serves the purpose of object creation, thus classified as creational design pattern.

— All these hides the instantiation logic. In other words, encapsulates object creation.

— All these have a factory class responsible for creating new objects.

What’s different?

Difference between Factory and Factory Method and Abstract Factory design pattern
Image Credit: bitMountn (We make mobile apps)

That’s all the major differences, you may like to see Design Pattern to have broader understanding.

Or you can continue reading, as the theoretical difference is done, and real programmer likes code 🤓 So, let’s see each design patterns with code snippets:

Factory Design Pattern:

I will demonstrate factory design pattern by creating two different types of label, Header and Footer label. If you don’t know what UILabel is, it shows text in mobile app. HeaderLabel will be slightly bold and blue while FooterLabel will be thin and grey. We will just give the input as “header” or “footer” to our LabelFactory and nothing more. Factory will create the suitable label object for us.

So, we declared a protocol which will be followed by the concrete label class.

Image Credit: bitMountn (We make mobile apps)

We are going to create header and footer labels. Each follows LabelDelegate These are pretty bare bone classes written in Swift 5.0

Image Credit: bitMountn (We make mobile apps)

Now we will create a factory class which generates HeaderLabel and FooterLabel based on the input.

Image Credit: bitMountn (We make mobile apps)

LabelFactory ultimately creates instance of concrete label class, but it doesn’t reveal the exact the object of the concrete class. That’s the speciality of Factory design pattern, it encapsulates object creation code and produces the suitable object based on given input.

Factory Method Design Pattern

We will demonstrate this by creating Tinder left and right swipe buttons. Swipe direction of SwipeLeftButton will be towards left and swipe direction of SwipeRightButton will be towards right.

First thing first we will declare a protocol for these buttons like an ideal programmer 😊

Image Credit: bitMountn (We make mobile apps)

Now we will implement the SwipeLeftButton by following ButtonDelegate protocol.

Image Credit: bitMountn (We make mobile apps)

Next we will implement the SwipeRightButton by following ButtonDelegate protocol.

Image Credit: bitMountn (We make mobile apps)

We will need a protocol for factory too, because we will have specialised factory classes.

Image Credit: bitMountn (We make mobile apps)

All the setup is done, now we will create the SwipeLeftButtonFactory. This will demonstrate factory method design pattern.

Image Credit: bitMountn (We make mobile apps)

Similarly we will create SwipeRightButtonFactory.

Image Credit: bitMountn (We make mobile apps)

Did you notice the difference? unlike factory pattern, factory method had two dedicated factories each specialised in doing just one thing. The actual instance is decided within the subclass. Concrete creators override the factory method to change the resulting product’s type.

One last thing is use of these factories in a ViewController.

Image Credit: bitMountn (We make mobile apps)

Abstract Factory Design Pattern

Think of an e-commerce app like Amazon, one noticeable ui element is “Add To Cart” Button. We will produce AddToCart button through the demonstration of Abstract factory. Also we will produce another ui element which is Title Label.

Image Credit: bitMountn (We make mobile apps)

Now, these protocols will be followed by concrete classes.

Image Credit: bitMountn (We make mobile apps)
Image Credit: bitMountn (We make mobile apps)

Now let’s setup the factory class but before that we need to define protocols for factories too.

Image Credit: bitMountn (We make mobile apps)

Factory class will implement this UIFactoryDelegate protocol.

Image Credit: bitMountn (We make mobile apps)

All done! This factory can produce different kind of objects. Thus abstract factory is also called as factory of factories, because it is capable of producing different kinds of instances based on the need. Also there exists an abstraction for factory.

Image Credit: bitMountn (We make mobile apps)

Please feel free to ask question in the comment box! I will answer to my best.

You liked this article? Please clap and share it with your fellow devs :-)

--

--