Loading video player…

Simplifying the Creator Function

00:00 In the previous lesson, you created the _get_serializer() Creator function. In this lesson, you will be simplifying that function.

00:09 If you look at your Creator on line 16, you’ll see that there are two input parameters. There’s song and data_format.

00:18 Now, the data_format input parameter is used to make that decision. Of course, whether that is JSON or XML or something else, the song is really only passed into _get_ serializer because it is needed as an input parameter into the Products functions.

00:35 So what I’m talking about is line 18 and line 20 where song is passed into either _serialize_ to_json or _serialize_to_xml.

00:46 You can simplify this a little bit by saying, well, if I don’t execute the functions on lines 18 and 20, then there is no need for song to be passed into _get_serializer.

01:00 So instead of executing the Product functions, we could have the Creator just return the function objects. So what does that mean? It means that on line 18, for example, I return not the execution of _serialize_to_json, but if I delete the parentheses and song within it, I just return the _serialize_to_json function object, not the execution.

01:31 So this is not executed when _get_serializer is called. So you’ll do the same thing on line 20: type return,

01:42 and you return the function object by deleting the parentheses and song with it.

01:49 So now that means that on line 16 song is no longer needed as an input parameter, so you can delete that there as well. Of course, then you would need to update the serialize() function on line 12 and in particular line 13, where you call _get_serializer and you pass it song that is no longer needed.

02:10 So you can delete song because song only takes one positional argument.

02:16 So now when you call on line 13, the _get_serializer function, it no longer executes a function, but it returns a function object. And therefore I would like to catch that object in a variable.

02:30 I’m going to call that serializer_product.

02:35 So I’m calling that serializer_product because _get_serializer returns a function object, and that function object is one of our product functions being serialize_to_json and serialize_to_xml.

02:50 So now all I have in serializer_product is a function object. That function still needs to get executed. So that’s what you will do on the next line.

03:00 So return, and then we want to execute serializer_ product.

03:09 And to execute this, you will need to pass in song.

03:15 That’s because serializer_product on line 13 captures what is returned by _get_serializer, and _get_serializer on line 17 returns either the serialize_to_json or serialize_to_xml function object.

03:33 And those function objects are defined lower down lines 27 and below.

03:40 You can see that those functions take song as an input parameter, so in line 27 and line 31. So therefore, to execute those functions, scrolling all the way back up to our serialize() function on line 14, you can see that you need to pass song into the serializer_ product function object.

04:06 Now that you have created a Creator and you have created your Products, you have in fact implemented the basic Factory Method design pattern.

04:18 Let’s see how it works. If you move to your REPL, don’t forget to exit the REPL and then start it again just to make sure everything’s refreshed.

04:29 And then you can use the up arrows to import. So from serializer, import song and serialize(). Create your song. So my song is “Forest” by The Cure.

04:42 I’m going to give it a different ID just to show that works, and then serialize my song into JSON, for example. There you go, id 65, that works.

04:55 Press up again and change JSON to XML.

05:01 That works as well. And we can try YAML and that should not work. It should trigger the else part of the if-elif-else, and indeed a ValueError is raised.

05:16 So you have changed quite a bit of code in the background, but importantly, your interface has not changed. You are using the up arrows to trigger the exact same lines of code as you were doing before.

05:30 So you might think, well, that was all a little bit pointless then, wasn’t it? Well, no, it’s not. That’s entirely the point that for the user, this is all irrelevant.

05:39 Everything happens in the background, but the code in the background is a lot better. It’s a lot more structured, and you are moving closer and closer to a proper implementation of object-oriented programming, which comes with all the benefits as discussed.

05:55 In the next lesson, you will summarize the changes to the code that you have made so far, and you will see how this all fits in the Factory Method pattern.

Become a Member to join the conversation.