Sunday, January 03, 2021

The Chain of Responsibility 

Let's create a PHP example:

The first thing I want to create is the interface Handler. We add the two method definitions:

public function setNext(Handler $handler): Handler;
public function handle(string $request): ?string;
Next we create a class called AbstractHandler it implements Handler and missing methods setNext and handle
we add code to setNext and to handle
$this->nextHandler = $handler;
return $handler;
and
if ($this->nextHandler) {
    return $this->nextHandler->handle($request);
}

return null;
we next add the class SquirrelHandler which extends AbstractHandler to it we add:
public function handle(string $request): ?string
{
    if ($request === "Nut") {
        return "Squirrel: I'll eat the " . $request . ".
"; } else { return parent::handle($request); } }
let's add another anamale DogHandler
public function handle(string $request): ?string
{
   if ($request === "MeatBall") {
       return "Dog: I'll eat the " . $request . ".\n";
   } else {
       return parent::handle($request);
   }
}
lastly we add the class MouseHandler and have it extends AbstractHandler and we add the code:
public function handle(string $request): ?string
{
   if ($request === "Cheese") {
       return "Mouse: I'll eat the " . $request . ".
"; } else { return parent::handle($request); } }
for our demo we go to the index.php file and add sime includes:
include_once ('Handler.php');
include_once ('AbstractHandler.php');
and we also need to add the folloing includes:
include_once('MouseHandler.php');
include_once ('SquirrelHandler.php');
include_once ('DogHandler.php');
now to the file we add the client function:
function clientCode(Handler $handler)
{
    foreach (["Nut", "Cheese", "Cup of coffee"] as $food) {
        echo "Client: Who wants the " . $food . "?
"; $result = $handler->handle($food); if ($result) { echo " " . $result; } else { echo " " . $food . " was left untouched."; } } }
next we define the following local varables:
$mouse = new MouseHandler;
$squirrel = new SquirrelHandler;
$dog = new DogHandler;
next we consider the following code to demo the chain of responsibity
$mouse->setNext($squirrel)->setNext($dog);

echo "Chain: Mouse > Squirrel > Dog

"; clientCode($mouse); echo "\n"; echo "Subchain: Squirrel > Dog

"; clientCode($squirrel);
thanks for being a part of The Ray Code. Be good...

Friday, January 01, 2021

The Chain of Responsibility

let's study the UML

In this post I want to discuss the Chain of Responsibility design pattern and how the UML is structured.

We want to couple the request of a sender to its receiver. 

If more than one object can handle the request, then the system should ascertain which object should handle it


We are given a list of objects that may handle that request and you want to be able to evaluate which object should handle the given request at runtime.







The Handler defines an interface that’s capable of handling the request.

If it can not handle request, the request is sent to the next handler

 

The concrete hander handles the request it is responsible for, if it can not handle this request, then the request is passed to the next object in the chain. 


If no concrete handler can handle the request, the request fall off the chain.


The client is the one who initiated the request...


The Strategy Design Pattern a Behavioral Pattern using C++

The Strategy Design Pattern is a behavioral design pattern that enables selecting an algorithm's implementation at runtime. Instead of i...