The Chain of Responsibility Pattern Explained & Implemented | Behavioral Design Patterns | Geekific

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

КОМЕНТАРІ • 32

  • @geekific
    @geekific  2 роки тому +20

    Thanks to @Michal Dudek in the comments below, at around 6:40 the code on the main class should be the below (it is updated on GitHub):
    Handler handler = new UserExistsHandler(database);
    handler.setNextHandler(new ValidPasswordHandler(database))
    .setNextHandler(new RoleCheckHandler());
    AuthService service = new AuthService(handler);

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

      Hi! I have a question. At around 6:40 shouldn't it instead be
      Handler handler = new UserExistsHandler(database);
      handler.setNextHandler(
      new ValidPasswordHandler(database)
      .setNextHandler(new RoleCheckHandler())
      );
      After all we're defining a next handler to the UserExistsHandler, which is ValidPasswordHandler, but then we have to set a next Handler to ValidPasswordHandler, not UserExistsHandler.
      The way your code is, we're just overriding the value of the nextHandler on UserExistsHandler is it not?

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

      @@danielfernandes1 4:35 ;)

  • @Stikmanas
    @Stikmanas 11 місяців тому +6

    you are the greatest man, made a module in university 10x easier, I love you man

  • @elyakimlev
    @elyakimlev 2 роки тому +8

    Thanks for these clear explanations. I'm actually a programmer several years now (mobile game industry), but where I have been working, we haven't had any strict rules so everybody just implemented things as they saw fit. Now that I'm thinking of moving to another company, I realize I lack some fundamental programming skills and your lessons are quite helpful. Clearer than other tutorials I've seen. Thanks!

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

      I am so glad I can be of help! Good luck with your new role :)

  • @mikooovsky
    @mikooovsky 2 роки тому +13

    Great tutorial! But I think there is a little mistake in the main method in 6:40. Since the 'setNextHandler' method returns the handler that we passed in, so when creating an instance of the AuthService, we pass the last handler from the entire Chain (in this case RoleCheckHandler) to its constructor, so the entire authentication flow doesn't work properly, because it executes only RoleCheckHandler. I guess we should assign UserExistsHandler to the variable 'handler'. Then using this variable, we should set the next handlers using the 'setNextHandler' method, and pass the 'handler' variable to the AuthService constructor. Am I correct?

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

      Very very nice! Well done bro! The code is updated on GitHub, however can't remember what happened at the time and why it figures this way in the video! xD Thanks man :)

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

      I came here to say this.

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

      I found the same issue, therefore I just declared the Handler at 1 line and chained the other handlers to it at a new one.

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

    Great job! Thank you ❤

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

    These videos are fantastic!

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

    Thanks, for the effort creating those well explained videos and for the well choosen examples

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

    nice content bro. I appreciate that !

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

    Great video

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

    It reminds me of the old Linked List in C++ where you had a serf referencing pointer [Node] that points to the Next->Node.

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

    How do you insert, remove or reorder handlers dynamically? Each handler only has a reference to the next one

  • @alilat.tech.dz.advisor
    @alilat.tech.dz.advisor Рік тому

    Thank you for the this amazing videos ! you are the best Sir !

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

      My pleasure! Glad I can be of help :)

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

    thank you

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

    thank you😍😍

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

    Why chain of responsibility is a behaviour design pattern? Plz ans me.

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

      Behavioral Patterns are concerned with communication and assignment of responsibilities between objects as the program is running, and the CoR is all about that! It creates several handlers each having a specific responsibility, and the behavior of each handler (execute, pass to next or stop) is determined by the request passed-in by the user at runtime. Hope this answers it!

  • @cparks1000000
    @cparks1000000 Рік тому +6

    I realize that this is outside the scope of the video; however, it's poor security protocol to tell a (possibly malicious) user weather or not the username exists. This gives the adversary extra information to use in the attack. If you send, "wrong password" the adversary now knows that the username exists and they should try more passwords. If you just send, "Invalid login information," the adversary doesn't know if the username is wrong or the password is wrong. This is particularly an issue when people reuse passwords and/or usernames (or variants thereof).

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

      In this video we are trying to focus on explaining and understanding the pattern in question, but yes you are right :)

    • @Kamil-lm7pi
      @Kamil-lm7pi 6 днів тому

      When creating an account the user needs an access to the information whether a certain username exists because if it does the user has to use a different one. So this just makes the information already public.

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

    i am sorry but i try to implement your code , i understood everything but this setNextHandler is not right becouse you will always set the last handler in witch case you will have only one handler

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

      Yes, there is a pinned comment for it, and it is updated on GitHub :)

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

      @@geekific sorry man , didnt thank you a lot for this tutorial 😁

  • @siddhanthsridhar4742
    @siddhanthsridhar4742 8 місяців тому

    pesu students thumbs up if here

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

    Hames Gosling