Skip to content
← ALL POSTS

Fun with Factory Design Pattern and Python

Discover how the Fun with Factory Design Pattern and Python simplifies object creation while boosting scalability and flexibility in software projects.

MD. RAISUL ISLAM4 MIN READ

In the world of software engineering, design patterns serve as fundamental solutions to recurring problems in software design. These patterns are not tied to any specific programming language but offer universally accepted strategies for creating efficient, maintainable, and scalable code. One such essential pattern is the Factory Design Pattern and Python, categorized under creational patterns, which focuses on separating object creation from its usage.

What is the design pattern/ Software design pattern?

Consider a design pattern as the magic wand that every coder wishes they had, but more akin to a cheat code that is shared among developers globally. It’s like having a secret recipe for solving common problems in software.

When pattern word comes to us what do we think?

A repeating black and white zigzag pattern
A honeycomb pattern of outlined hexagons tiled edge to edge

When software engineers try to solve a problem, always try to find a pattern that they already used before or try to create a new one. Because it is very important for future uses and others can understand it well. That’s why design pattern comes and there are some patterns that are approved worldwide. These are very much logical things. Not dependent on any programming language.

In software engineering, a design pattern is a known repeatable solution to a commonly occurring problem in software design. A design pattern isn’t a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. (Collected from Source Making)

What is a Factory Design Pattern?

When I use the factory word you may have made a picture of a factory right? Don’t worry, you are right.

Think about a factory where they produce cars. You don’t know how they do it. But you can order a car from there and you can ride it. This is actually a factory pattern says.

Let’s read about the definition.

The factory design pattern is a design pattern under the creational pattern, which is not exposing the creational logic to the users. This is one of the most used creational patterns by software engineers.

Now let’s make some fun with the factory design pattern.

The factory pattern drawn as a bike factory. On the left, one abstract bike design produces a yellow, a blue and a red bike. On the right, a user asks the factory for a blue bike and never sees how it is built

I think the above bike factory image is enough to understand the whole logic of the factory design pattern.

Specialties of this pattern

  • Separate creation from uses
  • Does not expose the creational logic to users
  • Makes code more robust, less coupled, and easy to implement
  • You can extend without touching the original code.
  • Almost many object creational problems can be solved by this pattern

Now let’s try to draw a UML diagram for the factory pattern.

UML diagram of the factory pattern. A Bike abstract class declaring CreateEngine and CreateChassis is implemented by Yellow Bike, Blue Bike and Red Bike, each fixing a colour. Bike Factory exposes YellowBike, BlueBike and Redbike methods, and User calls BikeFactory with the string yellow

Now it’s time to implement this pattern using Python code.

Let’s create an Abstract bike class.

from abc import ABC, abstractmethod class AbstarctBike: color = "" def create_chassis(self): print("Create bike chassis") def create_engine(self): print("Create bike engine") @abstractmethod def coloring(self): print("Coloring the bike")

Benefits of The Factory Design Pattern

By separating the creation of objects from their use, it facilitates code simplification and encourages a clearer structure.

Encapsulating the creation logic improves the scalability and maintainability of the code. It can be extended to create new objects without changing the existing code, which makes it flexible enough to adjust to evolving needs.

This pattern can be used to solve a lot of software engineering object creation problems.

Conclusion

One effective method for controlling object creation in software development is the Python Factory Design Pattern. It improves readability and organization of the code while enabling extensibility and robustness through the use of abstraction and encapsulation.

Developers can create scalable and effective applications by understanding and putting the Factory Design Pattern into practice. This applies to both simple and complex objects. Adopt this pattern to simplify your software design and provide well-organized, easily maintainable code for your projects.

NEWSLETTER

One engineering letter a month

New writing from our engineers, no marketing filler.

[ ONE CONVERSATION AWAY ]

Rather ask an engineer than read another post?

Thirty minutes, no sales script. Bring the problem you are actually stuck on and we will tell you honestly whether we are the right partner for it.