Skip to content

Instantly share code, notes, and snippets.

@66Ton99
Created September 18, 2011 18:54
Show Gist options
  • Save 66Ton99/7721023db8e8d8a49f82 to your computer and use it in GitHub Desktop.
Save 66Ton99/7721023db8e8d8a49f82 to your computer and use it in GitHub Desktop.
Chain of Responsibility
<?php
class Handler
{
protected $next;
protected $id;
protected $limit;
public function getLimit()
{
return $this->limit;
}
public function handler($id, Handler $handler = null)
{
$this->id = $id;
$this->limit = $id * 1000;
$this->next = $handler;
}
public function handleRequest($data)
{
if ($this->getLimit() > $data) {
return "Request for " . $data . " handled at level " . $this->id;
} elseif (null !== $this->next) {
return $this->next->HandleRequest($data);
} else return ("Request for " . $data . " handled BY DEFAULT at level " . $this->id);
}
}
function echoLn($var)
{
echo "{$var}<br />\n";
}
$start = null;
for ($i = 5; $i > 0; $i--)
{
echoLn("Handler " . $i . " deals up to a limit of " . ($i * 1000));
$start = new Handler($i, $start);
}
foreach (array(50, 2000, 1500, 10000, 175, 4500) as $i)
{
echoLn($start->handleRequest($i));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment