-
Book Overview & Buying
-
Table Of Contents
Mastering PHP Design Patterns
By :
Suppose we have a group of objects that together are meant to solve a problem. When one object can't solve a problem, we want the object to send the task to a different object in a given chain. This is what the Chain of Responsibility design pattern is used for.
In order to get this to work, we need a handler, which will be our Chain interface. The various objects in the chain will all implement this Chain interface.
Let's start with a simple example; an associate can purchase an asset for less than $100, a manager can purchase something for less than $500.
Our abstraction for the Purchaser interface looks like this:
<?php
interface Purchaser
{
public function setNextPurchaser(Purchaser $nextPurchaser): bool;
public function buy($price): bool;
}
Our first implementation is the Associate class. Quite simply, we implement the setNextPurchaser function so that it will set the nextPurchaser class property to the next...