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 FactoryFactory 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 articles

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:

We will demonstrate factory design pattern by creating two types of label, header and footer label. If you don’t know what label 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.

source : medium.com

Leave a Reply