Javascript Design Patterns #5 - Observer Pattern

Поділитися
Вставка
  • Опубліковано 18 лис 2024

КОМЕНТАРІ • 123

  • @DevSage
    @DevSage  3 роки тому +1

    📑 Blog Post: www.explainedsimply.io/observer-design-pattern-explained/
    🤖 GitHub: github.com/pkellz/devsage/blob/master/DesignPatterns/Observer.js
    📗"Design Patterns Explained Simply" Ebook: payhip.com/b/MLtJ
    💙 Twitter: twitter.com/realDevSage
    📙 Ebooks: payhip.com/devsage
    💥 Discord: discord.gg/BP8wPv6raA

  • @js_programmer8423
    @js_programmer8423 2 роки тому +3

    im not gonna lie..you are the best teacher I've ever had, you literally talk to me like i'm 5 years old. thank you.

  • @tahasaleh4697
    @tahasaleh4697 4 роки тому +57

    Are you a subject? because as an observer, I just subscribed!

    • @DevSage
      @DevSage  4 роки тому +2

      Hahah that's pretty good. Welcome!

    • @duechilidance5388
      @duechilidance5388 4 роки тому +1

      Wouldnt be a better analogy that a video viewer (or its account) is the subject, and the youtube channels are the observers, firing Videos (or notifications) at you, because you - the subject - subcribed to the Observer/Channel?

    • @alimertc
      @alimertc 4 роки тому +1

      @@duechilidance5388 Yeah don't overthink it

    • @duechilidance5388
      @duechilidance5388 4 роки тому

      @@alimertc 🤔🤓

    • @NotPolloz
      @NotPolloz 3 роки тому

      @@duechilidance5388 ooof :)

  • @DevSage
    @DevSage  5 років тому +31

    Note: Instead of using fn.call(), you could also just use fn(). They are the same!

    • @evgenylevchenya8734
      @evgenylevchenya8734 2 роки тому +3

      + .call() makes sense only if you want to provide call context

  • @CarlitoProductions
    @CarlitoProductions 3 роки тому +8

    This is by far the best explanation I could find on the subject. Great job with this video

  • @HologramJay
    @HologramJay 3 роки тому +8

    You're a talented teacher, DevSage. Thanks.

    • @DevSage
      @DevSage  3 роки тому

      I appreciate it! 😁

  • @Strasbourgeois
    @Strasbourgeois 4 роки тому +6

    What was demonstrated so nicely in this video is the pubsub pattern. It resembles a lot to the observer pattern and these two are used interchangeably most of the time. There is a subtle difference between them though: pubsub subscribers are not aware of the subject whereas observer subscribers have a reference to the subject

    • @aislanarislou
      @aislanarislou 4 роки тому +2

      Can you point me with some example code to this difference. I'm a little bit confused.

  • @peristiloperis7789
    @peristiloperis7789 4 роки тому +11

    You've just got a new subscriber. Great lesson. Keep the good job!

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

    You are the only one who has nicely explain these patterns. Good Work!

  • @stud_-yl1bu
    @stud_-yl1bu 4 роки тому +6

    I finally understood what is going on ,keeeeep goooing man.

  • @MegaVladikslavman
    @MegaVladikslavman 3 роки тому +2

    The best explanation I seen on UA-cam. Thanks a lot!

    • @DevSage
      @DevSage  2 роки тому

      Glad it was helpful!

  • @neuodev
    @neuodev 2 роки тому +1

    Thanks for you "Observations"!

  • @juraev0056
    @juraev0056 3 роки тому +2

    Clear explanation with simple examples. Thank you!

  • @trapslime16
    @trapslime16 4 роки тому +8

    This channel something different

    • @DevSage
      @DevSage  4 роки тому +1

      Appreciate ya

  • @circulardep
    @circulardep 4 роки тому +7

    This was a great explanation of how an event listening system would work in its essence.
    I'm not sure if it's a good practice to override the prototype object like you did, but rather extend it.

    • @DevSage
      @DevSage  4 роки тому +3

      Thanks. I agree with you. I should have done "Subject.prototype.subscribe = function()..." and the same with the other methods

    • @circulardep
      @circulardep 4 роки тому +2

      @@DevSage You're doing a great job man. I just searched for JS design patterns and found your videos. I immediately subscribed! Keep up the good work.
      This pattern specifically is extremely powerful in JS frontend applications, where you need to broadcast messages and notify other parts of the application about an event. It's great for communication between separate software modules. State management in the modern frontend frameworks adopt this pattern heavily.

    • @luiggymacias5735
      @luiggymacias5735 2 роки тому

      @@DevSage you could also use: Subject.prototype = {
      ...Subject.prototype,
      }

  • @onlyme0349
    @onlyme0349 5 років тому +2

    like how you keep it concise and simple ty

  • @RyanFromUltrasound
    @RyanFromUltrasound 3 роки тому +1

    Curly braces on new lines. Prototypal over Pseudoclassical. Based.

  • @Videosuser
    @Videosuser 3 роки тому +1

    One of the best explanations! Thank you.

  • @quarantinemade223
    @quarantinemade223 3 роки тому +2

    Thanks for such a simple and amazing explanation👍

  • @MrTomro
    @MrTomro 3 роки тому +2

    subbed, i already love this channel and ive only seen 3 vids

    • @DevSage
      @DevSage  3 роки тому +1

      Appreciate you

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

    Very clear explanation, thank you!

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

      No problem!

  • @rahulrao408
    @rahulrao408 3 роки тому +1

    Very Nicely Explained!

  • @devchannel5232
    @devchannel5232 3 роки тому +1

    Very, very good explanation! Keep going man!

  • @hornetdc
    @hornetdc 3 роки тому +2

    Why do you need to call prototype to add methods?

  • @XLpacman805
    @XLpacman805 2 роки тому +1

    You made this so simple, thanks.

    • @DevSage
      @DevSage  2 роки тому

      Glad it helped!

  • @SiegfriedxD
    @SiegfriedxD 4 роки тому +1

    Finally understood this pattern. Thank you very much!

    • @DevSage
      @DevSage  4 роки тому

      You're welcome friend

  • @bubz476
    @bubz476 3 роки тому +2

    I love these videos man! Thanks a lot!

  • @suganya32
    @suganya32 3 роки тому +1

    Very good video for observer design pattern. Thank you.

    • @DevSage
      @DevSage  3 роки тому

      You are welcome

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

    I was wondering whether I can use the observer pattern to replace a monkey patch for a drop handler. Let me explain: The host program has a foldertree._onDrop() . At the moment, i am replacing that with my own function, which executes some code, reading the parameters, (if a condition is fulfilled). Afterwards it calls the original _onDrop. So my objective is to execute my own code before a built in function is called. Monkey patching always felt a little bit heavy handed, so can it be done with observers?

  • @evgenylevchenya8734
    @evgenylevchenya8734 2 роки тому

    One little thing: your code allows you to push the same observer multiple times and it will fire it up multiple times.
    It should have check for having observer in array before push

    • @evgenylevchenya8734
      @evgenylevchenya8734 2 роки тому

      + this video doesn't explain how to pass data to observers

    • @DevSage
      @DevSage  2 роки тому

      @@evgenylevchenya8734 Good observations

  • @softwarelivre2389
    @softwarelivre2389 3 роки тому +1

    What's the difference between doing this and adding an event listener / firing an event in JS? I didn't quite get it.

  • @bibek8284
    @bibek8284 3 роки тому +1

    Very Well Explained video. But Can you also elaborate where Can I use this pattern in real world cases?

  • @Sindoku
    @Sindoku 2 роки тому +1

    “Girl you must be a subject because I’m observing all your actions”

  • @true_bites
    @true_bites 4 роки тому

    I became your big fan man. You are just a masterpiece.

  • @riccarrasquilla379
    @riccarrasquilla379 2 роки тому +1

    good explanation. thank you very much

    • @DevSage
      @DevSage  2 роки тому

      You're welcome!

  • @nandwanaabhishek57
    @nandwanaabhishek57 3 роки тому +1

    Liked and subscribed, good stuff man!

  • @zarkoboyadjiev3061
    @zarkoboyadjiev3061 5 років тому +4

    Instead of using if statement in your filter just use it like this:
    arr = arr.filter( el => el !== condition) ;
    if el is not equal condition it will return element, else won't return anything.

    • @DevSage
      @DevSage  5 років тому +4

      You are correct. I thought it was wise to be slightly more verbose than usual because it's possible that some beginners may not understand.

    • @DarkNivo
      @DarkNivo 4 роки тому +2

      @@DevSage That is not verbose it is misleading. The filter takes predicate functions and looks at the return !!value based on it true/false. So using it like a map gives misleading concepts of the filter function. And works only because !!fn === true and !!undefined (no return) === false.

    • @DevSage
      @DevSage  4 роки тому

      @@DarkNivo I'm afraid I don't understand what you mean

    • @DarkNivo
      @DarkNivo 4 роки тому +6

      @@DevSage OK. HI again.
      Array.prototype.filter() takes a predicate as a callback function. Yes? That is why the function that you wrote works but is misleading. Predicate function should clearly return bool value.

    • @DevSage
      @DevSage  4 роки тому

      @@DarkNivo Yes, now I see what you are saying

  • @sivashanmugam4444
    @sivashanmugam4444 4 роки тому +1

    Clean explanation, Thanks

    • @mindblow150
      @mindblow150 4 роки тому

      Greetings from Philippines

  • @robertchytil1
    @robertchytil1 4 роки тому

    very nice channel.....just little comment to this tut: everyvere you are using arrow function..you dont have to wright: ex: foreach(fn => {return ..} instead of foreach(fn => fn() ) and thats it :-) always reasonable make it shortly unless otherwise explicitly required .

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

    Nicely done, thank you.

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

      Thanks for watching!

  • @bairam90
    @bairam90 5 років тому +2

    Greetings from Egypt

    • @DevSage
      @DevSage  5 років тому +1

      Greetings!

  • @rodrickcoleman8449
    @rodrickcoleman8449 4 роки тому +2

    Great Video!!! I was watching a Traversy Media tutorial and was struggling. I had to rewatch yours four times to break it all down.
    subscribe: Adds functions to the array so that it may be used.
    unsubscribe: Removes functions from the array to discontinue use.
    fire: Loops the array and calls all the functions inside.

    • @DevSage
      @DevSage  4 роки тому

      Right on the money

  • @shamlikiruba
    @shamlikiruba 2 роки тому

    Great video man!

  • @mindblow150
    @mindblow150 4 роки тому +1

    Greetings from Philippines!

  • @stud_-yl1bu
    @stud_-yl1bu 4 роки тому +1

    I wish you are explaining other patterns with the same level.

    • @DevSage
      @DevSage  4 роки тому +1

      I have more design pattern videos on my channel

    • @stud_-yl1bu
      @stud_-yl1bu 4 роки тому +1

      @@DevSage on my way for watching it.
      If i understand it i will finally start my work as a js coder. I wish js will not cause me death before thaaat moment 😂😂😂

  • @НиколайГроза-е9н
    @НиколайГроза-е9н Місяць тому

    Чувак спасибо большое! Ты крутой!

  • @dungvu8790
    @dungvu8790 3 роки тому +1

    thank you

  • @avinanrey
    @avinanrey 4 роки тому +1

    Thank you for this video man!

    • @DevSage
      @DevSage  4 роки тому

      No problem!

    • @avinanrey
      @avinanrey 4 роки тому

      @@DevSage I tried to implement this in ES6 Class. It kind of taught me how React state and render observer pattern works: playcode.io/511599

  • @kirillpavlovskii8342
    @kirillpavlovskii8342 3 роки тому +1

    Thanks

  • @Sleepytj007
    @Sleepytj007 3 роки тому +1

    thank you.

    • @DevSage
      @DevSage  3 роки тому

      You're welcome!

  • @denpol9956
    @denpol9956 3 роки тому +1

    Why dont you use ES6 classes?

  • @MrMarkgyuro
    @MrMarkgyuro 4 роки тому +1

    can we use pop instead of filter ?

    • @DevSage
      @DevSage  4 роки тому

      Well, .pop() just removes the last element in the array. So unless you're 100% POSITIVE that the last observer in your array is the observer you need to remove, I would just stick to using filter.

    • @MrMarkgyuro
      @MrMarkgyuro 4 роки тому +1

      @@DevSage yeah, really :D
      this was a silly question :)

  • @MrXmen262
    @MrXmen262 4 роки тому +1

    keep at it

  • @tubanami
    @tubanami 4 роки тому +1

    great explanation bro, thanks for sharing
    +1 subscribed

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

    Thank you

  • @Russ_Paul
    @Russ_Paul 4 роки тому

    Just a thought but couldn't observers be a WeakSet instead? So no need for filtering. A quick test
    const a = (x) => x
    const b = (y) => y
    const c = (z) => z
    const observers = new WeakSet()
    const subscribe = fn => observers.add(fn)
    const unsubscribe = fn => observers.delete(fn)
    ;[a,b,c].forEach(subscribe)
    unsubscribe(b)
    console.dir(observers) // [0](z) => z [1](x) => x
    edit: order maybe an issue

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

    what does it observe ? :D

  • @yannicktamburrini7833
    @yannicktamburrini7833 2 роки тому

    Nice video but this es5 syntax is ticking me, why not just use classes ? Neither in this case a Set for subscribers ? No for of but a .foreach, and why would you use fn.call() instead of fn() ? Definitly this syntax is a bit confusing :/

  • @diegognoatto8706
    @diegognoatto8706 3 роки тому

    very nice :D

  • @LenonSec
    @LenonSec 4 роки тому

    you are a god!!!!

  • @Felipe-zl1rj
    @Felipe-zl1rj 3 роки тому +1

    3:12 Filter doesn't work like that, it should return a boolean value, like this: this.observers.filter(fn => fn !== fnToRemove)

  • @RashadJackson07
    @RashadJackson07 4 роки тому +1

    i dont understand

    • @DevSage
      @DevSage  4 роки тому

      Which part?

    • @RashadJackson07
      @RashadJackson07 4 роки тому

      DevSage I don’t understand the whole structure like can you give a real worl example because you gave a generic example

  • @rubickon9500
    @rubickon9500 3 роки тому

    first its an exact copied example from udemy course by Traversy called 'Modern JavaScript From The Beginning'. Second, its a bad example since basically its only showing a '.call' usage, there is no automatic notifier happing here. Observer should notify when a change is happening while here there is no implementation of that.

    • @DevSage
      @DevSage  3 роки тому

      I guess you're entitled to your opinion 🤷🏾‍♂️

  • @maxkinli
    @maxkinli 4 роки тому

    Tiimmy Turner

  • @dnyaneshwaruttarwar8832
    @dnyaneshwaruttarwar8832 5 років тому +1

    It's same like a PubSub pattern

  • @webmaster246
    @webmaster246 2 роки тому

    Helpful, thanks !

  • @itsmrakhil
    @itsmrakhil 3 роки тому +1

    Inside the unsubscribe function this.observers.filter(fn => fn !== fnToRemove); is enough. Luckily the code in the video is working because JS considers function as a truthy value.