The Observer Design Pattern is a behavioral design pattern that enables one object (the publisher) to automatically notify multiple dependent objects (subscribers) whenever its state changes. This pattern is commonly used in systems like YouTube notifications, where channel owners (publishers) can publish content and all subscribers (followers) receive automatic notifications. The pattern consists of a Publisher interface with methods for subscribe, unsubscribe, and notifySubscribers, and a Subscriber interface with a sendNotification method. Concrete implementations like EmailSubscriber and PushSubscriber handle different notification channels. The pattern decouples the publisher from subscribers, allowing the publisher to change state without knowing which subscribers exist, while subscribers can be added or removed dynamically.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Observer Design Pattern | Java Interview Prep Series
Added:Heat. Heat.
Hello.
Hi, Saty. Welcome to the chat. Hi Grace.
Welcome to the chat. Hi Dia. Welcome to the chat.
I see you guys are already here.
Okay.
Just when I try to start the stream, uh the internet was very slow.
So it delayed by a few minutes. Okay.
I hope uh it stays stable after this.
Let's uh begin. Fine. So those who joined yesterday, hi the again in the vertical stream. So those who joined yesterday uh need to switch to a good provider. Yeah, I need to switch to a good provider.
Okay. So no those who joined yesterday, we did observer design pattern yesterday, right? So we are kind of doing the same thing today. I kind of messed up yesterday. I didn't give proper definitions and all. This is new for me also like the teaching theory is new for me. The whatever we did previously, right? these uh other courses the ReactJS Java stream Java threading they were all programmatic so it was kind of easier but this is a little theoretical so I realized I didn't mention definitions and all so I deleted that video I mean I unlisted that video this will be the official video okay so those who watch the previous video ignore it come to this one directly fine so uh let's start that then and uh we'll see how it goes how much internet is there okay so for those If you do who don't know me, I'm Shibin. I'm a Java developer. I have like around 9 years of experience in the industry. I also do ReactJS. So I am a fullstack developer.
I als I'm also interested in uh Docker DevOps, right? So I do that also. So that's a quick intro and uh you guys saw the past courses already. This these are the past courses that we did so far. One in Spring Boot ReactJS full uh you know full stack. Then there was a Java streams uh playlist where we learned about Java streams API and then the multi-threading Java multi-threading we learned. Okay. So today we are going to look at uh design patterns and this is the first one uh observer design pattern. Okay. And uh if you guys like this content if you are new to the stream uh you know uh this content uh subscribe if you like it. Okay fine. So let's go. So let's start with the definition right. So the definition of design patterns. So design patterns are solutions to problems. Okay. So what solution to what problem? So let me remove this. So they are reusable solution to common software design problems. Okay. So uh to understand this definition, let's look at some examples.
Okay. Me go down. So here are some use cases. Okay. So there is there are some you know each of these five items are different different problems that keep reoccurring in the design world. Okay.
So let's see the first one is a publisher subscriber problem like YouTube notifying all subscribers when a creator uploads a video or uh if an Instagram um content creator posts a new post right then all of those who are following that content creator gets a notification right so that is also a publisher subscriber problem. There's a publisher subscriber. So all these kind of use cases we can solve with the design called as observer. Okay. That's actually what we are looking into today.
Okay. So there is another type of problem where you can have multiple ways to do the same task. Okay. That's the second one like choosing different payment methods at checkout and things like that. Right. So um in these kind of problems you can like have multiple ways to do the same thing. So here we are doing payment using either UPI or card or wallet. Right. So there are many uh different ways. So that pattern is called a strategy pattern. Right? You have to design a strategy u as to should I use this strategy you pay strategy or card strategy wall strategy. Right? So that's called a strategy pattern. Then another example is uh only one shared resource problem like having a single global global printer spooler like you have one you need to have uh only one entity in the whole system right and everyone should contact that single entity. There shouldn't be a duplicated entity. So for example you are storing a configuration of a system like uh shared config properties you are uh storing somewhere. So that is also like a single entity in the whole system. When you do something like that that pattern comes into singleton pattern. Okay. Ramo Ram said hi. Hi Rau. And uh fourth item. So object creation varies based on input.
So for example uh you enter in the input saying I need to create a truck object or like a ship object. You give a command in the input right based on that input it should create the object. So for example in a rest request you give that okay I want a truck object of like size five or something like that and then it should create a truck object. If let's say you take cases of like the usual oops uh inheritance they explain right animal dog extends animal cat extends animal. So if you're uh you know probably buying medicine for your dog so you have to go to the dog class right things like that. So when it comes to that kind of problems we call it a factory method design uh pattern. Okay.
So you see we already saw one four four different types of patterns. Okay. So the last one is add features without changing original code problem like adding toppings to a pizza dynamically.
So uh if you guys have seen the MECD um kiosk right where you can order when you keep adding stuff to the let's say you ordered a hamburger then you say you need extra patty then you say you need extra onion and you say extra ketchup something like that each time you modify the object. So if you see in the core it's an object right? burger object you are adding onion object to it. So it's actually uh adding toppings or like values on top of an existing object right. So that kind of design pattern is called as a decorator design pattern. Okay. So these are all kind of like behavioral uh patterns. Uh we will see more actually we'll go very deep into each of these design patterns.
So today we are looking at observer right. So we'll go back to the definition probably now we'll understand. Okay. So these are the reusable solutions. These design patterns are the reusable solutions to common software engineering like design problems. Okay. Fine. So let's move away from the definition of um design patterns and go to observer pattern.
Where is it?
Yeah, here it is. So let's go to the definition of observer design pattern.
Okay. So observer design pattern uh the observer pattern lets one object automatically notify many dependent objects whenever it changes. Okay. So this one object uh which you know uh changes right it's called as the publisher. Okay. And uh the dependents who are there on that object it's called as subscriber. Okay.
And whenever the publisher changes its state that is when the subscribers get notified. Okay. So the key terms here are publisher and subscriber. So always remember if you see any system where there is some kind of publisher and some uh some kind of subscriber like an event is uh being uh event happens in the system and someone wants to keep watching or like observing that event.
It's an observer design pattern. Okay.
So this happens in many cases. Uh you might want to for example have a sync between two systems. Right. So whenever uh something changes in one system one you want to send send that u state to system two saying that okay I have changed so you also update your state there you know uh so for example again we'll go back to um classic example YouTube right whenever I publish a video all my subscribers get notification right like tutorial sub uh published a new video right so in this case I am the publisher and you guys are the subscribers so literally you are the subscribers refers to the state. Okay.
And we'll see some examples. Okay. So the first example classic YouTube channel and the subscriber in it is as you know channel subscriber, right? So in case of Twitter user, the subscriber is whoever is the follower. The Twitter followers are the subscriber. You know you can follow the followers of a specific user are his respective subscribers. And then in an Instagram account again the Instagram account followers are the subscriber.
Okay fine. So that's all the definition and examples of observer design pattern. Okay. So now let's try to implement it with the uh application. Okay. So I those who joined yesterday's thing uh I made some few changes to the uh question. Where is it?
Ah here it is. Okay. This is the question. [music] I made some changes so that it'll be easier for everyone to understand. Okay. So we are going to design a YouTube notification service.
Okay. And bracket observer pattern because we are going to demonstrate that pattern. And uh the question is design and implement a simple simple command line YouTube notification system that let subscriber subscribe to YouTube channels either in either via email or push. Okay. So subscriber can say key say that uh send me notification via mail or via push. So for those of you who don't understand what push is. So if you have a mobile phone you get notification whenever someone sends right that is called push notification because it's not SMS or email or any other it's not a WhatsApp message or nothing like that right it's actually push notification they are called push notification which you get inside the app.
Okay. So that is the first thing. So subscribers can subscribe to YouTube channels.
Excuse me. And the second uh task that needs to be done is lets the channel owners publish videos. Okay. So any channel owner can be publishing videos.
So videos are posts actually. So there is um this concept of posts in YouTube if you guys are aware of it. Uh you can add poll or post saying like um we are going to start something new. You can put an image and write text in it. It's called posts. Okay. And then the third one is automatically notify all subscribers subscribed to that channel.
Okay. So whenever uh the channel owner posts a new content, it should immediately uh notify all the subscribers. Okay, that's the use case.
Okay, so first thing let's start with the flow diagram. Okay, so YouTube is the system here. Fine. So in the middle of it all, YouTube is a system. So can you guys guess what is on the left side and what what are these two people on the right side?
Okay, left is factorial. Okay, that is correct. So, left is actually the YouTube channel like Factorial and right is us, right? The right side is the um you know subscribers. So there are two types of subscribers, right? The email subscriber and push notification subscribers. Okay. So that's correct.
Fine. So the this is the simple flow diagram. This there is nothing very complicated in this observer pattern.
You just have the subscribers on the I mean the producer the publisher on the left and the subscriber on the right. Okay.
Simple.
Now let's move on to the next item.
Right? We will uh uh check what how the input and output is. Okay. So the input is supposed to be in system.in as in you are supposed to type a command like subscribe to first oral something like that. Okay. So that is the input and output is actually printed like it'll just print a message saying subscribe to first. Okay. It's not actually going to do in a rest or anything like that.
Fine. So this is the example console interaction. So first we'll have a command which says subscribe to uh you know subscribe for email notification.
uh and uh I am uh the my email id is [email protected] and I give the channel name saying fastal okay so when I say like this it should say subscriber added uh for email notification and the subscribers mail id is this whatever it is and then the channel name is fast oil okay that will be the first command and then there is subscribe push so I say that ship's iPhone my iPhone I want to subscribe for push notification to the same fast oil channel I'll get a similar you know output saying it's subscribed And then I will create a publish kind of message where I say that uh for the factorial channel uh publish a message saying this is the video title okay saying observer design pattern there is a video title observer design pattern which I have posted. So just to know what kind of um content it is I say that it's video. Okay. And then um when I publish it whoever has subscribed to the channel right so whoever is the email notifiers I mean email email people they will get message. Similarly the push people also will get message. Okay that's it. And then in the end we uh try to also publish AI videos. You know there is a I just have an arbitrary channel name called a videos and there is some brain video that nobody wants to watch. Uh and I am trying to publish a post into it. Okay.
And simply it says that no there are no subscribers for the channel AI videos.
That is okay. That's it. And then in if you when you hit exit and hit enter it is supposed to uh end the program. Okay.
So I hope this is clear for everyone. If you guys have doubts about you know what this whole thing is just let me know.
Okay. So I'll move on to the uh class diagram next. Okay. So we have uh on a higher overview. Okay. We'll go one by one. I thought [music] I can keep both in the screen. I it's not coming. Fine.
So we have the publisher, right? This guy publisher interface and we'll have one interface for the subscriber, right?
Always the key terms. There are only two entities in this whole system. There's a there's a publisher, there's a subscriber. Okay. So we have a publisher interface and we have a subscriber interface. Okay. So for the publisher interface um [music] can you guys guess what are the functions that will be there in this publisher interface? So if I write an interface for publisher what all functions can this guy execute? Can you guys guess it?
At least one or two maybe.
Publish. Okay.
Subscribe. Okay.
Notify. Okay.
Okay. Those are good guesses so far.
Okay. So, let's move see what it is actually. Okay. So we have subscribe as you guys told there is unsubscribe also.
Yeah dispose that is the unsubscribe part right it does the same thing as [music] uh subscribe and then the notify function right. So notify function just says that um notify to those who are the subscriber right the notify subscribers function. So publish uh that you have mentioned wouldn't be inside this interface at all. So publish is a uh system thing as in um [snorts] the publisher publishes correct. So what you're mentioning is correct. So he's doing the publish operation but what is the thing that you want to do? So for example you can add the publish action here but then the publish action would in a sense uh you know uh kind of be maybe in the video interfaces right or like the content class. So content class will have the publish. So content.publish will then call publisher dot notify subscribers. Right? That would be the case right. So I think that is how it would be. Okay. Fine. So and in the concrete class this I will uh reveal what it is. So first of all we need a map which stores a mapping of uh who are the u you know what the channel name is and then who are the subscribers to it right? So you need to have that mapping which says for this channel for factorial this is the list of subscriber for um let's say PewDiePie who is the like the topmost YouTuber or something he has this many subscribers for uh Mr. based these are the subscribers. Okay, so we have the map which says uh channel to list of subscriber mapping. Okay, it's the data which will have and then it will obviously inherit all the other functions also from the uh interface.
Okay, so uh if you guys are not aware why I put minus and plus uh in class diagrams minus means private variable plus means public variables or functions. So all of this subscribe and subscribe notify subscribers are public functions. This map will be a private variable. Okay, fine. Now, let's move on to the subscriber interface and see uh can you guys guess what you know uh function might be here in the subscriber interface.
By the way, those who are watching the vertical video, you can switch to the full screen video also. The link is there in the description if you guys are interested.
listen. Okay. So, correct. So, subscriber interface listens. So, um the thing is subscribers do listen to you know activities but they don't there is no one invoking the listen right it's actively listening you know you don't need to uh keep calling the listen function in order to listen to it. Okay.
So the answer will be add a listener.
No. So that's the thing here. Uh there is no listener per se. It is always push like the publisher is going to push the notification. Nobody is going to listen.
So the uh you know the subscriber is not going to actively say that I'm going to listen keep listening to the topic. He just subscribed and left. That's it. He can even shut down his phone or like get get off. He needs he not he need not be actively listening. That's the whole concept right? So uh the publishers always push the notification to the subscriber. So when they push the notification or push the state whatever event it is it's actually a send notification. So uh the publisher is going to call send notification and then he'll give like this content title like the video title and the content type. What type of content is it? A video or a post something like that.
Okay. So this will be the thing on subscriber. So the subscriber never needs to be tied to the listener you know the publisher and keep listening to it. It's always uh you know this way sorry. So uh how do I say it's always going to be pushing from here to uh here to the subscriber. So it's going going to be in this direction left to right always right. So there is never going to be a pull. So if you want to say listen, it's going to be a pull, right? You have to pull the notification from publisher.
That's not going to happen. Okay, fine.
So delete this one. And uh uh for the concrete classes, we will just have some few uh you know variables like type and destination ID. So the type is the type of uh subscriber. So this will just hold a variable saying I am an email subscriber. Similarly, the push subscriber will say a variable saying I'm a push subscriber. That's it. Uh it's needed because um whoever is publishing right they might want to know what kind of subscriber this guy is. So for that you have that. Okay. And the destination ID is actually the you know kind of the email id for destination ID. Here it is the email id and uh for destination ID for a push sender it is going to be like the device ID or something right where the notification needs to be sent. Okay.
Hi Syia on the vertical stream. Hello.
Okay. So um if you see there are two more extra functions here in the concrete classes that is uh the send notification gets implemented as it is.
Then the remaining things are whatever their getters for the uh fields. Okay.
Okay. So all diagrams and everything is done. Now we can start coding. Okay. So if you guys have any questions I'll give like a minute. You guys can think and put the question in the chat. Okay. I will uh uh respond to them.
Okay, I see you guys put let's go. Fine.
So, let's start. Okay. So let me go to STS my favorite ID and let's create. So let's start with the interfaces. Okay.
We'll create the publisher and subscriber interfaces first and then we will create the concrete classes for that. Okay. So new class um package I will put it into com.ftorial story the design patterns and in that I'll keep um yeah I'll put it directly in observer right observer design pattern and I'll create publisher okay it's going to be an interface Okay.
Intelligj VS code. No, no, only Eclipse STS. Okay. [laughter] Fine. So, publisher has subscribe, unsubscribe, notify subscribers, right? So, let me write that public void subscribe.
Okay. And what does it take in as input?
It takes the channel uh name and the subscriber itself. Right? So uh I'll just put that here. Channel name and subscriber subscriber. Okay. Uh the subscriber object I mean interface reading created when we create this error result. Okay.
And there will be an unsubscribe function which will also take the same parameters. If you see here unsubscribe also takes the same parameter. channel name and uh subscriber and then notify subscribers. So public void notify subscribers and that will have the channel name, content title and content type. Okay.
So, it'll be string channel name string content title and uh I'll create a um you know enum for the content type. Okay, content type. Fine. So, let's now create the missing items. Anyway, we have to create the subscribers. This one, right? The subscriber interface we need to create next. Let's create that.
create interface subscriber. Okay. And I'll create it in the same package and call it subscriber. Finish. And the subscriber has just one function called send notification. Let me copy this as it is.
Okay. Public void send notification and the title will be string.
content type will be content type itself.
Okay. So this guy has only one function.
I wrote that one function. Fine. So uh let's create again the missing items.
The next missing item is content type.
This will be an enum.
Create enum. Okay. And I'll put it into the enums package. And it's content type. So content type can either be a video or a post, right? Two types of content. In future if you want you can add more also. Okay. And let me import it. I'll import it here also. Command shift O or control shift O and Eclipse.
Okay. Imported. So the interface is ready. Shorts.
I I didn't understand shots. You mean you want to put this as short?
Okay. Content type. Oh, you mean content type shots.
Um, it's not in the question. What you're saying is correct only. There are many different content types, but our question just has two types. Only the video and post is what it's asking. So, we'll stick to it. Okay, we can uh you guys can actually take this as homework like if a shortcom.
So, let's start writing concrete classes. So, first I'll write the YouTube uh class. So YouTube class let me write it here new class and YouTube it's actually going to be YouTube channel actually okay so YouTube is it YouTube channel or YouTube okay it is YouTube itself because it's not the channel that is going to let me think yeah it is going to be channel only correct so YouTube channel because you can subscribe to a channel only cannot subscribe to YouTube itself right Okay.
So, YouTube channel and then click finish.
In here, I'll do implements publisher.
Okay.
Yes. Okay. So, it's showing error because I haven't added the unimplemented methods. Let me add that.
And there are two methods or three methods here, right? Okay. So, let's implement them. So for that first we need this map which says map of channel comma list of subscribers right so let me create a map for that so map of channel [music] will be the channel name it will be a string and then list of subscribers okay and this will be like let's call it the channel map okay fine and the implementations are going to be very simple okay so if you see here whenever someone calls subscribe you just do um channel map dot put and then whatever is the channel name right to that you have to add the subscriber uh you know to that list you have to add the subscriber so first you have to get the list itself so I'll get the previous list right so list of subscriber existing subscribers dot get um channel name. So first I'm getting the existing subscribers for this channel and to that I prefer prefixing interface names with I for a oh that's a nice uh coding practice.
Yeah, I'll also probably try to implement that. Thank you. Okay, and to that existing subscribers I will add this new subscriber who came in. Okay.
And then I'll put this back to um this thing channel map.put. And then you put the new uh list into this.
Correct. Huh? New list into this map.
Okay. So now the new guy will be added.
So there can be only one thing missing here. So let's say there is no this is the first subscriber to the channel.
Okay. Then this list might be empty. So for that what you have to do is there is this function called get or default. So it will either uh get the existing list or if there is no list it will just give you a new array list.
Okay. So if you guys have confusion here also let me know. Okay. So we we either get the list for list of subscriber for that channel or we create a new list if no subscribers exist. Okay. Fine. So in the channel map we put that value and that's it. That is a subscription. Now for unsubscribe, it's going to be the opposite of that. Okay, so what you're going to do is the same thing.
Okay, it's almost the same thing, but then instead of existing subscriber add is going to be existing subscriber remove. Okay, that's it. So that is unsubscribe. Okay, so you get the existing subscribers list and from that you remove this subscriber and that's it. So here could it be possible that um if there is if this if this is a new list right because if it's empty by default it's going to create a new list so uh in the new list if I try to remove an object which doesn't exist will it throw error I don't know let me try to read the removes the first occurrence of the specified object if present if the list does not contain the element it is unchanged okay then fine we don't care right so if the element is not there it is unchanged so we don't So the says remove all subscribers. No, we'll remove only one because for this channel name this subscriber want to unsubscribe. So let's say only you want to unsubscribe from factorial. I shouldn't be unsubscribe everyone else.
Right?
So that's the concept here. Is that clear?
Okay, I'll come back to that. If everybody unsubscribes, remove key. Oh, you mean like that? Um, we can try that, but that's going to get very verbose. The only uh I mean that happens automatically. I guess it wouldn't happen automatically. It'll just be a new array list. It'll be an empty array list for that key. So, it'll just be like uh let's say everybody unsubscribed from first oral. Okay, it'll just be a um first array will be pointing to an empty array list. That's it of size zero.
So what the vi is asking is uh why not just remove this key itself.
Yeah. So so I don't see uh any much benefit from that. But rewriting that code is going to make this very verbose.
So I'll just ignore it. Okay.
Fine. Okay. Okay, then then let's move to the next one. Oops. Uh, notify subscribers. Right. So, for notify subscribers, um, now we are going to do the actual observer design part. Okay.
In the notification, let me minimize all this unwanted stuff or like basic stuff.
Okay. So, for notify subscribers, what we are going to do is uh first of all, I need this code. First of all, get this list. Okay. Of existing subscribers.
I'll get this list of existing subscribers. Then for uh each subscriber in existing subscribers oops I'll do sub dot where is that function send notification function okay and you send the content title and content type see autofilled that's it so oops so what you you guys can understand now right so you get the existing subscribers for that channel and when someone says that you know some content title like a video is posted and that content types video you just send a notification to that subscriber that this has happened.
Okay, so simple so easy. So we have completed the YouTube you know the uh concrete class this concrete class is fully done. Okay, there is nothing else to write here. So if you guys have any questions post it or we'll move on to the next one.
Okay. So, no questions I see. So, let's move on.
Let's now write the uh concrete classes for email subscriber and uh push subscriber. Okay. So, we have to write two implementations, right? Uh so as Diva said let's first of all make this as I publisher okay it'll be easier for everyone. So I publisher and let me refactor. So I'm actually refactoring for those of you. So if you go to right click then refactor then there is this rename option. So I actually use this shortcut uh option shift r or alt shift r on uh windows. Okay. And then you just type the new name here. So I just add an i and hit enter. Okay. Okay. So this will actually rename it wherever it is.
So if I go to YouTube channel now, so you see it says implements publisher.
Okay. Simple tip. Okay. So now for I subscriber I have to write an implementation. So I go here, right click, new class. And the first type of subscriber is mail subscriber.
Okay. Mail subscriber. And he implements the subscriber interface. Right. Okay.
Hey, this is wrong. Where is our subscribe? Subscribe.
Hey, why didn't our interface appear?
Didn't I make it a interface? Oh, it's I subscriber.
Sorry.
STS attend and zero. [laughter] You will you will come to STS when you know the benefits of it. Okay. So, yeah, I have added I subscriber as an interface.
Okay. And okay, let me call him mail subscriber.
Okay. Okay. So for mail subscriber, let me What?
So when you send a notification to a mail subscriber, what all should happen?
Okay. So let me come back here and see the actual question. So when I send a mail, you know, notification to a male subscriber, it should show this many stuff, right? Let me copy that line.
I'll keep it here for reference. Okay. So the first thing it shows Satya also. Hey so many haters for ST is wrong. Okay. So huh for uh notification mail notification. The first thing it shows is the type of notification right SMS.
Then it is the mail id of the subscriber itself. And then it should say like published that's a static word. And then the type content type is this. And then this is the video name. Okay. So if I make it syntax wise this is going to be um what is this subscribe type. Okay. And then you have um destination ID. And then you have just the word published.
And then you have content type. Okay.
And then the actual content title. Fine.
Okay. So now let's try to build it. So um this content type and content title we are getting from the guy who is from the publisher. We are actually essentially getting that value from the publisher. Right? So let's so the remaining stuff right the you know subscriber type and this destination id this will be stored within the mail subscriber because this uh property of that guy itself like what type of subscriber I am and what is my mail id right those two u details are my own right so it it should be stored here only so I'll create uh variables for that right so even if you see here here right so for email subscriber the type and destination are private variables inside the email subscriber itself. Right? Okay, makes sense. Cool.
So, private string uh what is that? Uh so I'll create a subscriber type as an enum. Okay. And type okay and one more is the string which is the destination ID. Okay. The destination ID in this case is the email id.
Okay. And let me create this enum. So create enum and put it in the enum's package.
Okay. Finish.
And there are two types of subscribers, right? One is mail guy and another guy is push guy. Okay. Two types. Fine. So now this is also done. Okay. So let me make them immutable. So always make your variables immutable. Good coding practice. Okay. So how do you make something immutable? Just mark it as final. Final. Okay. So what is immutable means? Immutable means unchangeable.
Nobody else can change it. Right? So if you mark it as final, nobody else can change it. It can be uh injected only when you create the variable. Right? So when you create this class, you add values to it. After that you never change it. Now how do you const work in Java? I'm sorry. I am not aware of it. Okay. So let's go with basic Java. We'll come to con maybe when we learn JDK 21. Okay. Okay. So create generate con constructor using fields and I have selected all the fields. If I go down generate. Okay. So I actually made a um constructor which which people can send in the subscriber type and destination ID. Okay. No. Okay. So Java doesn't have it. So Java has this where keyword. I haven't uh studied it very much. Maybe when I come to that I'll understand that also like const. So const is there in JavaScript correct?
But not in Java apparently. Okay fine. So let's move to this and uh let me print it. So I'll minimize this and what should we print this out and it should print. First of all I'll just uh copy this as it is. Okay.
Okay. And what is this email? This email is going to be the subscriber type. Okay. And then this [email protected] is the Oops. By the way, this is not my mail ID. Please don't spam someone thinking that this is me. Okay. Destination ID.
And then published is static. And then this video is actually the content type, right? Content type.
And then this observer design pattern is the title of the video, right? So it is going to be the content title.
Okay, that's it. So with this mail reveal cup, [laughter] you can see in my LinkedIn it's already there. Okay, so this is done. So the main thing with the subscriber, what do you say? U publisher subscriber is done. So we actually wrote in YouTube channel function that send notification whenever someone creates an item right. So if I see the implementation of it okay so if I go to the open implementation you see there is send notification and whenever uh a new video is posted it'll automatically send notification. Okay we'll test this and all. So for all current or future subscriber type destination ID is email forever. Uh no that's why I'm marked marked as destination ID. So for example there might be a person who is subscribed for push notification he might not have an email id it might be a device ID right like a device code or something so if there is an SMS uh what do you say subscriber so as you mentioned it will be a phone number for them if they subscribe through SMS notifications so it's YouTube doesn't support it I don't think nobody gets SMS when your uh favorite YouTuber uh creates content right so that's that uh someone asks Sir, I encountered a problem in VS Code. Can you please help me? Sure. Uh, you can DM me. There is the discord uh channel of this I mean discord link of this channel. You can go there and u uh post your question. I can help you. Okay. Thank you. So, uh if you see we have completed the send notification part. Now we'll look into the get type and get destination that is just getters. Okay. So, I'll just um create getters. So go to source and there is generate getters and setters.
So I'll just have uh select getters.
Okay, generate fine. So with that we finished those two also. Get subscriber type and get destination ID. Right. So those are these two functions. The name is little changing but that's essentially what it is. Now with this same thing we can copy paste and create the um push subscriber.
Okay. So I'll just copy this guy mail subscriber paste it here and when you paste it ask what's the new class name I'll just call it post what is it push subscriber right okay now let me go to push subscriber so what all changes here uh let me word so here oh I just realized one thing so you cannot there is no point in um injecting the subscriber type here, right? Because for a push the the value should always return, you know, like this dot push, right? It's always going to return that the subscriber type is push, right? There is no point injecting it here. It's it became redundant because the push subscriber will always be of that, right? Type, right? So, I'll remove this and I'll remove it from the constructor also. I don't know if Diva that is what you asked initially uh asking like for all current or future subscriber type destination is oh no that is not what you asked okay okay so I'll save this uh uh there is this right so I'll just call this function here okay subscriber type and I'll do the same thing here also in mail this subscriber type is not needed Okay.
And this is also not needed.
Okay. And whenever this is needed, I'll just call this function.
And this function will always return subscriber type dot email. Oops. Mail.
Is it mail?
Okay. So I think push subscriber has nothing else extra to be done.
Everything else is the same. Exact.
Okay.
Okay. So with this we have built the whole architecture. Now let's try to actually invoke the function. Okay. I will create a main function. [music] Uh so if you where is main? Oh sorry class uh main I was looking for main there.
Okay. So if you see the actual question asks that where is it? Uh I need to you know actually pass commands like this like subscribe email. I have to actually type it in the stdn uh but for now we'll just do basic uh invocation direct invocation and see if it is working. Uh later we will try to uh you know emulate these commands. Okay. So the first command is subscribe email. Right. So let me create a main function.
Okay. In this main function, uh the first thing is subscribe email and then so I need a subscriber, right? So I subscriber uh sub sorry I need a publisher to subscribe.
So I publisher uh okay uh YouTube equals YouTube channel. I should actually say new YouTube channel. New YouTube channel.
Okay.
Why is it not it's not formatting properly. Oh, now it is. Okay. Now, first thing I'll do is YouTube dot subscribe and then I have to pass in the subscriber actually. Right.
So, let me create a subscriber for this guy. So, I subscriber and I will call this guy shibbin. Right.
Shibin equals to new uh what kind of subscriber? Mail subscriber and uh his email id is [email protected].
Okay, fine. So now what are we going to do?
We're going to subscribe shibbin to this channel called astorial.
Okay, is this code clear?
Someone says OMG design patterns. Hi I'm glad [music] you like it. STS. Yeah, STS. Please stop blaming STS.
What did it do to you? Okay, so we created a channel and then to it we subscribe this guy. Okay, then what is next?
We have to subscribe Shibin's iPhone to um faster again. Okay, so let me comment this out.
Okay. And let's create a subscriber.
Uh I'll call this ship mail. Okay.
And I'll create one more subscriber for ship um push. Okay. And this will be a push subscriber.
Okay. And then I'll subscribe that guy also here.
If I'm going too fast, just let me know.
Okay. I'm just copy pasting stuff so I'm doing it fast.
Okay, so I created uh you know subscribe to faster list shin push. Okay, two people. Satya says you should do a video comparing STS and intellig to show the power of STS. Yes.
Do you GitHub the code at the end? Yes, I'll be pushing it to GitHub. The GitHub link is there in the video description itself. The GitHub repo is actually empty now after completing this push.
Okay, fine. So, uh that is done. So, we have just subscribed now, right? What is the next thing we have to do? We have to uh publish a video, right? So, I'll copy this.
So, who has to publish video? YouTube has to publish a video, right? So, YouTube is publishing [music] a Oops.
What is the function called?
Ah, notify subscribers, right? So, it is going to publish a video saying channel name. Channel name is Ptorial.
And what is the other one?
content title and content type. Okay, so the content title is going to be this oops and the content type is going to be content type dot video. Okay, so publish is done. Then there is one more publish for brain rot. Okay. So if I call this I'll just copy the same thing and to factorial we are going to say that oh sorry not fast it's a different channel right the channel name is a videos and we are going to publish a video called brain rot part one and it is going to be a type of post.
Okay so with this we have kind of emulated everything programmatically.
Satyam Singh says hi. Hi Satyam welcome to the chat. Okay so we completed everything right? 1 2 3 where is it? 1 2 3 4. Four commands are there. We completed all the four commands. Let's run it and see. Okay. So right click run as Java application and it failed miserably. What happened? Null pointer exception map.get orde default object object. This channel map is null. Very good. Can you guys guess what happened?
This is the error message. Where is it?
Did you say STS or brain? No. [laughter] Just concentrate on the lesson. Don't stop trolling STS. STS is good.
Initialize. Yep. Simple. You have to initialize this. I didn't initialize this. So, new hashmap.
Okay. So, I'll initialize this as a hashmap and let's run it again. Okay.
So, we got two messages saying uh [email protected] published video this and I sent a mail and for push also I put the mail id as the push thing. I should have actually changed it to iPhone [music] or something, right? What was it? Huh? Shin iPhone. It's here. Let me change it. So the push subscribers device ID is the you know ID is the device ID itself. So I got these two.
Okay. Shib iPhone published video u observer design pattern. Okay. You should add a name variable too. You should add a name variable too. So name of uh what DA?
Okay. So one more thing while DA replies if you see here uh it is actually sub showing output whenever subscribers are added also. Right. So uh also similarly if there are no subscriber it kind of shows an error message also. So let's implement this also. So where are we adding subscribers? We are adding it uh to the channel I mean to YouTube right?
So I'll go there and whenever a subscriber is added I'll just put this message out.
Oops.
And it's supposed to say subscriber added. And then it says email.
Huh? So where do I get this email from?
I get it from.
So this is why we expose the get type, right? So subscriber itself. So subscriber.get um Oh, I didn't uh expose it in our interface. Is it? Yeah, our interface is supposed to uh expose these two functions.
I didn't write it here. Yeah, our interface is supposed to expose these two. Okay, that was a mistake. Let me rectify it.
Okay.
Okay. The arrows went crazy again. Okay. Anyway, so it's the interface is also supposed to expose it.
Then only the uh callers can call that function, right? So the two functions are where is it?
These two um this get subscriber type is one in I subscriber and the other one is get destination ID. Okay.
Okay. So these things if when you guys code you'll uh know it understand I mean understand it automatically. So you should add a name variable to YT channel. It would be useful here.
I still don't understand. You mean like uh in here um in the channel map here like fast hyphen yt channel something like that You mean add a name to the channel but okay the thing here is this entity right this class doesn't represent just a channel because channels are inside the channel map so I shouldn't have named it as YouTube channel I should have probably named it as just YouTube this is a system YouTube itself self. Okay, so let me fix that. One second.
Get uh subscriber type here, right? And let me add this stuff and then fix that.
Oops.
Okay. And the channel name.
Fine. That message is done. Uh yes then okay right so I I I understand your confusion so this should not be a channel itself the channels are stored in this map right so this should be the whole YouTube system itself okay this is YouTube itself I just refactored it okay so everywhere you'll see so maybe now the main class will also make sense so here >> [music] >> um YouTube is the publisher itself right so YouTube system and then I'm saying YouTube and then calling the subscribe function on YouTube for a specific channel okay so at At some point we would want faster lesser YT channel instead of a string.
You mean an entity? Okay. Okay. Okay. So like maybe you need the subscriber count for real or like the subscriber list for fast. That's why you're saying you need an entity for first oil itself. Right. I think I understand it. Right. Right.
Right. So I understand what both of you guys are saying. So uh we should probably store that in a different uh way here itself. we need to uh you know kind of design a different uh what do you say data structure here not a string to list of subscriber it then it'll then become list of channels itself right it'll become a flat one layer architecture so that channel will again inside have a list of subscribers right make sense okay got it fine maybe we'll do that low so I'll just mark it as to do and then homework uh try to do a list of channels and store the uh subscribers inside each channel. Okay.
Yes. Makes sense. Fine. Okay. So uh I wrote the system out printent for the success message whenever a subscriber is added. Okay. So let's uh show a error message when there are no subscribers as well. Okay. So for that uh it'll be in this notify subscriber right. So uh if existing subscriber if this list becomes empty if uh existing subscribers equals null. Okay. So, I'm going to do a little modification here. It'll print this. Oops.
Okay. And it'll be an error. System error. Len and I'll return. Which means if this is null, I'll not do this uh you know iterating through the list and seeing subscribing to each people. I mean sending notification to each people. Instead, I'll return. But now according to this code it will never become null because I'm always you know if it is null I'm getting a default of an empty array list. So I'll remove that logic.
Okay fine. So in in according to this current code now uh if there are no subscribers for a channel this will become null. This existing subscriber variable will become null. And once that is null it will print the error and return. It won't do anything. No notification will be sent after that.
Okay fine. Let me run the function again. I mean program again and you can see we got all the um expected messages.
So first excuse me first it said subscriber added subscriber added two subscribers got added and uh when uh video was posted two messages were sent notification was sent to these people and then we tried to uh post notification for this AI video channel but there are no subscribers so so it just said an error.
Okay. Okay. So with this part one is done. We try to uh you know uh do everything using Java classes. So this is done. Next we will try to uh write like a command line processor. Okay.
We'll take a break. After that we'll come and write a command line processor where it will read this input and then execute the functions accordingly.
Right? So it'll read it from stdin and u execute it. Okay. So homework is to switch to intelligent. [laughter] No, stay in STS and uh yeah. Okay, fine.
So, if you guys have questions, uh just a design pattern a day keeps many bugs away. True. [laughter] So, you guys uh read uh you know either take a break or like watch back. If you guys had any questions noted down or something, just post it in the chat.
After the break is done, I'll come back and uh explain. Okay. So, I'll start a 10-minute timer for the break. Let's meet after the break. Right. Thank you guys. See you.
Thank you for saying nice people.
Hello.
Okay. So, welcome back.
Uh, Wagaband asks, do you post your schedule somewhere? So, I don't post it anywhere, but um [music] I have been streaming for quite some time. I haven't been able to fix the date, you know, time yet, but then um um I'm thinking to do it 10 p.m. every day now, every weekday at least, because I come from office a bit late. So 10 p.m.
all weekdays. Okay, we can try. So if at all there's a stream, it will be on 10 p.m. at a week on a weekday. Okay, so if you subscribe and hit the bell, you will get the uh notification in our schedule.
Okay, so is this stream going to end again? No, it's not. Okay, the stream will run. I hope you guys can see me.
Yeah, fine. Okay, then let's uh uh why is this so weird?
Hello. Huh? Okay, so let's uh continue.
Okay, if you guys have any other questions, just let me know.
Okay, so you guys uh understand what we are going to do next. So, next thing we have to do is where is it? Uh we are going to make it so that we are literally going to read these commands from the command line and then execute them. Okay. So how are we going to do that? Um so if you think about it, these are commands, right? What what else is it? So we will have we will uh design a so this is kind of out of scope for the observer pattern itself. This is just to solve the problem, right? So if you guys came in for the observer pattern, you might as well leave because observer pattern is done. What we are going to do now is just convert this into whatever the question is asking. It's a problem statement that they give right that to you know execute any kind of commands from the command line. So that's what we are going to do. Okay. So what I'll design is I'll create a command uh kind of interface where I can pass this command and then say execute and then it will uh execute the command for me.
Right? So it'll figure out what uh interface to call and stuff like that and it will uh execute it. Okay? So that's what I'm going to do now. So uh first thing I'll design is an interface.
Let me call an interface and I'll call it I command. Okay. And I'll put it into uh some cmd processor. Okay. I'll create a new package for it and I'll call it cmd processor. Okay. In here there will be just one function called execute. Okay.
So whenever someone calls execute that command is supposed to execute. Now how will I pass the command in here? In the initialization when I initialize the uh class I will pass the um command in here. Okay. So that's my idea. So now let's u create another new class. Uh and this we'll call as so there are two types of commands. If you see here there's a subscribe kind of command, there's a publish kind of comment.
Right? So we'll create two classes for each. We'll create a subscribe command class and we'll create a sub uh publish command class. Okay? So subscribe command and it uh implements I command. Okay.
Okay. Finish. And let me write the code here.
Okay. So subscribe command takes the command itself. So string command and it will be private final string command.
And I will I I'll make it cmd because we have to keep calling this function um variable multiple times. Right? So I'll just call it cmd to make it simple. And in source there is this thing called generate constructor using fields. So I'll select all the fields and click generate. Okay. So anyone can in invoke subscribe command and then pass the command. It'll get set here. Now after this is set, what are the other things I need to do from the subscribe command?
Let me copy this again so that I not switch pages again and again. So this is how a subscribe command looks like.
Right? So there are four entities here.
First is the word subscribe itself. Then there is this word called email and then there is the actual email id and email ID or device ID anything. Okay. And then the channel name. Okay. So let me split it and store it into a separate variable. So I'll have a private uh string e or like final string email id. Okay. And then I'll have a private.
Oh. Oh, this is the email is the first thing, right? So that email is actually um subscriber type. I'll call it subscriber type.
And the second thing is email id. Right?
So final string the email id we named it as destination id right that is done and the last thing is uh the channel name so private final string channel name okay we need not store the subscribe because it's automatically if someone is calling this um subscribe command class they already know that we are going to subscribe right so there's no point storing this first uh command itself. Okay. Uh fine.
Now let's parse these items. Uh first thing is we have to do normal string manipulation like string splitting.
Okay. And we'll split this into an array of four strings by space because they are space separated, right? So we'll do that. First thing is string commands.
Okay. String array of commands. Actually call cmd do.tsplit. split with a space. Okay. So now I I have gotten four commands. Okay. So the first command is subscriber type right. So if subs uh commands of one okay because commands of zero is subscribe. We don't care about this. We need to look into this second thing. Right? So it is zero index. Right? So it will be uh this is zero. This is one. This is two and this is three. Okay. So, commands of one dot equals um email. Okay.
I'll just uh make this thing. Who is that guy? Subscriber type, right? This dot subscriber type equals subscriber type dot email. Oops. Was it mail? Okay.
Mail.
Fine. So similarly if it is not the case I'll just make it push or like huh push equals I'd rather copy all of it and change this to push. Okay so we have set the subscriber type. Now what is the next thing? Destination ID right? So destination ID is at index 2. Let's simply do uh this dot [music] destination ID equals commands of index 2. Then email id set. What is next? Channel name. Right. So this dot channel name equals commands of three. Okay. Fine. So with this we have initialized all the variables. Switch case. Um yeah you can do switch case but I just made it simple just if it's better to use switch case. Yeah that is correct. Okay fine let's do that switch commands of one. Okay. And then if case is email um what should happen this right this and break if case is um push then I'll do this break.
Okay, I'll remove these lines and default I'll just throw new illegal argument exception. Oops.
Or uh argument exception [music] and say invalid subscribe type and I'll [music] print this. Okay, commands of one whatever that was.
Okay, so I'll just throw an error if people somebody's not inputting any inputting anything other than email or push I'll just print this error. Okay, so with that the subscribe command we have uh deducted I mean reduced everything that we need to execute a subscribe command. Okay, now in the execute part what are we going to do? Uh we are going to first of all um get our channel right? Huh? We need the channel also by the way, right? Uh so I mean the YouTube system, we need the YouTube system. So we have to get that also from the constructor. So private final uh I publisher, right? Publisher publisher.
So I'll say why this is okay. Let me do that.
And this dotpublisher equals publisher.
Okay. Why we need this publisher is because then we can call the uh execute command right. So publisher dot this is subscribe right. So subscribe call is what we are doing here. So subscribe channel name and then we have the subscriber right. So, how are we going to get the subscriber?
I subscriber.
Subscriber.
Okay. Equals uh you need to know what the type is, right? So, what I'll do instead is I'll do it like this.
Um I'll take these two and put it here.
Okay. And once we know the type, we can build the class, right? Um so I'll have the subscriber here.
Private final I subscriber.
Subscriber.
Okay.
And this dot subscriber equals new mail subscriber. Right? Then I can pass in the um email id to him. Where is the email id?
Email id is here. Destination ID. Okay.
And similarly if it was a push kind I can pass the uh push in a push subscriber.
Okay. So now I can use this uh subscriber directly here. Subscriber object directly here. Make sense?
Okay. So I'll go quickly go through what I did just now. Why are there warnings?
Oh, we never use the subscriber type um variable anywhere. Okay, then I'll just remove it. I'll remove this. We don't need this.
Okay, we don't need to store this also anywhere. I see. So, I'll just bring it here.
Okay, so I just removed all the unwanted stuff. Fine. Okay. So, in the end, this is what it is going to be. So publisher we need a publisher right to publish the items and then what are the other variables that we need to store those are these okay so whenever someone sends a subscribe command they send the command what whatever subscribe command is and then they send us the publisher also the publisher is the uh main system which is going to publish the you know u notifications okay so from this command we are going to parse uh the destination ID and channel name and the subscriber type. Okay, so if you see here um the email or like the subscriber type is at argument one and then the destination ID is at argument two and then the uh channel name YouTube channel name is at argument number three. Okay, so that is what we are doing here. We got the destination ID from two, channel name from three and then based on whatever is the command the first item, right? Like subscribe was the command, right? So that was the oh sorry email was the type right. So that we got and then based on that we created a uh new subscriber object. Now whenever someone calls execute we simply say publisher.subscribe because this whole class was created to subscribe. Okay.
Okay. So with this we have uh created a subscribe command processor. Okay. Next what we'll do is we'll create a uh another processor for publish command.
Okay. So let me [music] um create that new class and [music] this is going to be publish command and I will extend our I command interface and finish. Okay, remove this and we'll do similar things here also. Okay. So, what is the first thing we need to have to publish?
So, I'll copy this, paste it here. So, for publish, we need the channel name and the video message and the uh type.
Okay, three items.
Okay. So here also we need the publisher because uh if we are going to publish a video we need to have the publisher also or like publisher notification we need to have that also. So I'll have private final uh I publisher publisher. Okay. So this will be one item. What else did we have here?
Um destination ID channel name subscriber.
Right. So do we need that here? Let's see as we go through go on whatever is needed right we'll keep adding more. So first thing is this dot oops not here where's my constructor public okay so people are supposed to pass this along with the command. So command is mandatory and we'll do this dotpublisher equals publisher. Okay, we have to call super before it. Good good coding practice.
And what is the next thing we have to do? We have to parse these items, right? This whole thing we have to parse. So for parsing this uh it is kind of having very crazy reject patterns and all because you know there is double quote here also there are spaces here. So if I do a normal split what will happen is it'll split like this you know fast oral and then after this it will be a split after this it will be a split then after this again it'll be a split so it becomes like instead of four commands it becomes like six commands or something that is not what we want right so to do that we need to actually do a reject pattern based compilation so I um kind of copied that code I'll just paste it here right because it's just boiler plate to um figure out all that. Okay, so pattern uh matcher and this is the command we got right. So I'll show you what is happening here. So I wrote an uh rejects pattern which is like someone says publish. No, you see it starts with publish and then there is some space one or more space. This small s means space.
Okay, then it says there can be one or more words, right? This capital S means negation of space. Anything other than a space, right? Then again there could be multiple spaces. Then it says there could be a uh double quote. You know something starts with double quote.
Again there could be lot of words.
Uh this is space again right? So after the double code the string comes in and then again there's a space here. So that is the smallest S again. And then again there could be a word. The capital is again a word. Okay. So this is kind of the reject pattern. So I'm not very expert in reject and all. So uh this is what I got built and with that I am doing a pattern matcher and then matcher.find will actually find matches for us. And after that when you do matcher group of 1 2 3 it will actually give us the items. Okay. So uh the items will be this factorial and then the u actual name and this thing. So that will be the uh matcher.123. Okay. Okay. So what we'll do is we need a few items here, right? So we need the topic name and message. So for these we will need variables. Let me create them.
So private final channel name. Okay, it's a string.
And then I need the private final string um content title. Okay. So whatever is the published content it's title I need.
So this channel name is the topic name and content title will be the message.
Okay.
Okay. So that those two are done. Now um this ignore this I'll actually write it.
So this is not notification category.
This will be the uh content type as string. Okay, we'll do the switch case here also. So, switch content type as string.
And if it is a video, uh we will have an enum here. Okay, where is the enum? So content type content type equals new I'm sorry content type oh should be null sorry null initially okay [sighs] and if it's video I'll just set content type equals to content type dot video okay and uh break if it is post I'll make this as content type. Okay. And if it doesn't match either of that case, I'll just throw new illegal argument exception saying um invalid content type and print the content type itself. Okay, content type as string.
Done. So from this we got all of these values. content type uh you know we passed everything we passed the channel name the message and the uh content type okay now when it says execute so what all do we need in the execute function okay so I have the publisher right so publisher dot uh [music] it's this notify subscribers function right and I call this notify subscriber it takes the channel name content title and what is this last argument uh it's the content type itself right so I need to have this content type in my um global variable. Okay, let me do that.
Private final content type content type.
Okay, so this dot content type is that this dot Okay.
Okay. And then we'll pass the content type here.
I hope everything is done. Everything is done. Okay. So, let me recap.
Exit pattern.
Um, I don't know what is exit pattern actually. I think it's a design pattern, right? But I'm not aware of it. Can you elaborate what is exit pattern?
Okay. So, I'll uh explain what this is meanwhile. So, we are now writing a um class which will handle [music] anyone who calls this publish function. Okay.
So the publish function uh gives us the channel name, the title of the video and then the content type. So this could be a title of a video or a post. Okay. And the content type could be either video or post. So we have to store all those variables that are coming in. Okay. And uh I mean we have to create variables to store it. And then uh whenever someone calls a publish command, they are supposed to give the command and the publisher itself. Okay. And uh what we do is we initialize the publisher the local publisher and this pattern matching is done so that uh this weird uh what do you say string is there right with double quotes and all in between that can be handled. Okay. So I'll just write it here.
We are using this pattern matcher to handle the weird quotes and spaces in the um content title of the command. Okay.
Okay. So that is why we did this part.
Okay. And then we are parsing the given input and then finding 1 2 3 all of the values from that. Okay. we got we passed and got all these values and then what we are doing is based on content type we are setting I mean the string content type we are setting the uh content type that needs to be created for the uh notification okay if there is some error we just throw an illegal argument exception saying that the content type is invalid fine and that's it so Satya says question asked to say goodbye when someone says exit your reject does not handle that. Okay. So that is one another command. Okay. So the last command which we want is exit command.
Where is this one? So exit command is also a command type. If you see right.
So what I'll do is I mean we can easily simply do uh you know if we are running a while loop or something we can exit but just because we are using um an interface we will create one for that also. So I'll call it exit command because we have a publish command subscribe command and this will be the exit command. Okay, I command interface.
Okay. And to this interface I need not do anything. If somebody calls exit command.execute, it will simply do system.exit of zero. That's it. Okay. It'll just end the program.
Fine.
Okay. So you are saying your reject doesn't handle that, right? because my reject is parsing this command only.
When the exit command comes, it will becomes a different command at all.
Okay. So when I write it in the main function, you'll understand. Okay. Just just hold on to mains. Okay. Now go to main function. Where is main? Main.
Okay. So let me um cut all this and keep it like backup.
Okay. Okay. So in here, what do we need first?
Uh we have to keep listening for arguments right for that what do we need the scanner oh it is supposed to print goodbye is it yeah good catch so I'll write that also in our exit command this out right so when it exits it's supposed to say goodbye and then exit okay good thank you okay so in main what I'll do is I'll keep listening for commands and what do we need for that we need a scanner scanner in equal to New scanner of system dot in. Okay.
Okay. Uh and in the end we have to do in dot close. Okay. And in between what should we do in between? So I will run a while loop. Okay. While true.
And while true. What I do is Oh.
Uh so I have to put this in a finally block I I'll take care of uh what is say resource leaking later. Okay let's just write the function now. So while true what I'll do is um I'll do string command equals in dot read line oops in dot next line right uh in dot next line.
So I am reading the next line that the user is passing in the S line. Okay. And then what I do is if cmd oops cmd dot starts with what? What is the first type? Subscribe. Okay. So if it starts with subscribe then what I do I create a uh I command.
So I'll have this command line here itself.
I command command uh executor.
Okay.
So I'll do command executor equals new.
What command is this? Subscribe command.
Subscribe command. And I have to pass the command. And I have to pass the publisher also. Okay. So I didn't create the publisher yet. So let me create him as well. So publisher YouTube.
It's I publisher. Okay. Uh equals new YouTube.
Okay.
Okay. So I have to pass these two variables. Okay. So just now we wrote the subscribe command remember. So for subscribe command we have to pass the command and the publisher itself. Fine.
Next. Uh else if cmd dot starts with what is the next type? Publish. So if the command starts with publish then my command executor will become uh publish command. Okay publish command okay and the last is the else case I'll just say if that doesn't happen I'll command executor will be uh my exit command.
Okay and exit command doesn't take any argument. It doesn't need to know about anything. Okay.
And in the end, so once I set my command executor, I just call it. Okay, command executor do.execute.
Okay, and this will keep running in the while loop forever. So unless I type exit or some invalid command or something, it'll keep running and ask asking me for the next next commands.
Okay, so now I think this is pretty much it. We have completed what they have asked. Uh there is in do.lo, right? I'll do that later. Okay, so [laughter] ignore me. Don't do this in production.
But uh just because we are focusing on the code, I'll just ignore it for now.
And let me run the main function. So is there something that has to uh there's no input in the beginning, right? So let me run it. Right click, run as Java application.
Where is it?
So the application is running. It's not showing anything, but you see if you see stop button, it means that it's running.
Okay. And it is waiting for my input. So let me paste this first thing here.
Okay. Paste. Enter. So you see you got a message saying subscriber added. Okay.
Next. Let me take the next command.
Subscribe push and paste it. Enter. So it says subscriber added for push shiban iPhone. Done. Okay. What is the next thing? Uh we have a publish command.
Copy. Paste. Enter. And you see you got two messages. Mail push. uh you know mail got sent and the push got sent to shipping. Okay. So you see you got these two notifications and what is the last command publish AI videos paste enter and it says no subscribers for channel AI videos right so that is the message that you see here as well okay and the last thing is exit let me exit and it says goodbye and you see the stop button is gone which means it's already stopped okay the program exited done soya says but in this case even one invalid in input will crash that correct That is correct. So in this case it will crash the app. So um to handle your case that's actually a nice question. What I'll do is I'll do a small change. So I'll demonstrate what Satya said. Okay.
So instead of uh doing subscriber publish I just write some random word.
Right? So it just says goodbye and exits because uh I my condition is like that right? So if if the command doesn't start with subscribe or publish I'm simply exiting, right? So that shouldn't happen. It's not good. Also what we'll do in instead is um we will have else if and if command dot starts with exit only then do this okay else I'll just print saying sis out invalid command try again okay and then I will continue continue the loop okay fine so this makes sense So now what happens is if I run it and if I go to my console and some type some random it just says invalid command try again.
Okay. If I type something again it just says invalid command try again. Okay. So if I do actual subscribe it is supposed to show subscriber error. Okay. And if I do actual exit now it will exit also.
Cool.
So with this our observer design pattern is complete. Okay. So we took how much time?
1 hour 45 minutes. That's the record. I took I thought it'll take 2 hours. So, if you guys have any questions, post it in the chat and let's wrap it up.
Okay. Yeah, you guys post it in the chat. Meanwhile, I'll uh just do some final cleanups and push this to GitHub.
Okay. The GitHub link is in the video descriptions. You can go and check it out there.
So for the scanner Um, Nice stream, Shin. Good work. Thank you.
Thank you, Satya. Thank you for helping me with the refining the question and stuff.
Okay.
Why is there not getting ignored?
Get in. [laughter] Yeah, get in it. I'm not very good at remembering commands. Okay.
What the Okay, I have pushed the code. You guys can go fetch it from here. Okay, where is it? Here we have uh so we have done only observer. Okay, so we will continue with the remaining uh patterns in the upcoming streams.
Okay, so thank you all. Thank you for joining and staying till now. See you guys in the next stream, probably tomorrow. Okay, bye-bye. Have a good night.
Related Videos

