Factory design pattern in Python

Design patterns: Factory pattern

Jordan P. Raychev
Geek Culture

--

Hey folks and welcome to yet another article related to Python. In this series of articles we are going to take a look at several design patters (creational, behavioral, structural), what are they and how we can implement them in Python.

Photo by Lalit Kumar on Unsplash

The factory design pattern (FDP) falls under the category of creational design patterns along side abstract factory, builder, prototype and singleton. The FDP is based on a single function written to handle object creation. We execute it, passing a parameter that provides information about what we want, and, as a result, the wanted object is created. During this process we are not interested how the resulted object is implemented or where it is coming from.

Imagine that we are building a ticketing system and we would like our customers to be able to create different type of tickets. Since we are not aware what the customer would like to create (as a ticket type) we need to support it somehow. The factory method would allow us to create a common interface that the user will be using during ticket creation. Since we are in the beginning of the development process, we decide to support only couple ticket types: incident and problem, but we think to expand to other types in the future. That would not be a problem since the factory method would allow us…

--

--

Jordan P. Raychev
Geek Culture

Network, system and software engineer with true passion about technology. Love to read and spend time in nature.