3. Observer Design Pattern Explanation, Walmart Design Interview Question, 2022 | LLD System Design
Вставка
- Опубліковано 9 тра 2022
- Hi, in this video we have covered Observer design patter out of many design pattern which is asked in Low level system design interview.
➡️ Become a Member: / @conceptandcoding
This is the first LLD video where I saw real coding example.. and not just theory. Great work man!! Keep it up !!
Thanks 🙏
@@ConceptandCoding please share notes of videos
@@ConceptandCodingplease can you share github or notes that is the only lacking point
nicely explained Shrayansh. Hope to get more videos on LLD.
Very good explanation with examples, Thanks a lot for such videos.
Happy to be here before this channel blows up. Keep up the quality content.
welcome
Man! You are really killing in teaching the concepts❤️.
Just brilliant, You are giving us true insight into enterprise level coding,, thanks a lot
Thanks
Very crisp and clear explanation.
awesome explanation
Thank you @Shreyansh ! I have never found a better Design pattern video.
thanks
Thanks for the wonderful explanation
Awesome Explanation♥
🎉🎉🎉 perfect explaination
Very nice explaination. In the Observable concrete class, you should first update the stocks value and then call update for the observers. Reason: It might happen that the update method in Observer has something to do with the number of stocks currently available. It will get the old value not the updated one.
Thankyou for this video. The mixture of the question and the explanation pattern together, will help me to remember this kind of scenario well. Thankyou once again!
Thanks 🙏
Again very well explained , And yes it will be nice if you explain Implementation With PropertyChangeListener which is another way to implement this.
Bahoot hi achha explanation
Great one!! I could not understand the part where it was mentioned that we would have had to use "instanceof" if we were not doing constructor injection. Please add some clarity on that part. Some example would be great!!
Great video
bought Gaurav Sen's course but you explain better than him. I saw only 1 video from his playlist and will watch all ur videos. Keep up bro..
As a fresher , Started learning LLD from today. Enjoying the concept .so simple explanation and easy to understand
Thanks bhaiya
Thank you
Hi Shrayansh, wouldn't there be need of initializing many objects if there are many obervable for a single observer...dependency injection in method update will be better right? THen both of them become loosely coupled, and anyway since the method are from interface we can call it using the interface argument......eg could be like 1 user can call notify on multiple items..so in that case it becomes m:n relation.. pls correct me if i am missing something
Simple and brilliant explanation. Particularly good for those who is revising and have some idea
Thanks
One of the best explanation on youtube. Thanks
thanks Ayushi 🙂
Too good.... This level of depth in knowledge covered with such simplicity..... Its phenomenal. Thank you so much and keep the good work up
Thank you
Very informative video! I have a question that suppose if my store has 100 products apart from iPhone. I would have to create 100 concrete classes for it?
Simple and crispy explanation, hats-off to you
Thanks a lot 🙏
For the notify function in observable concrete class, we can call the update of observer and send the current object in parameter. In the observer class, we don't necessarily have to use instance of operator in conditionals, if getData() is there in the observable interface, then we can directly use observable.getData() and it is fine. No need for constructor injection as well
Exactly. This code will be lengthy when observer will observe multiple observable objects. So better to pass observable object through update parameter.
Was thinking the same. Why not pass the object of current concrete class to update instead of interface.
My though process:
Assume you are passing object in update()
Method: update(Object obj)
1) Check if this obj is null or not
2) Check if this is object of Observable or not
3) Then have to perform typeCasting, multiple checks if multiple child of ObservableInterface.
Simply we can't do obj.getData() because obj can be null or of any other type.
Better use constructor injection approach. I might be wrong.
@@ankitgoyal8556 good point, what if we pass null. solution-> you can return without doing anything with just one if condition.
2. Why do we need to check if object is of type Observable or not, if we are expecting a variable of type Observable then how can other type can come. if someone tries this, then code will give compilation error
3. type casting is not needed. we can directly call obj.getData()
According to my understanding, we don't need both the things neither constructor injection nor passing the current object, We are already adding the observer in iphone observable, why we need to differentiate it
Thanks for the video!!
Having both Setdata Getdata methods inside Observable class seem to violate Single responsibility principle. Now this class is responsible for both notifying as well as managing the data.
May be better to seperate the data managing part to seperate interface and take it as input to observable class
Good Video. Thanks man.
great video :)
qq - in the observer class, what of its observing >1 observables. In that case, is using 'instanceof' the only solution ?
Thank You for the video . Can you suggest me which design pattern are helpful from Mobile (Android)App Development Point Of View
Thank you for the detailed video. Very good explaination.
Thanks
Loved the real-time scenario
Thanks
great explanation, thank you so much for making this kind of video.
Thank you
Amazing simple explanation i have never seen such a simple explanation keep it up 😊
thanks
In case of multiple observables, do you end up writing different update methods in the observer class. Checking instance of gets replaced with new method strongly tied to one of the observable. Am I missing something here?
Very Nice Explanation. Please keep on adding new videos on these topics
thank you
Bhai kya explain krte ho yr, maja aa gya. I love you.
Thank you 🙂
While notifying observers, we should do the same in an asynchronous manner.
Nicely explained everything ❤
Thanks
Great explanation man! It was really helpful
Thanks
very good explanation. Thank you
thank you
Hi Shrayansh, thanku for this amazing tutorial.
Thanks 👍
just joined the channel for lld, hld and java videos, great content bro, worth the money 😃
:) thanks a lot
I think that in the setStockCount() method, we should do stockCount = newStockAdded instead of stockCount+=newStockAdded since let's say we want to make the stock as 0 as in 32:42, the stockCount will stay 10 itself as 10+0 = 10. That's the reason why it didn't notify second time. Please correct me if I am mistaken somewhere while understanding this. Thanks.
Yes I was also thinking why it didn't print twice
I think it's better if we keep the setStockCount() as such and add another method addStockCount() which will notify subscribers.
Another thing is we have to add the stocks before notifying the subscribers if we want to get the new stockCount from the observable in update() method.
stockCount+=newStockAdded is correct. Let's say stockcount = 5, then we add 100 new stocks, so stockcount becomes 105. But if we use stockCount = newStockAdded, then it will give incorrect stock count (in this case 100 instead of 105 ).
@@tanmaya2421 True, but then for the example he gave when he set stock count to 0, he should have reduced the stock count with -10
Yes , it should have printed twice but it didn't because stockCount is set to 0 for once only, after that stockCount is getting updated everytime resulting notify method not getting executed.
Amazing explanation. nice video
Glad you liked it
Greate work, Huge respect for you bro.
Thanks
Awesome Content
Perfect , to the point explanation. Keep posting vedios👏
Thanks
very nicely explained.Keep it up
thank you
Awesome Explanation ...
thanks
Hey, lets say if there is a observer concrete class listening to 3 different observables, [video time : 12:55]
1. How will we inject 3 different objects of observables [for eg if a single customer says notify me for 3 different products at different times] in the observers class ?
2. Even if we can lets says we maintain a List in Observer, we still would need to go through all of them to know which one has actually changed.
So how is Observer keeping an object / list of Observable/ Observables helping ? Basically whats the best solution in that case ?
What is the benefit of injecting the observable interface (StockObservable) in observer classes (EmailNotification, SMSNotification etc.) when you are not using it at all.
Brother-- I am not sure about others but it took me 4 hours to understand the observer pattern-- but still not feeling confident. Could you please make a detailed one on explaining why we need this pattern??
Hi shreyansh !could you please explain the "o isinstance of thing " from 11th minute onwards... like isinstance of what? i'm having problems understanding that
excellent explanation!
thanks
Great explanation 👍🏻
Thank you
Great Explanation
Thanks
Can we achieve this problem statement via pub-sub model. if yes then what is core difference between observer pattern and pub-sub model ?
Would it be better to send all Emails using one Observer or Creating observer for each email?
I was thinking ideally for each product, there would be 100s of subscribers and foir each we have to send email, And each of these email should be stored in Db.
So we can read and create a Observer class at once.
Why can't you pass 'this' (instance of WSObservable) in the update instead of constructor injection. You can then call whatever methods on that like you did with constructor instantiated object.
Thanks, greatly helpful.
Glad it was helpful!
Awesome explanation
Thanks 🙏
observal -> state changes.
observer -> state changes of observable observed by observer
make there interface where there can be many observer but single observable and make there classes
In observable class u will make a list of observer , u will add observer, remove objserver , notify all observer , and change the state of data or get the state of data.
For the update method in Observer why it will take param of type Object rather than of type Observable? It should ideally take Observable type param. If this true then we are not accomplishing anything with constructor injection way, because type casting will be required in both ways. Can you pls elaborate more on the benefits of constructor injection way instead of making it method param?
Thank you
I have a query for a so long... do we need to create n number of classes similar to IPhone class .. because we will have lakhs of products in Amazon or Walmart and if we have to notify for every product... do we need to create individual classes for each and every product?
Sir I am just starting to watching your video. Please Sir make more it'll help us
Sure, yes lot of videos already uploaded and next will come on Sunday
So In real world senario Relational DB is more sutable for observer pattern for sending notification to our users via web scokets as there are more add and delete (sub and unsub) operation ?
I have one question why are we storing the object of the stock Observable in the Observer class, what will be the other way to implement this, I saw the video but didn't get this part
Hi you used DisplayObserver Interface in WsObservableInterface add and remove methods what if I have different kind of observer how is it handled here ? Or am I missing something?
Isn't having both the setData() and notify() method in the same class violating the single responsibility principle?
Good stuff Shrayansh. Keep them coming
thank you
Really helpful Thanks
Thank you
At 14:40 if we are setting one observable object in the constructor, how will we get notified for mutliple observables (practically observer should be able to get notified for multiple items).
Why i found your video so late ?Anyway, You make things easier then what people used to complex it.
thanks for the feedback
Just like a wow
Let's say I have 3 million observers, should I iterate ?? Also, this code is storing the state in the app which can be a single point of failure, aka the observer list. Should I consider it stored in a cache rather than the application ??
Great work man..
Thank you
Really Helpful!
Thank you
Hi. I didn't get what would be the issue if we pass the same object to update method. Sorry, I am a noob. I mean, what issue would it create? Why would we need to check the instance of the class if we can directly call a specific method to do something on update. Thanks.
in the update method firstly the count should be set and then if (count != 0) notifySubscribers can be called.
So, we can say that observer pattern behaves like publisher and subscriber?
the best teacher for lld
Thanks 🙏
Buddy it's easy to add caption for old videos. Just edit your video in UA-cam studio and select English in caption drop-down and then click save/submit changes. It will take sometimes to add caption automatically to your video.
No buddy it's not working, hindi to English caption is not working. It want me to type each line which I am saying in hindi.
There are certain language for which auto caption is available and hindi is not one of it.
@@ConceptandCoding Oh okay buddy. If possible could you please create playlists for spring boot if possible. You really good teacher nice content creator that's why I am giving my suggestion
I understand the observer pattern but what if the application goes down or somehow we lost the stored information(observers) ? in this scenerio how will get the data back ? of course we need to store these information in db. but if it`s in the db then what is the purpose of observer pattern ? we can simple query the db and do the stuff (querying again and again is not good approach ) so can you or anyone explain this to get better understanding about this.
Can we write the update method of subscribers in the following way:
update(Iobservable *obj){
obj->getData();
//some other operations
}
From ur example felt like emailnotification observer is bounded with only 1 observable but if emailnotification observer wanted to notify via iphonestock observable and samasungstock observable???
What we we pass the concrete object in update method and in update we receive that only like : update(ObserverableConcreteClass obj) . in this case we i don't need to check the instance of right ?
CricketScoreObservable, How will we inject when you have injected WSObservable?
thanks
Mail sent message should be display twice right? But it's showing only once
Hi I have one doubt, if we set the type of Observable a Observer class is listening too, then in the usecase where we will have 1 Observer class listening to multiple Observable class how will we assign the type? Either we will have to maintain a list in Observer class which will denote all the Observable it is listening to or we will have to create separate Observer class for each Observable?
At 13:04 I have a question, is adding getter and setter methods of every state in the publisher interface a good implementation ? This will violate two properties :
1. SRP
2. Interface is an abstraction layer to the implementations. If we specify getter and setter methods there, we are making the internal implementation public.
Kindly respond .
Hey how to implement in real life, like are we going to push all the observers in kafka like for Notify me for flipkart
And then a cron job or consumers will process those events and send the push notifications
when we make the object of observable, then it needs list of observer and to make the object of observer i need to give its observable, wont it create cycle?
very good
thanks
Suppose, we have multiple classes which implement an Observable interface, e.g , ConcreteObservable1 has 2 properties, age and salary, and ConcreteObservable2 has 2 properties, city, and country, and the observers are interested in the state change of all these 4 properties, and we have the update() method in Observer interface which the concrete observers implements, then how are the observers going to get the data.
As you told, we can have setState() in the Observable interface, what should be the return type of setState() in this case since the observers are interested in all 4 properties.
observable class should contain different sets of notification,
Set cityinterestedobservers;
Set ageinterestedobservers;
Whenever state is changing, based on what is changed , interested observers are notified.
Question 1 : In case of more then one observable in a single observer, wouldn't you still have to check which observable called the update method? resulting in some if/else again inside update().
Question 2 : If we don't send the data in update(), won't the update logic loose on some data in case of race condition? (say temperature updates few times in a row, and since instead of passing the temperature value we are passing observable object which will have updated data always.)
It will be better if we send the observable object in update method. You have found the loopholes
Yha par, ek observer, multiple observable ko observe nhi kr skta. right ???