17
May

Design Patterns in Python by Peter Ullrich



which if I pronounced it right a one-stage thank you thank you very much all right it's good morning everyone I hope I don't breathe into the microphone but well good morning everyone thanks a lot for having me my name is Peter I'm a computer science master student from the Netherlands and my education focused a lot on software design and architecture in the past year and I became quite passionate about this topic because it allows you to go from raw code that is unclear and right in front of your face to more abstract terms so here for example we use very easy UML or just a very standard modeling to model a system that has an API some logic a model an SDK and that communicates with the outside world so if you take your code and you put it in such a model you immediately see how your system works how it interacts and most importantly you will also see disadvantages or security flaws and there's a software design when it's more a code level and then you have software architecture that is more on a structural level so you can for example model how how many api service we have for that you have a load balance in between and how you connect to the database and these kind of things so that's how you model more your whole system so these two topics became quite important to me and I would like to give you a very short history of design patterns of software design namely software design wasn't didn't come from actual software engineering but it came from real-world architecting so really building buildings and this man Christopher Alexander he was so to say the founder for something that is called at the pattern language so he wrote this book in 1977 and he actually in this book proposed 253 design patterns for real world architects so when you want to build a building you don't have to reinvent the wheel you don't have to think whether you put the dining room next to the kitchen or next to the bathroom these kind of things when you want to think about what kind of interior you need for a certain room or what kind of kind of styles work nicely together u.s. real word architect can go back to this book just have a look at it and pick your design patterns out of that book and this pattern language actually became quite interesting also for other fields for example software engineering and these four authors they were became known as the Gang of Four those were the first authors of the first people in software engineering who used the design language in software engineering and this book in 1994 was the very first milestone on well book milestone in this area and in this book they proposed 23 different design patterns and they are still used today and they are language independent where they use Python or something else you will always mostly use these design patterns let's list actually got extended now by different people it's around forty forty design panels now you can find them on Wikipedia if you want to look up all of them but you might actually ask here so what's actually a design pattern you're all practical people so let let me give you a practical example of the proxy pattern so you have a problem so a design pattern in general it's always about a very general problem and it explains that problem and then it offers a very general solution to that problem and you can then implement a customized version of that about that pattern in your code so well our problem is that you have high resolution images on a website and because when a customer comes to your website he or she has to load all of these images at once so you have a long loading time it takes a long time and you also want to be able to style these images so the problem is basically the long loading time so if you look the if you look at the proxy pattern then the solution offered is that you simply replace these images with placeholders and whenever and you can play style these placeholders and then whenever a user comes to that part of your website you load them down it dynamically so you download all the web images in the beginning but then over the time when they use any needs them then you load the image and if you look at the if you look at it in in UML a little bit that's how you would implement a proxy pattern so you have an image proxy let's ponder see if those image foxy both of them an image proxy and an actual image the image proxy holds the CSS and a connection to the actual image that holds like a file path or something else a ul to the actual image it's just an example when you want when you will implement the proxy pattern in your code it will actually look differently but that's in general terms how you would do it so that is a design pattern we now let me give you a little example of how you can actually use design patterns and I made a little comic story about this and I hope you like it so this is evil overlord and as well evil overlords he obviously wants to take over the world he wants to control everybody any things well people are a lot in front of their smartphones and they're kind of controlled by the smartphones so if I just control the smartphones I can control the world so he goes to his little toys minion in his dungeon and he says create three updates which gives me pseudo rights over iPhone users because he wants to start there and the minion obviously obeys he gets the updates he goes to the iPhone users he makes him update his malicious update but once the iPhone users have it the Android users show up and they're like man these nasty iPhone users with a fancy features and everything we also want that update ins give us the update say says ok I will do that he goes back to his dungeon and ask us meaning well give me seven updates to control the Android users and the mini says well it's a reflect today but we all know it's not that important to your factor is it yes I will get right to it he makes the up the Android users also update their phones with a malicious update but then all of a sudden they're in a Windows Phone user shop and we're like well would you mind developing this update for Windows Phone as well and we all know it's a little bit hard to develop for Windows Phone sees like total despair no and one eternity later he finally creates the update everybody's almost dead and people Windows Phone users are like well thank you very much they install the update well poof they're gone thank you all right that's a lean introduction but a little sure story here and now actually the Overlord he has this malicious update on all the phones but he actually has a problem because you see he has three different groups of people and they speak different languages so elvish for example – or fish and the humans speak Swedish and his language is English so whenever he wants to call when he wants to send a command so to say to one of these groups he has used either either elfish dwarfish or Swedish and he has to translate that first from his English language so you have to a lot of you have to customize a lot and he looks into his book of gamma 4 and he sees the adapter design pattern Yuri allows it because with the adapter pattern you can just simply add another class and adapter class in front of your minion class Jose and you will do the translation in that adapter class so you just have to call you have you have to as a one unique command that you can call on the adapter and the adapter will translate for you I will just show you because I it's my first dog and I heard from a lot of people that it's a great thing to do a live demo yeah that's always a great idea it works for lousy so I created these example classes I hope you can see that you have some like else some dwarfs and humans and all of them have kind of the same commands it is actually translated from the English call me apparently to the Internet in elvish it's known in and dwarfish it's astronaut oh and in Swedish it's a me pronounce it and kinda correctly and you see it's kind of a same command but you have different names for it and what you could do was that what the overlord could do you iterate over all you in a minions and then you check every time okay if it's a if it's an elf I will call this function if it's a dwarf I will call another function otherwise I call the default function so to say but I mean everybody I think you spot already the problem here because if you add any other minion groups like L like trolls and orcs then you have to create more and more of these else if statements and it just gets longer longer and it's hard coded which we don't like so when we use the adapter pattern we can actually create an adapter for each of these groups so we have an elf adapted rorf adapter and a human adapter and they all like they all look quite the same you just when they you create them you hands on a minion object that you want to adapt and they all have the same function which is a call me function and the elf one called Nolan and the dwarf one called this one and the humans calling me so you create an adapter for every group but then again it works yeah you can iterate over the minions and you only have to call one single function and adapter takes care for you and it works but again you see the problem whenever we want to create another group you have to create another adapter as well so what if we just use a very general adapter class for any minion and that's a little bit complicated but I will walk you through it it's just a meaning that doesn't matter whether in hell for dwarf and it's same you create a you personal and minion object and you also pass and some adapted methods and you can create any you can pass on any method you want to adapt so we have the wing make function for example we can hand this in here and then you just iterate over them you get that function from the actual object and you set it onto the adapter find a class object with the new sort of a name with a new key so you take the wing make function and you register it on the adapter with a new name so you can call it under a new name and the and I do this here so I create for example a minion adapter for an elf and I say that I want to have a Call Me function and that should be the non in function on the elf object and I can do the same for the trough and the humans and then I can simply again just iterate over them and say and call the call me function but another cool thing about this adapter is that you can still get and set any attribute any variable any function on the actual object on the actual minion object and you don't set it on the adapter but on the actual object so also at one time you can just rip out the adapter and you still hold the object with all the attributes set so this is what you would do with the with the get and set so this code actually I think will be online afterwards so you can just look at it in peace at home but when we run this we actually see that okay all that else you they call home and the humans at the dwarfs call home and now I tested okay I looked at the name on the actual adapter and I checked it with the name on the actual minion and then I change it you see that's the same and then I changed only the adapters name and check it again and you see that on the adapter but also on the minion object the names changed okay so this is the adapter pattern and our evil overlords happy because he only has one function to call but still every time when he wants to call all in summon all those minions he has to make one call for every minion he has so you just iterate over them and call so many functions he just wants one update rule them all just one function and the minions as well it sounds complicated but I'm pretty sure there's a design pattern folders and the minion is well educated because there's the facade observer pattern the facade observer pattern you probably all have used it before you also node with the name interface so if you may ever use an API that's actually interface it's a facade facade design pattern so with a facade design pattern we can put something in between that handles all all our calls for us so we just have to make one call to the facade and the facade again calls all the minions I'll show you again how that looks like and we have again our adapter and then I created a minion facade and I also created a function to create all the minions to take all over the people and that's the same that you know from before I just create a couple of adapters but then I have this one function to rule them all and I can summon all the minions and that's just as we have seen before a for loop of all the minions but I can just call one function that's nice and the overload on the other side just has to create to call two functions they create minion function and then the summoned minion functions so not like a for loop not a hundred calls going out just a single call to the facade all right so with the facade pattern in place the Overlord still has one last problem because the overlord he is a very busy person and very busy people have to go on vacation every now and then but when he's on vacation he doesn't want to check his laptop all the time to check whether all his minions are still in a good state and they're all under his control but he actually wants to get a notification whenever something changes on these minions and he looks up his Gang of Four book and he just burst through it and he actually finds the observer pattern so you might have used the observer pattern before especially if you if you have used Java because it's baked in it's a built-in function there and with the observer pattern we can just we can just make all these minions call back to the observer to our overlord every time when something changes so we don't have to actively poll and I ask like hey something changed no okay but they notify us when something changed if we go back to our code so this is the observer pattern it's two classes it's taken mainly from from but some Python you have the observer class that is the class of the object that gets notified and it has a single function which is an update function and it gets an object some arguments and some key value arguments so all the information you want to hand in there you can you can just change it as you want and then the more important part is the observable class which is the object that notifies when something changes on it and it just has a list of subservice you can add an observer you can remove observers and there's the most important part whenever something changes we simply call notify observers and we iterate through all the observers and simply call the update function instead like hey something changed here with this argument and this key value argument so when we use it in our in our project first of all I created an evil overlord class and that evil overlord class has an observer is inherits from the observer so this one will get notified and it will implement the update function so we just say we received the message and that's the information we got and then I decided on you making the minion adapter one and observable because we already wrote the gate attribute and set attribute functions here so whenever you change something on the actual minion object you will most likely go through the adapter and everything I have to do actually is make this inherit from observable and then say okay whenever something changes whenever we set an attribute on the object on the minion which is notify all the observers and we just send the like which attribute has changed to which new value yeah so when you run this again you see that first we create the minions we add and observer to the elves because I I added an function called just money told the elves for now and you simply add the overlord as an observer to the elf object and then yes we add the observer and then we just for test purposes change the elves name and we do that here here so we just say okay we change the name and then we changed the name on the adapter but as you know already it's actually changed on the minion object and then we say well the elves name changed and this is exactly what happens so we changed the elf name and then hey the evil overlord received the message something changed it's not good I have to look at my phone and the message that the message you got is from a minion adapter and we see that the attribute name was changed Elrond and then we say okay we successfully changed the elves name so this is this is how you see observer pattern you can use it it's it's a simple implementation that I've shown you here when you implement it especially in a more concurrent environment then you have to make sure that it's thread safe and that's a little more complicated but still you can find those observer pattern on the internet and just copy-paste it okay so with this observer pattern in place our evil overlord is happy and you can finally go on vacation about design patterns whenever you maybe maybe you have heard about them before and maybe you've read some articles and in the community there are a couple of things that are criticized on these design patterns and one of them being is well design patterns can mostly be replaced with built-in functionality and about this so we don't have to implement them ourselves they already kind of in our languages we just have to use the language right and about this criticism I have to say yes there's some truth to it but also know there are some things that don't really match so yes some design patterns are already built in for example the iterator pattern it's an actual pattern but you are if you use Python you're so used to it that you don't see it as something weird or something cool on you because it's just built in if you use a list comprehension anything you get irate if you heard that if you use something else like C++ or C you don't have an iterator function entirely sure about this but I heard that you have to at least when they wrote the book the Gang of Four then you had to implement the iterator thing yourself every time when you want to use it which is not really nice so this one is built in yes but many are actually not build in so as I shown already is the facade the interface you can't really have one fits all interface or facade you also can't have one fits all adapter it will always depends on the class you want adapt so this that's my opinion on this criticism and the second criticism you see a lot as well design patterns I'm really more for Java and c-sharp and not really that much for Python I must say that I disagree with the strong lis because yes these design of design patterns in general somehow more prevalent in these fields if you want to become a Java developer fresh from university you apply for your first junior developer job you must you you really have to know that the design patterns you have to know the names and you have to know how to implement them because they will most likely be part of your interview for your first job and that's in Java I'm not entirely sure how that some Python but I must say that I think they are also really important in Python and that's why I'm giving this talk because they not only helped you to solve problems but they also help you to get a more abstract and understanding of your software because when you want to use these design patterns what you have to do is you have to model your software mostly you have to look at how your components interact and when you model your software with UML for example then you really see how how your system looks like how it interacts and that's I think if you train that a lot it really it really gives you like it helps you understand yourself for better and you will also create a better structure there well structure software in even less time because you can just pick up these design patterns implement and implement them in your code and these design patterns are mostly well actually all of them are really well structured so if you implement them well your software immediately becomes better structured and one important thing about this whole thing is that they all actually offer a shared language to communicate so when you sit down with your developer colleague over lunch and you solve the problem with for example the observer pattern and yet a colleague tells you his problem or her problem and you use a thing like well you know what your problem is quite similar to my problem so if you wouldn't know the term observer pattern you then have to go into all these details of what your problem was and how you solved it but since you already know and hopefully your colleague also knows the term observer pattern you can just say well just use the observer pattern and let's say that's all you have to say so it you can apps you can communicate about problem solving on a more abstract level and Genma talk I would like to give you some takeaways one of them is please look at design patterns really only study them forget them again and then look them up whenever you need them because once you read them and I read the book of gang of fours myself and I thought like my hand that's just a solution I needed in my software and you immediately probably start using one of these design patterns and even later on when you count a different problem you just think back well there was something in the book and you just look it up and you have yeah like a very easy solution and you will write better software when you look at design patterns and they will become more and more helpful and even necessary the more senior you become so when you become when you move from junior developer to senior developer or whatever other title you want to use you most likely won't really sit in front of your computer every day all day long and just look at lines of code but you will rather start to decide on the overall structure of your system and the architecture and then when you know the design patterns again your job then becomes easier you will have better software you will structured better and with these last notes I would like to thank you [Applause] okay any questions Sariah what's that in front here yes absolutely the Gang of Four book I'm pretty sure we can find an online version of that one somewhere I bought it myself as a physical one because I like to just pro through it it takes a little bit time but I don't know just put it next to your toilet seat and every nun that lets go through it you and the back yeah yes yes yeah just look at through them there's one in particular that's most important for Python it was developed by a Google developer and it's called the bork design pattern that's for example is one of the new design patterns that are specially for Python so I would just say when when you go through the 23 original ones just have a look at a quick look at the other 17 or 18 okay but next to the camera you had a question you can yeah and I'm sorry can you repeat the question so do you have maybe five or three top design patterns for Python developers no I don't have a list myself but the design patterns in the book actually structured in three groups which are creational behavioral and another group and I would say that it's it's most most interesting to look at the creational ones no sorry the structural structural design patterns that's just a list of around seven eight design patterns in the book if you for me personally they were most valuable in in Python development yeah I want to comment on the Gang of Four book yes it's actually not very easy to read especially the first time there is a much easier book that the first time you pick it up you can kind of think it's silly it's the head first serious and its head first design patterns yeah and there is also a very good web page which is called Java desh design – pattern yes calm and it's in very graphical forms they show you all the patterns and there are many many more than the both of the books list mm-hmm thank you very much actually that brings me to the very last slide is some references you will find this then later in the in the slide and what you said yes the Gang of Four book it's a little bit academic so if you want to just have a hands-on approach they had first book for design patterns is better and also you have some online link so the source making website with the design patterns is really well really well written they have really easy and understandable examples and they also lead lead you through all the design patterns and there are two more links that I will include here so on the read the ducks is also a good explanation there's one github repository that just implemented all of the patterns once and code so you can just look at it in code form no more questions well then thank you very much

Tags: , ,

11 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *