Chain Of Responsibility 🔐(Middleware Design Pattern)

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ก.ค. 2024
  • Design Patterns in Php ~ Lesson 1: Chain of Responsibility (Aka how "middleware" is implemented)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The chain of responsibility design pattern is one of my absolute favorite software architecture concepts.
    One of my largest pet peeves is nested indents....I avoid them with my life!
    The chain of responsibility can take a whole bunch of nested if statements and flatten them out by abstracting the conditional logic to classes.
    On top of getting us out of indented if hell (Actual hell if you as me), the chain of responsibility design pattern is PHENOMENAL for validating http requests being sent into our application.
    It allows us easily extend, replace, remove, and re-order the validation that we intend to implement on an incoming request before it ever reaches our applications actual core behavior.
    The best part is that we don't have to modify existing if else code, we simply create another class. Brilliant and assists us in following the O from SOLID (Open closed principle) extraordinarily well.
    When we use the Chain of responsibility design pattern on Http Requests, especially within frameworks like Laravel, Php slim framework, Lumen, etc... we usually reference this pattern as "Middleware".
    Using the chain of responsibility pattern we are able to put a middle layer between the http request coming in and the actual ability for that request to reach parts of our application they're not supposed to.
    The Chain Of Responsibility Breakdown
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1. What "Thing" are we validating? Define it.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    class Thing
    public $id;
    public $name;
    public $email;
    2. Create an abstract class each "Link" in our "Chain of Responsibility" will extend from
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    abstract class CheckThing
    protected $nextCheck;
    public abstract function check(Thing $thing);
    public function then(CheckThing $nextCheck); // setter for $nextCheck
    public function next(Thing $thing); // Trigger the nextCheck($thing) in the chain
    3. Create Chain Links (Specific Thing Checks), Each one validates something about thing
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    class CheckId extends CheckThing
    public check(Thing $thing)
    1. check or validate id
    A. if check fails throw exception
    B. if check passes run next check by calling CheckId's next($thing)
    class CheckName extends CheckThing
    public check(Thing $thing)
    1. check or validate name
    A. if check fails throw exception
    B. if check passes run next check by calling CheckName's next($thing)
    class CheckEmail extends CheckThing
    public check(Thing $thing)
    1. check or validate email
    A. if check fails throw exception
    B. if check passes run next check by calling CheckName's next($thing)
    4. Implement the chain of responsibility design pattern
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // A. initialize thing
    $thing = new Thing;
    // B. Initialize thing checks
    $checkId = new CheckId;
    $checkName = new CheckName;
    $checkEmail = new CheckEmail;
    // C. Define the order of responsibility.
    // $checkId~then($checkName);
    // $checkName~then($checkEmail);
    // D. Trigger Initial check in our Chain Of Responsibility
    $checkId ~ check($thing);
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Clean Code Studio ~ Simplify
    Clean Code Clean Life
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    cleancode.studio
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #ChainOfResponsibility #MiddlewareDesignPattern #SoftwareDesignPatterns
  • แนวปฏิบัติและการใช้ชีวิต

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

  • @Ibrahim-fh6kv
    @Ibrahim-fh6kv 3 ปีที่แล้ว +3

    This was one of the best PHP design pattern videos on youtube. I am looking forward to your next upcoming videos on design pattern.😃

  • @FoxHousGR
    @FoxHousGR 3 ปีที่แล้ว

    Great video's, thanks to you I've finally understood my own bad coding patterns and helped me remove a few that I learned over the past few years

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

    Thanks, really enjoy the design principle tutorials in Php :)

    • @CleanCodeStudio
      @CleanCodeStudio  4 ปีที่แล้ว

      Good to hear, I really enjoy making design principle lessons in Php :)

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

    Love this content homie keep it up !

    • @CleanCodeStudio
      @CleanCodeStudio  4 ปีที่แล้ว

      Will do, thanks for the support Milan!

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

    thanks for your great content , awesome explanation : D

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

      Glad it clicked for ya ~ Chain of responsibility is a golden design pattern

  • @mamdouhzaq
    @mamdouhzaq 4 ปีที่แล้ว +2

    Thanks, love Laravel because it handles this behind the scene for us :D

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

      Same here Mamdough, same here :)
      It's pretty incredible how many design patterns Taylor and the Laravel team use behind the scenes.
      I can't tell you how many times I've been confused while learning up on a design pattern, then finally got it after seeing it implemented properly within Laravel.

    • @mamdouhzaq
      @mamdouhzaq 4 ปีที่แล้ว +3

      @@CleanCodeStudio Those guys are amazing just like u :)

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

    Great video, but I’ve seen people do the same as you demonstrate, but they call it ‘decorator pattern’. How does chain of responsibility differ from the decorator pattern?

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

      The chain-of-responsibility pattern is structurally nearly identical to the decorator pattern, the difference being that for the decorator, all classes handle the request, while for the chain of responsibility, exactly one of the classes in the chain handles the request.

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

    Can I implement this to have a chain of process and have some checks to return instead of doing a next() ?

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

      I'd recommend using a different pattern than the Chain of Responsibility for something like that.
      You could simply have an array of functions, and map through them if you want distinct values returned.
      Otherwise, if you want a single build up object that gets handed off from one function to the next you can use reduce.

  • @Semicolon..
    @Semicolon.. 4 ปีที่แล้ว +1

    very useful