Chain of Responsibility to the Rescue!

2024 ж. 12 Мам.
10 048 Рет қаралды

Become a patron and get access to source code and exclusive live streams: / chain-of-to-81381632
Chain of Responsibility is one of the less-frequently used design patterns, but also one of the most useful patterns. It helps compose simple decision-making objects into a chain structure, so that the objects get the opportunity to decide in order of their appearance. The first object to successfully complete the task gets all the credits, and its response becomes the result of the entire chain.
That is the theory, and in this video you will learn the practical aspects of applying and implementing the Chain of Responsibility design pattern. We will address the issue of combining simple IComparer instances to build a multi-level sorting comparer object which can be used in contexts where only one IComparer object can be supplied.
Video courses on design patterns:
Design Patterns in C# Made Simple ► codinghelmet.com/go/design-pa...
Refactoring to Patterns ► codinghelmet.com/go/refactori...
Creating Objects ► codinghelmet.com/go/tactical-...
Managing Responsibilities ► codinghelmet.com/go/tactical-...
Control Flow ► codinghelmet.com/go/tactical-...
Chapters:
00:00 Intro
01:10 Understanding the need for the Chain of Responsibility
02:55 Implementing the Chain of Responsibility
05:20 Using the Chain of Responsibility
06:53 Implementing reusable partial comparers
Learn more from video courses:
Beginning Object-oriented Programming with C# ► codinghelmet.com/go/beginning...
Collections and Generics in C# ► codinghelmet.com/go/collectio...
Making Your C# Code More Object-oriented ► codinghelmet.com/go/making-yo...
Other courses at Pluralsight ► codinghelmet.com/go/pluralsight
Other courses at Udemy ► codinghelmet.com/go/udemy
Other videos on this channel you may be interested in watching:
Using GitHub Copilot to Write Complex Code | Step-by-step Tutorial ► • Using GitHub Copilot t...
Coding with GitHub Copilot - Beginner to Master | VS Code Demo ► • A Comprehensive Guide ...
What is Covariance and Contravariance in C# ► • What is Covariance and...
How to Initialize a Clean ASP.NET Core Project with Entity Framework Core and Identity ► • How to Initialize a Cl...
The Null Conundrum: A Guide to Optional Objects in C# ► • How to Avoid Null Refe...
#designpatterns #dotnet

Пікірлер
  • Become a patron and get access to source code and exclusive live streams: www.patreon.com/posts/chain-of-to-81381632

    @zoran-horvat@zoran-horvat Жыл бұрын
  • 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.

    @marklord7614@marklord7614 Жыл бұрын
  • Your pluralsight and udemy courses are fire! Thanks for all the great content!

    @LassePi@LassePi Жыл бұрын
  • Neat! A nice reminder og a useful pattern -- thanks!

    @andersjuul8310@andersjuul83103 ай бұрын
  • A very nice real-world example of the pattern in use.

    @krccmsitp2884@krccmsitp28846 ай бұрын
  • I like how practical your examples are!

    @10199able@10199able Жыл бұрын
  • 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.

    @real_logicmonk@real_logicmonk Жыл бұрын
    • He made another functional programming course on pluralsight. it's on my watch list.

      @superpcstation@superpcstation Жыл бұрын
    • Functional c# 10 - mine too :)

      @real_logicmonk@real_logicmonk Жыл бұрын
  • Great video, as always. 🙌 Greetings from InterVenture.

    Жыл бұрын
  • 1:36 - Reverse impl (video about impl reverse that run once)

    @ivandrofly@ivandrofly8 ай бұрын
  • 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. 🙂

    @therealgraeme@therealgraeme Жыл бұрын
    • 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.

      @zoran-horvat@zoran-horvat Жыл бұрын
  • 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?

    @alexlo5655@alexlo5655 Жыл бұрын
    • 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.

      @zoran-horvat@zoran-horvat Жыл бұрын
  • 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() ?

    @MrJonnis13@MrJonnis1310 ай бұрын
    • 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.

      @zoran-horvat@zoran-horvat10 ай бұрын
    • @@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 :)

      @MrJonnis13@MrJonnis1310 ай бұрын
    • @@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.

      @zoran-horvat@zoran-horvat10 ай бұрын
    • @@zoran-horvat Thanks again Zoran, I will be following your content from now on :)

      @MrJonnis13@MrJonnis1310 ай бұрын
KZhead