The Factory Method design pattern in Python is a creational pattern that allows a superclass to declare an interface to create objects while allowing subclasses to alter the types of objects created, thereby resolving problems related to code coupling with operating systems.
Abstract
The Factory Method design pattern is a powerful tool for creating objects in Python that separates the process of creating an object from the code that depends on the interface of an object. This pattern is particularly useful when building GUI applications that need to work across multiple operating systems, such as Windows, macOS, and Linux. The Factory Method pattern involves creating an abstract class, concrete classes for each operating system, and a Factory class that builds objects based on a parameter. This allows for easy extension of the program and the addition of new types of objects without dependencies. However, it can also make the code more complex and result in a large number of small classes.
Bullet points
The Factory Method pattern is a creational pattern that separates the process of creating an object from the code that depends on the interface of an object.
This pattern is useful when building GUI applications that need to work across multiple operating systems.
The Factory Method pattern involves creating an abstract class, concrete classes for each operating system, and a Factory class that builds objects based on a parameter.
The Factory Method pattern allows for easy extension of the program and the addition of new types of objects without dependencies.
However, it can also make the code more complex and result in a large number of small classes.
The Factory Method pattern is one of the most commonly used design patterns in Python.
You can also find all the code used through this series on GitHub.
The Factory Method pattern is a creational pattern allowing a superclass to declare an interface to create objects while allowing subclasses to alter the types of objects created.
Like most of the creational patterns, it separates the process of creating an object from the code that depends on the interface of an object.
Problems the Factory Method can Resolve
Imagine you’re building a GUI app. The first version of your app only works on Windows. But later, you want to extend your app and allow it to run on macOS. But your code is coupled with Windows.
Later, if you want to make your app work on another OS, like Linux, you will have to make changes again.
It would be more efficient if your code was not coupled to the OSes, no? It’s why the Factory Method is here.
But first, let’s check where is the problem in the following code:
As you can see, if I want to add another dialog for Linux, I have to modify the client code and add another elif .
Solution
Let’s start with the code, and then I’ll explain it:
Now, as you can see in the main I don’t have to change the code anymore, because the dialog is created by the factory and is not coupled with the client code anymore.
To implement this pattern, first, you have to make an abstract class. In this case, this is the Dialog class.
Then, you can make as many concrete classes as you want. In this case, I just made two classes, one for a WindowsDialog , and another for a macOS dialog.
Finally, you have to make a Factory class. This class is used to build your objects depending on a parameter. This is the DialogFactory .
If I want to add a new OS to the code, I just have to create a new concrete class, for example, a LinuxDialog , and add a condition in get_dialog .
Combining Factories
A powerful way to use factories is to combine them. In our example, we were building dialogs. We can also build a whole application, using dialogs and other objects, such as buttons. Let’s see how to do this efficiently:
First, we create our abstract classes and subclasses. Then, our factories. Finally, we combine everything in the Application class.
Now, if we want to extend our application, we can do it in just a few lines of code!
Applicability
I guess thanks to the examples you know when you should use the Factory Pattern, but let’s summarize this here.
You should use the Factory Pattern when you don’t know beforehand the dependencies and types of objects you will work with.
You should use the Factory Pattern when you want to provide a way to extend the components of your program easily.
Advantages
You can easily extend your program and add new types of objects without dependencies.
The coupling between creator classes and concrete products is reduced.
Disadvantages
Factory Method can make the code more complex.
You end up having a lot of small classes instead of having everything clustered.
Factory Method is at the same time flexible and not flexible. Indeed, it’s easier to add new classes, but you can’t create them dynamically. For example, in a drawing software, perhaps the user wants to create custom shapes. It’s not easy to do with the Factory Method.
Final Note
This pattern is one of the most used patterns. It’s also the first you discover when learning design patterns because it’s easy to understand the problem it solves and the solution.
To explore the other stories of this story, click below!