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

Introducing the Factory Method

00:00 In this lesson, you are introducing the actual factory method. So far you have seen the _get_serializer method in the Creator class, but now you are introducing the actual factory method, just to move a step closer to the Factory Method design pattern.

00:20 To enforce the use of the factory method, you are going to use an abstract base class for the Creator class. You might wonder what the point is of using an abstract base class here. In the previous lesson, you learn that abstract base classes are useful to enforce structure when there are a large number of child classes, but here there is only one Creator class.

00:45 So what’s the point of using an abstract base class? And that is a very valid point. In this particular example, there is no need for an abstract base class, exactly to your point there is only one child class.

00:58 But in larger projects there might be multiple creators. And also, if you do more research online, you will often find that abstract base classes are used for the creator.

01:10 So in order to future-proof the code and to help you on your journey of discovery, you’ll implement the abstract base class here as well for the creator.

01:21 So please scroll down to the Creator section, and here at the top create a class called CreatorBlueprint.

01:31 And for that to be an abstract class, it needs to inherit from ABC.

01:36 The method you will be introducing is called a factory_method.

01:41 And that method takes self as an input parameter and it doesn’t do anything, so just type pass. It just needs to be defined. Now remember, you have to make sure you put the decorator on top.

01:53 So @abstractmethod. This is the same thing you have learned when you were introducing the ABC for the products. Therefore, what else do you need to do?

02:05 You might remember that now the SerializerCreator on row 48 needs to inherit from the CreatorBlueprint. So please do that. Go to row 48, open brackets, and paste CreatorBlueprint.

02:21 And now I’ll leave you a second to think whether this is the end. Will this work? Let’s have a look at what’s on the screen. You have the CreatorBlueprint abstract class that defines the factory_method on line 45.

02:37 But then your SerializerCreator, which is your actual Creator class, which inherits from the CreatorBlueprint, at the moment, does not have the factory_method.

02:51 Remembering the example during the lesson where you introduced the abstract classes for the products, that will cause the code to crash because the CreatorBlueprint will enforce the factory_method.

03:04 Given that you have defined factory_method in your abstract class, it needs to exist in the classes that inherit from the abstract class. So in other words, SerializerCreator needs to have a method called factory_method.

03:20 Now so far, the factory_method has been the method to get_serializer. So all we have to do is change the name of that method to factory_method.

03:34 Now the final thing to change is then of course where we were calling the get_serializer method, we now need to be calling the factory_method.

03:42 So if you move up to the serialize function, which starts on line 32. If on line 34 you replace get_serializer by factory_method, everything should work just fine. So please give that a quick check.

04:01 Make sure you save the code, then restart your REPL

04:06 and then type your by now familiar from serializer import Song, Book, and serialize. Press Enter, and then use up arrow to find something you fancy.

04:18 For example, my_song, let’s create the song and then let’s serialize that song. So serialize(my_song) to JSON, for example. And there you have the output.

04:31 It’s confirming that it is the JSONSerializer being used, and you have the expected output there below. And use the up arrow to serialize using XML and as expected, serializer is XML, and you have your output in XML format.

04:51 In the next lesson, just for good measure, you will be using abstract base classes for the client objects Song and Book.

Become a Member to join the conversation.