Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Implementing the Factory Method Pattern in Python (Summary)

Factory Method is a widely used, creational design pattern that can be used in many situations where multiple concrete implementations of an interface exist.

The pattern removes complex logical code that is hard to maintain, and replaces it with a design that is reusable and extensible. The pattern avoids modifying existing code to support new requirements.

This is important because changing existing code can introduce changes in behavior or subtle bugs.

In this video course, you learned:

  • What the Factory Method design pattern is and what its components are
  • How to refactor existing code to leverage Factory Method
  • Situations in which Factory Method should be used
  • How Object Factories provide more flexibility to implement Factory Method
  • How to implement a general purpose Object Factory and its challenges
  • How to specialize a general solution to provide a better context

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

00:00 Congratulations, you have reached the end of this course. You’ve been on a long journey where you went from conditional code to a basic and then an advanced implementation of the Factory Method pattern.

00:15 You’ve learned how to implement the components of the Factory Method pattern that was the client code, and the creator, and the factory method and then the products, and you improved the design of existing code by applying the Factory Method pattern.

00:32 You’ve also learned how to select an appropriate implementation of the Factory Method pattern. Remember, you went through a number of iterations where you gradually improved the code, and so now you can select which of those levels of improvement you would apply to your particular projects.

00:53 So thank you for going on this journey. I’ve really enjoyed being on this journey with you, and I would encourage you to check out some of the links here.

01:02 That will give you a lot of extra background. For example, there is the tutorial on which this video course is based. It’s The Factory Method Pattern and Its Implementation in Python.

01:14 There’s actually more information in that tutorial. Then there are Python classes. Of course, you have used classes throughout this course extensively. There was also mention of the use of underscores, there was the implementation of the YAML format.

01:32 Then you learned about data classes, abstract base classes, and Python protocols. So that’s a lot of good content for you to get stuck into.

01:45 So thank you for watching, and I hope to come across you again in one of the next courses.

Avatar image for p.r.worrall

p.r.worrall on July 14, 2025

In this excellent video course I learned how to use the Factory Method Pattern to implement Serializers. My only suggestion is that I would have appreciated some pointers on how the method could be used for Deserializers.

Avatar image for Steven Loyens

Steven Loyens RP Team on July 15, 2025

Thank you for your feedback, that’s much appreciated!

I think the beauty of the method is that the Client doesn’t need to worry about that. All the Client has to do is establish an interface with the Creator, for example, pass a dictionary my_dict to the Creator and tell the creator to organise the deserialization of that dictionary into a class, maybe something like deserialize(my_dict, "class").

The Creator will then call the Factory Method which will trigger the appropriate Deserializer Product, the instance of which can be returned to the Client. In my mind I’m just going through the little graphic discussed in the course. So, the setup would be the same, but the products would be different.

Not related to the Factory Method pattern itself, but further to your question, to deserialize a dictionary to a class you can use the __dict__ instance attribute in the __init__ method of your Product class which will make the dictionary keys available as instance attributes of the product class, for example

import json

class Dict_to_Class_Deserializer:
    def __init__(self, my_dict):
        self.__dict__ = json.loads(my_dict)

A video course on the use of __dict__ is being edited at the moment, so should be available shortly. In the meantime, there is a great written tutorial: Using Python’s __dict__

Hope that helps!

Become a Member to join the conversation.