A Complete Guide to Creational Design Patterns in Java: The Factory Pattern

Varsha Das
3 min readSep 27, 2022

--

What is a design pattern?

A software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design.

Why should I learn patterns?

Makes life simpler by not reinventing the wheel

  1. Toolbox of time-tested solutions to common problems in software design.
  2. Defines a common language between the team and organization to solve emerging issues related to technological advancements.
  3. Start understanding patterns in libraries and languages.

Classification of patterns

Creational

Structural

Behavioral

There are 5 main types of Creational Design patterns:

  1. Builder
  2. Prototype
  3. Singleton
  4. Factory
  5. Abstract Factory

In this article, we will focus in detail on one of the Creational patterns — The Factory Design pattern.

Usage:

To not let the client code know how to create various types of objects

The goal is to take out the responsibility for the creation of objects from the client program to the factory class.

Existing problem:

Let’s understand with a simple example.

There is an Icecream shop newly opened. The owner of the shop sells 3 types of ice cream — chocolate, vanilla & strawberry.

Photo by JÉSHOOTS: https://www.pexels.com/photo/dessert-ice-cream-summer-sweet-3631/

We have 2 classes now — Icecream & IcecreamStore.

Right now, the IcecreamStore class has all the logic to create ‘icecreams’ but has no idea in the future what other types of ‘icecreams’ would it want to create.

A few questions arise

1. Should the IcecreamStore be concerned about how to create other types of ice cream or should it be concerned only with selling the icecreams?

2. When ice cream flavors are added or removed, will the class be robust enough to incorporate the creation of other types of icecreams?

3. Will the existing logic be changed for the above reasons?

How can the Factory pattern help?

Considering the problems that came up above, we introduce another factory class in between the client class & the abstract class.

Going by the literal meaning of factory — a place where products get ‘manufactured’. We also want the factory class to be concerned only with the manufacturing of objects.

So, going by the above example, we can have another IcecreamFactory class that is going to be concerned with the creation of icecreams.

We can extract the object creation logic outside of the client class.

Later on, when new flavors of icecreams get added or removed, the factory class is going to take care of that without affecting anything in the client code.

Code with an example:

We have an abstract class — Icecream.

And 3 concrete icecream classes — ChocolateIcecream, StrawberryIcecream and VanillaIcecream.

Below is the code for Icecream class:

Below is the code for the concrete subclasses:

Next, we introduced the IcecreamFactory class, the one that will be concerned with the creation of icecreams.

Finally, the client class or the IcecreamStore class.

Output:

As we can see from above, now it is quite simpler to add or remove icecreams without affecting anything else outside this Factory class.

If you liked this article, please click the “clap” button 👏 a few times.

Gives me enough motivation to put out more content like this. Please share it with a friend who you think this article might help.

Follow me Varsha Das to receive my weekly articles.

Connect with me — Varsha Das | LinkedIn

Follow my Youtube channel where we discuss Data Structures & Algorithms.

Thanks for reading.

Happy learning! 😁

--

--

Varsha Das
Varsha Das

Written by Varsha Das

"Senior Software Engineer @Fintech | Digital Creator @Youtube | Thrive on daily excellence | ❤️ complexity -> clarity | Devoted to health and continuous growth

No responses yet