Loading video player…

Advantages of the Factory Method Pattern

Resource linked in this lesson: Python Classes: The Power of Object-Oriented Programming

00:00 In the previous lesson, you were introduced to the Factory Method pattern. Let me walk you through some of its key advantages. So it separates the process of creating the object from the code that depends on the interface.

00:14 We’ve established that, so you are in fact adhering to the single responsibility principle. This principle states that a piece of code, like a function or a method or a class, only has responsibility for one thing.

00:29 So it should only do one thing, and therefore separating the client code from the code that creates the objects is a step in the right direction. The user of the interface shouldn’t worry about how the objects are created.

00:44 In doing so, you also establish decoupling, so it reduces dependencies and that improves code stability, and you also improve maintainability. It makes a code more modular and easier to understand and modify.

01:01 So, so far you have looked at what the Factory Method pattern is and what it’s trying to achieve. So let’s have a quick look at its implementation and the advantages of that.

01:13 So the implementation of the Factory Method pattern is usually based on object-oriented programming principles and therefore with that come the usual benefits or advantages of the OOP paradigm.

01:28 So that is enhanced code organization, reusability, scalability, and maintainability. Now, if you want to study that in a bit more detail, I include a link to an excellent Real Python tutorial.

01:42 I did say that the implementation is usually based on OOP because it’s not always. In fact, you will look at an example where this is not the case.

01:53 Well, with all that theory now fully understood, I think you are ready to move on to some coding. In the next lesson, you’ll study the initial code for our serializer example.

02:04 You will then rewrite that code using a basic implementation of the Factory Method pattern. And in the lessons that follow, you will then gradually improve the code to a fully fledged Python implementation of the Factory Method design pattern.

Become a Member to join the conversation.