Today you'll learn how at runtime, you can enhance the behavior or existing objects using the decorator pattern.
In this episode, we'll discuss the advantages of decorators over inheritance and look at two examples from real-life - implementing a network communication protocol and enriching user interface components.
Episode 8 - Decorator Pattern
00:00:02 - 00:05:04
00:05:04 - 00:10:15
What you can do. Just find your primitive abstraction button. It is already there. You can create a borough which accepts an instance of A. Let's say you why we or something which happens to be implemented by the button and a border around. You Have Igloo Inc you. I wish it as well which is just another class which accepts e you. I wish it at this glowing effect around it. It is very important for the glowing. Ui widget and for the border. You I wish it for both of them to implement the Coen obstruction caught you eye widget. Because this way you'll be able to compose things around you'll be able to create a glowing button. Just by reading instance of the glowing and passing a bottom in its constructor and dates are on. You'll be able to take this and pass it to the constructor of the twitch it and this way you're going to have a border glowing button. This is an extremely convenient design partner in user interfaces. It can see you can use these very lightweight classes which later on you can compose and you'd more sophisticated solutions and you compose metron time. Which is fascinating. You don't have this explosion of different classes and these complicated inheritance chance so the decorator resent is a fascinating pattern to go. Actually this reminds me the first. The first time I heard about design Padre was longtime ago. That was maybe over ten years ago. I was first year in college and We were able to implement a new data. Structured absolutely nowadays directors class so our university professor wanted us to implement a billion array where we were keeping the individual boolean values instead of in a bully rain simplest. Plus where each individual invalid takes bite. He wanted us to implement this data structure by using individual bits. Because well the Valerie's just true or false and we can represent this with a single bit which is either zero one. We went through there and implemented actually implemented for us and we're just following along but we ended up having this strategy which he later referred to the property potter and design patterns. Well given also that he said this in English and we were in a Bulgarian University. We work quite confused so after two showed that we don't know design patterns. Are you said like Har? You like Ori- force hearing university how you don't know who designed partners are we're already our first year but that is a really excellent motivation for us to go ahead by the Patterns Book and Study Oldies Difference Call Slips in order to help us improve the software that who were building right. This happened to be an extremely essential Zampa other for me as well. A couple of months ago Corsair acquired a startup that I co-founded cold rammed calm and incentive in the system I use the decorator example a couple of times and given that I rewrote the product because of different requirements. A Few Times. I had to implement this design pattern properties five times or so now. I use this for something else I do and use it for user interface. I took advantage of that in order to build a network protocol for communication between the front and the back end in Ram dot com. We want to have a real time updates in a lot of different places. It was pretty much a traditional single page application where we had our framework on the front and we have a back end and we want to get updates between individuals students because education platform was the guy joints or whilst Stephen Jones. We want to get an E. We wanted to immediately notify the teacher. What would it was ending up implementing a web sockets three? So when you win. A new student joins we were sending push notifications to the guides. Wear to the students information. We ended up multiplexing. There's like we'll be placing a lot of different messages over this web sockets three and we ended up sending Mike some screen shots. We ended up sending some business messages. Like business details for example we implemented chat over this communication protocol. We were sending goes well some system messages and so on and so forth originally we just created the simple obstruction. I believe we call the channel or something on these lines this channel and just knew how to send and receive messages. We had an implementation of this channel obstruction. This channel was just an interface. We have a website channel which was accepting a call it which knows how to decode encode individual website messages which were in binary format because we were really aiming for high efficiency and very low bandwidth usage and from home.
00:10:15 - 00:13:08
We were just able to encode individual messages at the user was passing and right after that we were able to decode them and send them over to website extreme right so far so good but at some point we wanted to implement an abstraction which was able to sent a message and after receive response of this message brought we could have done was obviously implement actually inherited from swept socket message bus or however we call it and try. It's after implement this request response from genetic but imagine if we wanted to change the communication channel that we're using internally. We wanted to switch from websites to web. Rtc well in this case will have to implement yet another abstraction and who have to complicate our inheritance. Chang changed dramatically without actually adding value. Pretty much had to duplicate the functionaries that we had in our web sockets request response abstraction to our web RTC request response abstraction himself. So we're just create a requests pulse message channel or request Response Channel at remember. How exactly we call it. This channel was accepting a channel in its constructor and it will just adding this request response logic basically every message that we were sending. We were assigning to eat a UI android after that. The client on the other side was responding with a mass. Which with the exact same you. Id So we're keeping a hash with the messages that we sent and their corresponding hugh ideas. That was pretty much everything and when we receive a message which specific you I did. That was already in our map. We're just notifying the client that well. Here's here's the response that you were waiting for. That's where we used the creator and of course we use decorator on many other times where we want to attach some extra features to our. Ui Abstractions wants to talk about this design pattern not only because it is very essential indicate may look in it can may look your source code to more elegance and help you just kill just butte rich user interfaces or you'd reach back end systems by just composing. See what you already have. It is also very essential for the understanding of the aspect oriented programming. This is a paradigm that we're going to discuss sometime in the future. I hope you enjoyed this episode. I'm going to add links to the gang of four books together with Provia decorator example on the page associated with episodes. Thank you talk to you next time. Learn about the episodes. You can follow me on twitter at an get you. The list of horses and recordings is available at PODCAST DOT com. Get YOU DOT com. Thanks for listening.