Chain of Responsibility to the Rescue!

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ธ.ค. 2024

ความคิดเห็น • 24

  • @zoran-horvat
    @zoran-horvat  ปีที่แล้ว +1

    Become a patron and get access to source code and exclusive live streams: www.patreon.com/posts/chain-of-to-81381632

  • @marklord7614
    @marklord7614 2 ปีที่แล้ว +16

    One of the biggest problems developers face with learning designed patterns is finding practical use cases. I can't recall seeing a more practical use case for a design pattern than the one you just presented. Congratulations. This is great content.

    • @sulmarpl
      @sulmarpl 5 หลายเดือนก่อน

      Here you go, one more real world example: I use Chain of Responsibility to process email messages. Each link is responsible for a different processing element: extract data from body, tax number validation, finding the customer in db.

  • @LassePi
    @LassePi 2 ปีที่แล้ว +12

    Your pluralsight and udemy courses are fire! Thanks for all the great content!

  • @real_logicmonk
    @real_logicmonk 2 ปีที่แล้ว +3

    Simple explanations! Watched your Course on functional programming on pluralsight in 2019 or so, changed my life! Program in go these days but still these videos are so enjoyable and informative that cant pass on.

    • @superpcstation
      @superpcstation 2 ปีที่แล้ว +1

      He made another functional programming course on pluralsight. it's on my watch list.

    • @real_logicmonk
      @real_logicmonk 2 ปีที่แล้ว

      Functional c# 10 - mine too :)

  • @krccmsitp2884
    @krccmsitp2884 ปีที่แล้ว +1

    A very nice real-world example of the pattern in use.

  • @AhmadMazen-i9t
    @AhmadMazen-i9t 5 หลายเดือนก่อน +1

    Really That is amazing explain , i have searched about videos explain Design patterns to understand it until i found and watch your videos
    really it were Awesome
    thank you too much.

  • @10199able
    @10199able 2 ปีที่แล้ว +1

    I like how practical your examples are!

  • @PieJee1
    @PieJee1 หลายเดือนก่อน +1

    I use chain of responsibility a lot! It is the easiest way to avoid making god objects that know everything. I did learn of the pattern before I knew how it was called when working with Spring Framework and Symfony, which often uses a variation where you first call supports() method that returns a boolean and if it return true you call the aggregate method.
    The only thing you can do wrong with this pattern is that one of the chains is relying on a state change in a previous chain as those dependencies are easy to miss. I prefer a chain to be functional and immutable.

  • @ivandrofly
    @ivandrofly ปีที่แล้ว

    1:36 - Reverse impl (video about impl reverse that run once)

  •  2 ปีที่แล้ว +1

    Great video, as always. 🙌 Greetings from InterVenture.

  • @therealgraeme
    @therealgraeme ปีที่แล้ว

    Really interesting video - thanks for sharing. Also, a very good and practical example. I was following along, but used Java 17 for the implementation. To my surprise, I realised that Java already includes the Chain of Responsibility in its Comparator class, via the `.thenComparing()` method. Nice. 🙂

    • @zoran-horvat
      @zoran-horvat  ปีที่แล้ว

      Now that you said it, I think I remember it does. Java has one practical feature that i like a lot - functional interfaces. So many interfaces only expose one method, and the ability to use lambdas and interfaces interchangeably is really useful.

  • @andersjuul8310
    @andersjuul8310 10 หลายเดือนก่อน +1

    Neat! A nice reminder og a useful pattern -- thanks!

  • @alexlo5655
    @alexlo5655 2 ปีที่แล้ว

    Hi, Thank u for the interesting video. Should we mentioned that the same can be achieved using LINQ? If yes, should we consider LINQ chaining as the Chain of Responsibility?

    • @zoran-horvat
      @zoran-horvat  ปีที่แล้ว

      If the nodes are organized into an IEnumerable, then LINQ is a viable option. Though, you must take care to construct a correct expression. In the example from the video, nodes are NOT ordered into a sequence.

  • @MrJonnis13
    @MrJonnis13 ปีที่แล้ว

    Nice and practical video.
    However, in C# this can happen using linq. Is linq actually doing an implementation of "Chain of Responsibility" ?
    Or it the the Comparer itself that is "chained" and it be used both in List.Sort and IEnumerable.OrderBy() ?

    • @zoran-horvat
      @zoran-horvat  ปีที่แล้ว

      It depends on what you mean by "doing an implementation". If you consider the example implementation from the GoF pattern, then it is only one possibility. It is far from being the only one, let alone the best implementation one can come up with.
      In my opinion, design patterns are not their implementations, but the externally visible interaction, usually - but not always - implemented via object injection.
      In the case of the Chain of Responsibility, there are two principal implementations that correspond to linked list vs. array list, with all pros and cons of both approaches.
      Using LINQ operators such as SkipWhile, First, etc. makes it no different than the CoR implementation from the GoF book, which is based on object chaining.
      So, the answer to your question does LINQ implement CoR is (in precise terms): no, it doesn't. But the caller could not tell the difference anyway, due to encapsulation.

    • @MrJonnis13
      @MrJonnis13 ปีที่แล้ว

      @@zoran-horvat Thank you Zoran for you answer. It totally makes sense your "point of you" about what design patterns really are and teach us this way of thinking.
      I already bought your Udemy course on this topic and I look forward to studying it in detail.
      How would you do the the sorting of a collection in C# ? Would you use the linq support to "Chain' method calls like "OrderByDescending(comparer1).ThenBy(comparer2)..."
      *OR* "OrderBy(comparer1.Then(comparer2))" which is your example of CoR (let's imagine that we don't want to use List.Sort())?
      Is it a matter of preference or what ?
      And by the way, I am not aware what the example implementation from the GoF book is :)

    • @zoran-horvat
      @zoran-horvat  ปีที่แล้ว

      @@MrJonnis13 The CoR implementation in GoF is literally a linked list of objects. I have used CoR several times in practice and I always flattened that into a list for performance gains (which were measurable).
      Regarding sorting, from what I have seen in the LINQ source code, OrderBy and ThenBy methods are heavily optimized and I see no reason to avoid them. My guess is that they could even perform better than the custom chained comparer. The reason for having the chained comparer is for the sake of sorting methods that can only accept one IComparer, such as the List's Sort method.

    • @MrJonnis13
      @MrJonnis13 ปีที่แล้ว +1

      @@zoran-horvat Thanks again Zoran, I will be following your content from now on :)

  • @janoslaszlo3272
    @janoslaszlo3272 3 หลายเดือนก่อน +1

    wonderful