A guide to software design patterns

Creational Design Pattern: Factory Method

Peterson C
The Startup
4 min readDec 23, 2019

--

Photo by Ale Alvarez on Unsplash

The factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without specifying the exact class of the object to be created. This is accomplished by creating objects by calling a factory method — either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes — rather than by calling a constructor.

In plain English, that simply means you call a factory and pass one parameter and a class is created for you without having to instantiate the class yourself. The factory takes care of all that internally for you. You just need to know which value to pass for each distinct class you want to create. In our case, we’ll create a factory and show how to use it to create objects.

This pattern is also one of the “Gang of Four” design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, maintain, test, reuse, and scale. It also adheres to the S.O.L.I.D principles of programming by decoupling the construction of objects from the objects themselves.

--

--

Peterson C
The Startup

Husband, father, engineer, musician, and writer by luck. I write mostly about whatever comes to mind. Follow me on this crazy writing journey if you dare.