Resource linked in this lesson: Single and Double Underscore Naming Conventions in Python
Creating Products in a Basic Implementation
00:00 In this lesson, you will be converting your initial version of your solution into a basic implementation of the Factory Method pattern. And you’ll be doing this in a number of steps, three to be exact. In the first step, you’ll create products.
00:17
In the second step, you will create a creator, and in the third step, you will be simplifying the code a little bit. So in the first step, if you move to line 12, you will find the serialize()
function.
00:30 And the idea is that you’ll be taking out the implementation of the JSON format, which is on lines 14 and 15, and the XML piece, which is lines 17 to 22.
00:43
The idea is to take that out of the serialize()
function, and stick it into two separate functions: one for JSON, one for XML, and those will be your product functions.
00:55
So if you move to the bottom of the code and create a few lines, I would start with a comment that says Product
, so hash Products
, that’s just for clarity.
01:08
And then create the first product function, which is called _serialize_to_json()
,
01:15
and it’s all in lowercase. And that takes a song
as an input parameter. And for the moment, just type pass
, you will come back to that in a second.
01:25
Also, create the second product, which is def
_serialize_to_xml()
, again, all in lowercase. And it also takes song
as an input parameter.
01:38
And for now, again, just type pass
.
01:41
Now to move the functionality out of the serialize()
function on line 12, I’m just going to scroll up to lines 14 and 15, select those two lines, and copy them.
01:52
I press Ctrl+C, and then scroll back down to line 29 and overwrite the pass
there by pasting in what I just copied. Now my indentation doesn’t quite work, so make sure you use backspace to get your indentation right.
02:09
So now you have copied out that functionality out of the serialize()
function.
02:15
Now in the serialize()
function on lines 14 and 15, you no longer need those two lines, but what you do need is to call the _serialize_to_json()
product function you have just created.
02:28 So instead of those two lines,
02:32
make sure your indentation is correct, and then just call _serialize_to_json()
and pass in the song
.
02:43
So all you have done is you’ve separated out the serialize_
to_json()
functionality in a separate product function. And you call that function from within the serialize()
function on line 14. You’ll be doing the exact same thing for XML.
03:00
So if you select and then copy lines 16 to 21, and then move down to line 32 and paste, overriding the pass
,
03:14
and then selecting and making sure your indentation is correct. So now all that functionality, the XML functionality, sits within the _serialize_to_xml()
product function.
03:27
You then move back up to the serialize()
function on lines 16 to 21. You can delete those lines. Again, make sure your indentation is correct, and then call the product function you’ve just created so _serialize_to_xml()
.
03:45
And that takes song
as an input parameter. So that was the first step you have created two product functions. Now, you might have noticed that those product functions start with an underscore, and that is because they are internal, they are non-public, so they are internal within this module.
04:04 That means they are not supposed to be used outside of this module. If you want to find out more about underscores and the use of underscores in Python, I include a link to a video course that explains all of this in detail.
Become a Member to join the conversation.