TOP 15 Data compression Interview Questions and Answers 2019 Part-2 | Data compression | Wisdom jobs
wisdomjobs
281 views•2019-06-28

CTS 158: 802.11w Management Frame Protection
ClearToSend
4K views•2019-02-04

NDSS 2019 Send Hardest Problems My Way: Probabilistic Path Prioritization for Hybrid Fuzzing
NDSSSymposium
496 views•2019-04-02

How realistic is Cities: Skylines?
CityBeautiful
159K views•2019-02-14

GUIs & TUIs: Choosing a User Interface for Your Python Project | Real Python Podcast
realpython
2K views•2025-04-04

The OSI Model - Explained by Example
hnasr
225K views•2019-05-12

Cloud Computing - Introduction
elithecomputerguy
98K views•2019-10-07

From Traveler's Dilemma to Dynamic Routing | Demystifying Networking
IITBombayJuly
5K views•2019-08-04
Trending

WOW! Judge TURNS THE TABLES on Trump in His OWN $10B LAWSUIT!!!
MeidasTouch
197K views•2026-07-23

Playstation NO DISC/NO BUY Fight Is Over...
DavidJaffeGames
4K views•2026-07-23

Steam and Xbox Just Dropped The Hammer On PlayStation
OhNoItsAlexx
9K views•2026-07-23

Americans Confused in Australia for 17 Minutes Straight
IWrocker
17K views•2026-07-23