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

КОМЕНТАРІ • 415

  • @NeverGiveUp186
    @NeverGiveUp186 Рік тому +59

    This is the first LLD video where I saw real coding example.. and not just theory. Great work man!! Keep it up !!

  • @deepakkedia3881
    @deepakkedia3881 Рік тому +5

    nicely explained Shrayansh. Hope to get more videos on LLD.

  • @kapilgyanchandani
    @kapilgyanchandani Місяць тому +1

    Very good explanation with examples, Thanks a lot for such videos.

  • @agunnik8
    @agunnik8 Рік тому +9

    Happy to be here before this channel blows up. Keep up the quality content.

  • @eshwarsai5027
    @eshwarsai5027 15 днів тому +1

    Man! You are really killing in teaching the concepts❤️.

  • @parasmyname784
    @parasmyname784 Рік тому +7

    Just brilliant, You are giving us true insight into enterprise level coding,, thanks a lot

  • @sameerchoudhary8590
    @sameerchoudhary8590 3 місяці тому

    Very crisp and clear explanation.

  • @DeepakKumar-zb8bh
    @DeepakKumar-zb8bh 9 днів тому +1

    awesome explanation

  • @dhvanilvadher1356
    @dhvanilvadher1356 2 місяці тому +1

    Thank you @Shreyansh ! I have never found a better Design pattern video.

  • @paritoshdadhich2954
    @paritoshdadhich2954 8 місяців тому +1

    Thanks for the wonderful explanation

  • @sameer9368
    @sameer9368 9 місяців тому +2

    Awesome Explanation♥

  • @vikashkumarchaurasia1299
    @vikashkumarchaurasia1299 Рік тому +2

    🎉🎉🎉 perfect explaination

  • @omtiwari2387
    @omtiwari2387 11 днів тому

    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.

  • @ParomitaDeSarkar
    @ParomitaDeSarkar Рік тому +1

    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!

  • @sagar1691
    @sagar1691 4 місяці тому +1

    Again very well explained , And yes it will be nice if you explain Implementation With PropertyChangeListener which is another way to implement this.

  • @neha6000
    @neha6000 Рік тому +1

    Bahoot hi achha explanation

  • @aadhaarbhatia2759
    @aadhaarbhatia2759 3 місяці тому

    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!!

  • @user-cy8fj7xx1n
    @user-cy8fj7xx1n Місяць тому +1

    Great video

  • @gyandeepdigra8461
    @gyandeepdigra8461 6 місяців тому +2

    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..

  • @runtime379
    @runtime379 Рік тому +9

    As a fresher , Started learning LLD from today. Enjoying the concept .so simple explanation and easy to understand
    Thanks bhaiya

  • @abhaytiwari1615
    @abhaytiwari1615 Рік тому +9

    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

  • @srijonroy7084
    @srijonroy7084 6 місяців тому +3

    Simple and brilliant explanation. Particularly good for those who is revising and have some idea

  • @ayushijain2911
    @ayushijain2911 Рік тому +1

    One of the best explanation on youtube. Thanks

  • @GlynesDsouza-zb8pq
    @GlynesDsouza-zb8pq Рік тому +5

    Too good.... This level of depth in knowledge covered with such simplicity..... Its phenomenal. Thank you so much and keep the good work up

  • @syedsheheryarumair97
    @syedsheheryarumair97 9 днів тому

    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?

  • @IAF805
    @IAF805 Рік тому +1

    Simple and crispy explanation, hats-off to you

  • @adhrit1426
    @adhrit1426 5 місяців тому +8

    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

    • @swayamprakashsahoo9002
      @swayamprakashsahoo9002 2 місяці тому

      Exactly. This code will be lengthy when observer will observe multiple observable objects. So better to pass observable object through update parameter.

    • @msingla135
      @msingla135 2 місяці тому

      Was thinking the same. Why not pass the object of current concrete class to update instead of interface.

    • @ankitgoyal8556
      @ankitgoyal8556 2 місяці тому

      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.

    • @omkarjoshi3750
      @omkarjoshi3750 2 місяці тому

      @@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()

    • @utkarshjaiswal7224
      @utkarshjaiswal7224 Місяць тому

      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

  • @satish7931
    @satish7931 Місяць тому

    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

  • @anirudhsingh7832
    @anirudhsingh7832 4 місяці тому +1

    Good Video. Thanks man.

  • @vaibhavpopat99
    @vaibhavpopat99 Місяць тому

    great video :)
    qq - in the observer class, what of its observing >1 observables. In that case, is using 'instanceof' the only solution ?

  • @kushal7966
    @kushal7966 9 місяців тому

    Thank You for the video . Can you suggest me which design pattern are helpful from Mobile (Android)App Development Point Of View

  • @jaymanik
    @jaymanik Рік тому +1

    Thank you for the detailed video. Very good explaination.

  • @mohitladia
    @mohitladia Рік тому +2

    Loved the real-time scenario

  • @Newbie789
    @Newbie789 Рік тому

    great explanation, thank you so much for making this kind of video.

  • @harshmangal6576
    @harshmangal6576 2 місяці тому +1

    Amazing simple explanation i have never seen such a simple explanation keep it up 😊

  • @terminator8397
    @terminator8397 Рік тому +3

    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?

  • @nishantkumar6116
    @nishantkumar6116 Рік тому +1

    Very Nice Explanation. Please keep on adding new videos on these topics

  • @mahendrapatidar7416
    @mahendrapatidar7416 Рік тому

    Bhai kya explain krte ho yr, maja aa gya. I love you.

  • @umangmalhotra1222
    @umangmalhotra1222 Рік тому +3

    While notifying observers, we should do the same in an asynchronous manner.

  • @dheeraj5531
    @dheeraj5531 9 місяців тому +2

    Nicely explained everything ❤

  • @anuragv400
    @anuragv400 9 місяців тому +2

    Great explanation man! It was really helpful

  • @prakashshindalkar6959
    @prakashshindalkar6959 Рік тому +1

    very good explanation. Thank you

  • @vishalsharma8432
    @vishalsharma8432 Рік тому +1

    Hi Shrayansh, thanku for this amazing tutorial.

  • @handlenotavailable538
    @handlenotavailable538 10 місяців тому +1

    just joined the channel for lld, hld and java videos, great content bro, worth the money 😃

  • @snehilsinha4689
    @snehilsinha4689 Рік тому +41

    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.

    • @dhanbahadurchhettri2712
      @dhanbahadurchhettri2712 Рік тому

      Yes I was also thinking why it didn't print twice

    • @wul_frik
      @wul_frik Рік тому +3

      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.

    • @tanmaya2421
      @tanmaya2421 Рік тому +1

      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 ).

    • @ishaansrivastav5732
      @ishaansrivastav5732 9 місяців тому

      @@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

    • @ranitganai2725
      @ranitganai2725 4 місяці тому

      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.

  • @ravikumar-gp6ui
    @ravikumar-gp6ui Рік тому

    Amazing explanation. nice video

  • @jinendrajain7672
    @jinendrajain7672 11 місяців тому +1

    Greate work, Huge respect for you bro.

  • @GirishJain
    @GirishJain Рік тому +1

    Awesome Content

  • @gauravsharma4532
    @gauravsharma4532 Рік тому

    Perfect , to the point explanation. Keep posting vedios👏

  • @aashishgoyal1436
    @aashishgoyal1436 Рік тому +1

    very nicely explained.Keep it up

  • @patrisrikanth
    @patrisrikanth 4 місяці тому +1

    Awesome Explanation ...

  • @Lucifer-dd1zn
    @Lucifer-dd1zn 27 днів тому

    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 ?

  • @tastaslim
    @tastaslim Рік тому

    What is the benefit of injecting the observable interface (StockObservable) in observer classes (EmailNotification, SMSNotification etc.) when you are not using it at all.

  • @rhimanshu6288
    @rhimanshu6288 10 місяців тому

    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??

  • @rahulnamdev5115
    @rahulnamdev5115 9 місяців тому

    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

  • @iWontFakeIt
    @iWontFakeIt 3 місяці тому +1

    excellent explanation!

  • @pawan0209
    @pawan0209 Рік тому +1

    Great explanation 👍🏻

  • @rpatil1169
    @rpatil1169 9 місяців тому +1

    Great Explanation

  • @geekcse4733
    @geekcse4733 Рік тому

    Can we achieve this problem statement via pub-sub model. if yes then what is core difference between observer pattern and pub-sub model ?

  • @ROHITRAAZANAND567
    @ROHITRAAZANAND567 2 місяці тому

    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.

  • @shardulsant4726
    @shardulsant4726 Рік тому +4

    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.

  • @utkarshsharma4877
    @utkarshsharma4877 Рік тому

    Thanks, greatly helpful.

  • @vnitish14
    @vnitish14 Рік тому

    Awesome explanation

  • @namanjain2488
    @namanjain2488 8 місяців тому +2

    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.

  • @clutchh_god
    @clutchh_god 10 місяців тому

    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?

  • @chandanprakash2677
    @chandanprakash2677 Рік тому +1

    Thank you

  • @SudhirKumar-oz4tk
    @SudhirKumar-oz4tk Рік тому +1

    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?

  • @nikhilupadhyay2199
    @nikhilupadhyay2199 Рік тому

    Sir I am just starting to watching your video. Please Sir make more it'll help us

    • @ConceptandCoding
      @ConceptandCoding  Рік тому

      Sure, yes lot of videos already uploaded and next will come on Sunday

  • @nitinkukreti2489
    @nitinkukreti2489 Рік тому +1

    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 ?

  • @santoshbhatnagar2155
    @santoshbhatnagar2155 5 місяців тому

    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

  • @anantjain1263
    @anantjain1263 Рік тому

    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?

  • @sristijaiswal1013
    @sristijaiswal1013 4 місяці тому

    Isn't having both the setData() and notify() method in the same class violating the single responsibility principle?

  • @mohitthakur5571
    @mohitthakur5571 Рік тому +1

    Good stuff Shrayansh. Keep them coming

  • @shivshaktipandey7148
    @shivshaktipandey7148 Рік тому

    Really helpful Thanks

  • @UntamedRogueMavrick
    @UntamedRogueMavrick 3 місяці тому

    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).

  • @shatrudhankumar7824
    @shatrudhankumar7824 3 місяці тому +1

    Why i found your video so late ?Anyway, You make things easier then what people used to complex it.

  • @syedmizbahuddin9390
    @syedmizbahuddin9390 3 місяці тому +1

    Just like a wow

  • @ryan-bo2xi
    @ryan-bo2xi 11 місяців тому

    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 ??

  • @sunitajoshi902
    @sunitajoshi902 10 місяців тому +1

    Great work man..

  • @engineer_jaatni
    @engineer_jaatni 10 місяців тому +1

    Really Helpful!

  • @vaibhavjaiswal7811
    @vaibhavjaiswal7811 11 місяців тому

    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.

  • @anantsingh2004
    @anantsingh2004 Рік тому +1

    in the update method firstly the count should be set and then if (count != 0) notifySubscribers can be called.

  • @ashutoshpandey4271
    @ashutoshpandey4271 5 місяців тому

    So, we can say that observer pattern behaves like publisher and subscriber?

  • @abirpaul9027
    @abirpaul9027 5 місяців тому +1

    the best teacher for lld

  • @aravindkumarn1776
    @aravindkumarn1776 10 місяців тому +1

    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.

    • @ConceptandCoding
      @ConceptandCoding  10 місяців тому +1

      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.

    • @aravindkumarn1776
      @aravindkumarn1776 10 місяців тому

      @@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

  • @AnupSingh-hd5ck
    @AnupSingh-hd5ck 8 місяців тому +1

    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.

  • @siddhantjaiswal4231
    @siddhantjaiswal4231 Рік тому

    Can we write the update method of subscribers in the following way:
    update(Iobservable *obj){
    obj->getData();
    //some other operations
    }

  • @premium3968
    @premium3968 Рік тому

    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???

  • @harishaseri
    @harishaseri 5 днів тому

    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 ?

  • @SaketAnandPage
    @SaketAnandPage Рік тому

    CricketScoreObservable, How will we inject when you have injected WSObservable?

  • @AmanSharma-ht5zq
    @AmanSharma-ht5zq Рік тому +1

    thanks

  • @LeetCodeWithAK-im1nn
    @LeetCodeWithAK-im1nn 9 місяців тому

    Mail sent message should be display twice right? But it's showing only once

  • @adityaagarwal7291
    @adityaagarwal7291 Місяць тому

    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?

  • @smirkedShoe
    @smirkedShoe 2 місяці тому

    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 .

  • @SushilKumar-rv9eo
    @SushilKumar-rv9eo Рік тому

    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

  • @lokeshrajput2458
    @lokeshrajput2458 6 місяців тому

    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?

  • @akshaymahajan9626
    @akshaymahajan9626 3 місяці тому +1

    very good

  • @zmxncbv95
    @zmxncbv95 Рік тому +1

    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.

    • @ANSHULGUPTA880
      @ANSHULGUPTA880 10 місяців тому

      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.

  • @ishanpatil6294
    @ishanpatil6294 9 місяців тому +4

    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.)

    • @abhishekmahajan17
      @abhishekmahajan17 9 місяців тому

      It will be better if we send the observable object in update method. You have found the loopholes

  • @nishaaa_maurya
    @nishaaa_maurya 9 місяців тому

    Yha par, ek observer, multiple observable ko observe nhi kr skta. right ???