Chain of responsibility design pattern in php

Let us see how Responsibility can be Unchained!
In object-oriented design, the chain-of-responsibility pattern belongs to behavioral design patterns segment. The simplest example contains a person using ATM. He’s entering pin, receiving payment and receipts etcetera.
What is behavioral design pattern? They are a type of design patterns that generally identify communicating objects.
You may ask, what kind of objects we’re talking about? These objects have common communication patterns between them. While communicating they must realize that patterns.

In a glance it seems that the definition is a bit fuzzy. When I started with ‘chain of responsibility pattern’, I’ve found it rather unclear. But thing is, once you understand the basic concepts, you’d love its potential qualities.
In this pattern one object communicates with the other and in doing so, these patterns increase flexibility in carrying out this communication.
This pattern consists of source of command objects and a series of processing objects. So we need a ‘processor’ and the ‘next’ command that actually delegates its responsibility to the other object and each processing object contains a certain type of logic that describes the types of command objects that it can handle.
It processes a part and then the rest are passed to the next processing object in the chain.
Let us assume a calculator. It adds, subtracts, multiplies and divides. Each object does its job and passes the processing job to the next one.
Consider this code.

successor = $successor;
    }
    
    public function next(Calculator $calculate) {
        if ($this->successor){
            $this->successor->check($calculate);
        }
    }
}
        
class Adds extends Calculate {
    
    public function check(Calculator $calculate) {
        
        if (!$calculate->add){
            
            throw new Exception("Addition has not been done! Please check.");
            
        }
        
        $this->next($calculate);
    }   
    
}

class Subtracts extends Calculate {
    
    public function check(Calculator $calculate) {
        
        if (!$calculate->sub){
            
            throw new Exception("Subtraction has not been done! Please check.");
            
        }
        
        $this->next($calculate);
    }   
    
}

class Mulitiply extends Calculate {
    
    public function check(Calculator $calculate) {
        
        if (!$calculate->mul){
            
            throw new Exception("Multiplicatiotion has not been done! Please check.");
            
        }
        
        $this->next($calculate);
    }   
    
}

class Divide extends Calculate {
    
    public function check(Calculator $calculate) {
        
        if (!$calculate->div){
            
            throw new Exception("Division has not been done! Please check.");
            
        }
        
        $this->next($calculate);
    }   
    
}

class Calculator {
    
    public $add = true;
    public $sub = true;
    public $mul = true;
    public $div = true;
}

$add = new Adds();

$sub = new Subtracts();

$mul = new Mulitiply();

$div = new Divide();

$add->calculateWith($sub);
$sub->calculateWith($mul);
$mul->calculateWith($div);

$add->check(new Calculator());

The first part of the code is the “Calculator” class which defines each property to be true. We assume a situation where addition, subtraction, multiplication and division properties are true. When they are true, the application would not throw any error message. But when one comes out as false, it’ll first checks which one is false and throw error message accordingly.
The first client object that performs the request and sets up the chain is ‘add’.
Look at the part of the code section.

$add = new Adds();

$sub = new Subtracts();

$mul = new Mulitiply();

$div = new Divide();

$add->calculateWith($sub);
$sub->calculateWith($mul);
$mul->calculateWith($div);

Here ‘$add’ object sets up the chain. If the property of ‘$add’ is false, error will be thrown. The chain is affected immediately.
In the middle part if ‘$mul’ or multiplication object comes out as false, the chain is affected in the middle.
We’ll see each output. In the beginning, we assume that the addition property is not true. Let us see what we get in our output.

PHP Fatal error: Uncaught Exception: Addition has not been done! Please check. in /home/hagudu/Code/php7book1/Day4/chain1.php:28
Stack trace:
#0 /home/hagudu/Code/php7book1/Day4/chain1.php(102): Adds->check(Object(Calculator))
#1 {main}
thrown in /home/hagudu/Code/php7book1/Day4/chain1.php on line 28

The very first line of the chain has been affected; so it does not proceed any more.
Let us make the subtraction property false and see the output.

PHP Fatal error: Uncaught Exception: Subtraction has not been done! Please check. in /home/hagudu/Code/php7book1/Day4/chain1.php:43
Stack trace:
#0 /home/hagudu/Code/php7book1/Day4/chain1.php(17): Subtracts->check(Object(Calculator))
#1 /home/hagudu/Code/php7book1/Day4/chain1.php(32): Calculate->next(Object(Calculator))
#2 /home/hagudu/Code/php7book1/Day4/chain1.php(102): Adds->check(Object(Calculator))
#3 {main}
thrown in /home/hagudu/Code/php7book1/Day4/chain1.php on line 43

Next we’ll make the multiplication property false and see the output.

PHP Fatal error: Uncaught Exception: Multiplicatiotion has not been done! Please check. in /home/hagudu/Code/php7book1/Day4/chain1.php:58
Stack trace:
#0 /home/hagudu/Code/php7book1/Day4/chain1.php(17): Mulitiply->check(Object(Calculator))
#1 /home/hagudu/Code/php7book1/Day4/chain1.php(47): Calculate->next(Object(Calculator))
#2 /home/hagudu/Code/php7book1/Day4/chain1.php(17): Subtracts->check(Object(Calculator))
#3 /home/hagudu/Code/php7book1/Day4/chain1.php(32): Calculate->next(Object(Calculator))
#4 /home/hagudu/Code/php7book1/Day4/chain1.php(102): Adds->check(Object(Calculator))
#5 {main}
thrown in /home/hagudu/Code/php7book1/Day4/chain1.php on line 58

You have probably noticed that the line of output has been increased as we move towards the lower segment of the chain. Inspect the stack trace. It starts with multiply checking. Then it hits the ‘next’ button and proceeds to check the previous object.

#0 /home/hagudu/Code/php7book1/Day4/chain1.php(17): Mulitiply->check(Object(Calculator))
#1 /home/hagudu/Code/php7book1/Day4/chain1.php(47): Calculate->next(Object(Calculator))

Lastly we’ll make the division property false and see the output.

PHP Fatal error: Uncaught Exception: Division has not been done! Please check. in /home/hagudu/Code/php7book1/Day4/chain1.php:73
Stack trace:
#0 /home/hagudu/Code/php7book1/Day4/chain1.php(17): Divide->check(Object(Calculator))
#1 /home/hagudu/Code/php7book1/Day4/chain1.php(62): Calculate->next(Object(Calculator))
#2 /home/hagudu/Code/php7book1/Day4/chain1.php(17): Mulitiply->check(Object(Calculator))
#3 /home/hagudu/Code/php7book1/Day4/chain1.php(47): Calculate->next(Object(Calculator))
#4 /home/hagudu/Code/php7book1/Day4/chain1.php(17): Subtracts->check(Object(Calculator))
#5 /home/hagudu/Code/php7book1/Day4/chain1.php(32): Calculate->next(Object(Calculator))
#6 /home/hagudu/Code/php7book1/Day4/chain1.php(102): Adds->check(Object(Calculator))
#7 {main}
thrown in /home/hagudu/Code/php7book1/Day4/chain1.php on line 73

Two more lines have been added to the stack trace.

This chain of responsibility pattern includes a handler. It could be either an interface or an abstract class. In our case we have made it an abstract class. It sets the successor in the chain. And the total operation has been done by a series of processing jobs. All the handlers are the part of the chain.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s