The Observer Design Pattern - Classes Creation

Paulo Dichone | Android, Java, Flutter Developer and Teacher
A free video tutorial from Paulo Dichone | Android, Java, Flutter Developer and Teacher
Software Engineer - Android, Java, Flutter, Dart and Teacher
4.4 instructor rating • 13 courses • 114,792 students

Learn more from the full course

Java Design Patterns: The Complete Masterclass - 21 Patterns

Master Java Design Patterns and Write Robust and Extensible Software - MVC, Observable, Singleton, Facade, Builder...

09:50:47 of on-demand video • Updated February 2020

  • Master Java Design Patterns
  • Apply Design Patterns to Real-World Projects
  • Build Software that's Robust and Flexible Using Java Design Patterns
  • Solve common Software architecture problems with Java Design Patterns
English [Auto] All right let's go ahead and take a look at this diagram UML diagram here that I put together that shows this overall view of the observer pattern and design pattern. So as we discussed earlier the observer design pattern has the subject or the provider and has the observer so all of the classes that are there will be notified when things change. The first thing we do obviously is we create the subject interface. Now the subject interface is the provider will have a way for registering objects observer objects and registering them notifying and updating them. And at the bottom here what do we did we created an email topic in this case here what I'm doing is I am emulating an email subscriber situation where there is an email provider you subscribe to this email list and each email list could have different topics. Right. So I created here a topic email topic that way we can actually Similac. That way we can actually point to two different topics if we wish to do so. So lets say we could even have topics on Star Wars or Star Trek or Harry Potter or programming or whatever just whatever whatever else we may need. They don't ask this email topic class. You have a class inherits directly. Our subject interface because he wants to be. It needs to be able to know which observer is being connected and what to do with all that information. But he also noticed at the bottom will have the post message because it will know how to post a message from this topic so that all or everybody else can actually receive everybody else. In this case who is subscribed to our email topic at and we have our observer interface at the top left which has all the update and the said subject. But he also noticed this very important common theme throughout our studies here. All of these patterns all these Design Patterns rely heavily on object oriented programming topics such as polymorphism and compositions and so forth. And of course inheritance being the main one. So with that said he noticed there is a lot of composition happening so were passing we are referencing objects inside of other objects. In this case here in our observer notice we are setting the subject we're passing the actual subject subject class or interface in our set subject that way we have that POINAR waited for us to use. And at the bottom we have the email topic subscribers so this is one of the subscribers which will then make sure that we we update it because it's also directly inheriting from our observer it will inherit from our interface observer which means we will take the update method subject said subject. And of course we will also add our own e-mail subscriber constructor to construct an email topic subscriber. This is why we are going to be putting together so we can actually see how it all works. OK let's get to it. I've already created a empty and empty project so go ahead and do that if you wish to do so. And I'm going to go ahead inside Resorcin when I created another package here. I'm just going to call this interface is probably better and call than say model such is probably less refactored is just call this interface things like that. All right. So what are you going to do here. The first thing I'm going to create is this subject. So I'm going to go ahead and put together our subject interface here. This is our provider right. This is the satellite. If we go back to what I've shown you in the previous video example in the previous video game create that so this will be responsible for registering and registering notifying. And of course get updates could do that. So right click on interfaces and say new Java class and want to call this object. This is going to be of course interface and it's going to be to have a few methods. The first method is going to be IBM's a public voice to register and get a pass observer right. We haven't created it yet. It's going to have an error there that's fine the public for it. And register also passed the observer. Wow. A public voyde notify servers and pass anything. And then I'm going to create a get update method. This method is going to return an object. So I'm just going to say public object type such generic object could be any type of object. Get update and then pass of course or observe again. All right so what I'm going to do now is just get rid of this I'm going to go ahead and create an observer interface. Right click here. You have a class called Sorber and it's going to be an interface Sache an interface it's going to be very simple and say public void date and then public for said subject and to set a subject we're going to pass this subject class which we created earlier here. Sub subject. OK. So we've now created these two. So have the observer and we have the subject. Now we're going to go ahead and create concrete classes modeled classes here. Email topic and e-mail topic subscriber. Not e-mail topic of course. See here it will implement our subject because we want to make sure we take advantage of all of the methods that comes with our interface. Let's start with our e-mail topic. All right I'm going to go inside of a model here. Say a new job class and let's call this e-mail topic. Now this is going to implement our subject class. And for that to work we have to of course implement certain methods going a select all of them and there we go. And up here I'm going to shade or create a few instance variables. First one was a private create a list. This is going to be a list of observers and I'm going to call these observers. And then when I say private I'm going to create a strange call this message. Right. Because we have here remember we are going to create this message. There is very good and I'm going to go ahead and let's go ahead and create a constructor let's just initialize our observer list just like this. Right. It's really for us to use. And at the bottom we're going to go ahead and say look for aid we're going to call this post a message. So what we're going to do we can pass our strong message here. Just say I'm sorry we're going to say as out and going to say the message seems to be a string the same message posted to back in going to past message. And here I'm going to go say this that message item in an asset to our message search. And I'm going to call it not a fight. Notify observers and we are going to set up our notify observers in all these override methods in a second here. So what's going to happen here is every time we post a message what are going to do we're going to set of course the message it's unbearable to whatever message we're passing and we're going to make sure that we called inotify observers because this whole idea we want to notify all the other observers around that hey something has changed something has changed that means you have to update as well. OK. Now let's go ahead and sort of register and observers. So what we're going to do here is just make things a little bit clearer. I'm going to just say if observer is not you know always know if it's all going to just say throw you know point or exception and we're going to pass a whole object sir for such. Just in case. And then I'm going to say if observer list that contains an observer of this type that we're passing inside of our observers list does not have this current observer here. We want to make sure that we are not duplicating things. If that's the case what are we going to do is we are just going to go there and say observers we're going to add this to the list and going to add our observer such. That's all. So we're just registering our observer to register our observer. Well we're just going to go and say observers list that remove and remove our observer. It's like that to notify our observers. Well we know that it's going to be a list of observers so we have to look through that list and then do the notification or the update. I'm going to say for observer observer observers observers are at least so inside here I'm going to see observer dot date that's all. So it's not if I am going to loop through all of the our observer list which will and then all of these different observers and I'm going to just update all of them. Now the get update here what we're going to do is just going to go ahead and say this then just return the message that has been updated. And that's it. So we are now done with our e-mail topic notices e-mail topic again implements our subject. That's why we are able to then override all these methods. So the next video we're going to go ahead and put together finalize and put together other classes namely will be our e-mail topic subscriber. And then we're going to create a test class. That way we can test it and see our observer design pattern working perfect. I'll see you next.