Javascript Design Patterns #8 - Visitor Pattern

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

КОМЕНТАРІ • 44

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

    🤖 GitHub: github.com/pkellz/devsage/blob/master/DesignPatterns/Visitor.js
    📕"Design Patterns Explained Simply" Ebook: payhip.com/b/MLtJ
    💙 Twitter: twitter.com/realDevSage
    📙 Ebooks: payhip.com/devsage
    💥 Discord: discord.gg/BP8wPv6raA

  • @GauravBoraJodhpur
    @GauravBoraJodhpur 4 роки тому +13

    I was going to sleep but then I came across a video of yours and binge-watched all the videos in the Tutorials playlist. And it is 3.30AM.
    So, awesome work!!

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

      Glad you like them!

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

      2 years later and I'm watching this at 0355am

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

    I happy that I "visited" DevSage!

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

    I've been struggling to understand this, thanks a lot!

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

      You're welcome!

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

    Amazing videos. Very nice explanations. Would like you to consider making a video on composite pattern

  • @amerikan
    @amerikan 4 роки тому +15

    Overall good video, though I would just say be careful when doing Employee.prototype = { getFoo: function () { ... code ....}}; as this will overwrite the original prototype reference! It’s safer to do Employee.prototype.getFoo = function () {.....};

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

      That's actually a really good point. I hadn't even thought about that until reading this comment. Thanks

  • @ינוןאלבז-כ1ז
    @ינוןאלבז-כ1ז 2 роки тому +1

    very cool, thanks for shering, exellent channel!!!

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

    I went to watch this video and realized I'm watching it at the exact same time it was recorded 2 years later. 3:28 PM on December 7th

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

      😅😅

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

      Maybe it's some kind of omen

  • @subrotomukherjee8571
    @subrotomukherjee8571 20 днів тому

    very good series

  • @Chris-qg6kc
    @Chris-qg6kc 3 роки тому +5

    Is there a particular reason that you always do JavaScript OOP using the prototype method vs class kw? I like what you are doing -continued Success 👍🏾💯👌🏾

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

    Thanks!

  • @georgidimitranov8781
    @georgidimitranov8781 5 років тому +3

    Hey, greeting from Bulgaria. Thanks for making this videos. Your explaining stuff really good.
    My request is. Can you make videos for the PubSup and Mediator patters. ^^

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

      Greetings. Thank you for the compliment. I have a video about the mediator pattern here -> ua-cam.com/video/ZuhgOu-DGA4/v-deo.html

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

      +1 from Bulgaria! :D

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

    nice pattern! thanks!

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

    isnt this is same as bind?

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

    Nice tutorial. I’m trying to think about a practical use case for this. Is this scenario valid.. The Employee class is manipulated by the HR department who hires employees, logs their information and such. The ExtraSalary class is handled by the Finance department?

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

      Yeah that actually sounds like it could be a valid application

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

    Hey man great video but do you think solving this with class that came with ES6 is simpler.

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

      just a bit simpler :o
      class Employee{
      constructor(name, salary){
      this.name = name;
      this.salary = salary;
      }
      getSalary(){
      return this.salary;
      }
      setSalary(salary){
      this.salary = salary;
      }
      accept(visitorFunction){
      // this is a reference to our Employee
      visitorFunction(this);
      }
      }
      const devChris = new Employee("Chris", 10000);
      console.log(devChris.getSalary());
      function ExtraSalary(emp){
      emp.setSalary(emp.getSalary() * 2);
      }
      devChris.accept(ExtraSalary);
      console.log(devChris.getSalary());

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

      @@CrassCriss thanks dude, though now I have become very much comfortable with js from the time the comment was posted

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

      @@karthickdurai2157 haha okay, just saw the video today sorry... well..maybe its useful for someone else :)

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

    Thank you

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

    What is the benefit of this over simply writing:
    devsage.salary = devsage.salary * 2;
    Right? We just changed the internal state of an object instance of a constructor function. What did we achieve?
    I'm sure there is a point to it, and it is useful in some situations.
    However, I didn't understand why we did it.

    • @SR-er6hx
      @SR-er6hx 2 роки тому

      Useful for AST walking

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

    Thank you very much. Now it is very clear how this pattern works, but I am still a bit confused about its use case.

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

      Suppose we have a method which calculates monthly salary of employees. Now company has decided to add spot bonus for few employees. In that case we can create a modifiedSalary() function.
      This function will contain a variable 'spotBonus' which gets added to calculatedSalary of employees.
      In this way we don't need to modify existing class/ function.

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

    var DevSage = new Employee('DevSage',10000);
    console.log(DevSage.getSalary()); // 1000
    function doubleSalary(DevSage){
    DevSage.setSalary(DevSage.getSalary() *2) ;
    }
    doubleSalary(DevSage);
    console.log(DevSage.getSalary()); //2000
    We can also call this way,
    Can you please telll me why do we need accept method here ?

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

      I suppose either way could work. I don't see any major advantage. The Visitor pattern is not very widely used/known in Javascript but in other languages there may be good reasons to use it.

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

      @@DevSage Thank you

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

    How do you feel about making a video on javascript prototypes?

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

      Hmm.. I will definitely consider it!

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

      I think they are obsolete, considering that classes are introduced, and if you worry about compatibility, there's transpilers

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

      @@ishdx9374 I don't think so. ES6 class is sugar syntax, it main concept is from prototype inheritance. So, it’s good to know the prototype.

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

      @@mfhsieh6858 well, I meant that they are obsolete in a way that they aren't that interesting to a programmer any more, and JS can do some optimizations on classes because it knows what you are doing

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

    It does resembles decorator pattern.

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

    lowkey thought this was a fireship video