Created
September 18, 2011 18:54
-
-
Save 66Ton99/7721023db8e8d8a49f82 to your computer and use it in GitHub Desktop.
Chain of Responsibility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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