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

Working Towards a Register

00:00 Over the next two lessons, you will be working towards a register and that will make your code more flexible and even more maintainable than it already is.

00:11 Now, as a first step, you’re going to move away from the if, elif, else conditional code in your Creator on lines 61 to 68.

00:22 Now, at the moment there are only three products. So one if and two elifs. But as that number of products grows and therefore the number of elifs increases, you might want to consider a more readable solution in the form of a register.

00:38 So the first step in implementing a register is to create an internal dictionary in your Creator class. Now the keys of this dictionary will be the data formats.

00:49 So JSON, XML, and YAML. And the values in this dictionary will be the associated products. So _JSONSerializer, _XMLSerializer, and _YAMLSerializer.

01:07 So in the __init__() method of your Creator class, add self. and then underscore because we want this to be an internal dictionary.

01:17 And I’m going to name this dictionary products. Why? Because the values will be products and that is a dictionary.

01:28 The second step is to then create a method in your Creator class that allows you to populate that internal dictionary. And that is going to be called register_format().

01:41 So in your SerializerCreator, you will need to create a method called register_format(), but hang on, your Creator class inherits from the CreatorBluePrint.

01:53 As you can see, in line 55, there is inheritance and you will remember that is inheritance from an abstract base class. Now the abstract base class sets the rules.

02:05 So first, you will need to create this method in the abstract base class. So that is your CreatorBluePrint class, which starts on line 50.

02:15 So go to that class and firstly get your decorator @abstractmethod

02:23 to decorate that new method. So def and the method is called register_format().

02:31 That method takes self as an input parameter, just as the factory_method did. And again, this isn’t going to do anything, this is just defining the method, so type pass. Now that you’ve set the rules in your CreatorBluePrint abstract base class, move to your SerializerCreator, so the Creator class, and create the register_format() method here.

02:56 So on line 64, create some space.

03:01 register_format(self, is the first input parameter. And then secondly, there is data_format

03:10 because those will be the keys of the dictionary. And then the values of the dictionary will be products. So also have product as an input parameter, and those will be product classes.

03:24 Excellent. So now the objective of this method is to populate the internal dictionary. So do exactly that. So self._products, which is the internal dictionary you defined in line 62, that is a dictionary.

03:42 So open the square brackets data_format that is the key, and assign the value, which is the product. There you go. So now you have your internal dictionary, and you have a way of populating that internal dictionary.

04:00 The next two things to do, and you will cover those in the next lesson, are firstly to have the factory method read from this internal dictionary, and secondly, to actually populate the dictionary.

Become a Member to join the conversation.