Adding a Playlist
00:00 During this course, you have made quite a few changes to the original code, and in this lesson, you will get to experience the power of some of those changes.
00:10
Say that the client now not only wants a song id, song title and a song artist, but also wants to add a playlist to the Song
class. In this lesson, you will see that all you have to do is change or add two lines of code.
00:27
So I invite you to add those two lines of code. In line 20, the first thing to do, of course, is to add a fourth attribute. So type playlists
, which is also going to be a string.
00:41
And the second line of code will be in the use_product
method that you can see on line 22. And all you have to do is add another property. You added a title on line 24 and artist on line 25.
00:57
So on line 26, you’re going to add playlist
.
01:02
I just use a copy-paste and then instead of artist, I enter playlist
.
01:08
And then self.artist
will become self.playlist
.
01:14
And this is where the power of your changes becomes obvious because in the first version of the code, before you made the changes to your product classes and made your product classes so flexible, this would’ve involved, well, firstly, of course, adding an attribute to the .__init__()
method of the Song
class, so that hasn’t really changed.
01:34
But secondly, you would’ve needed to change the dictionary in the _JSONSerializer
product. And thirdly, you would’ve needed to update the element tree in the _XMLSerializer
product.
01:47
Of course, you still need to add the playlist as a class attribute to the song data class, but then all that’s left to do is update the use_product
method in the Song
class.
02:00
Now, what is very important here is that there is no need to change the product code. You have made the product code so flexible that it can automatically deal with this change in the client’s object in the Song
class.
02:14 This is worth thinking about for a second. The only change required is a change to the client code not to the products. So the client is free to make the changes they require, and the client code is therefore clearly separated from the product code, from the object creation.
02:33 And that is one of the key features of the Factory Method design pattern. Now, all that’s left to do, of course, is just to give it a go. So please make sure you save the code and restart the REPL.
02:48
So from serializer import Song
and serialize
, create a song, my_song
. So that is an instance of the Song
class.
02:58 We need an id, we still need a title.
03:05
We still need the artist, but now we also need the playlist. And my playlist is my liked_songs
playlist.
03:16
And now serialize my_song
,
03:23 and let’s use JSON. And there you have it. There’s your output, your serializer is JSON. And the last row here you can see that playlist has been added to your output.
03:38 So you have now implemented an advanced example of the Factory Method. In the next lesson, you will summarize the findings.
Become a Member to join the conversation.