Skip to content

Instantly share code, notes, and snippets.

@tkouleris
Created November 23, 2016 19:36
Show Gist options
  • Select an option

  • Save tkouleris/f36807ccc0fed072a36aeeeeb7aed15a to your computer and use it in GitHub Desktop.

Select an option

Save tkouleris/f36807ccc0fed072a36aeeeeb7aed15a to your computer and use it in GitHub Desktop.
Chain of responsibility pattern example
<?php
/*
Chain of Responsibility Pattern - Wiki
---------------------------------------
* In object-oriented design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and * a series of processing objects.[1] Each processing object contains logic that defines the types of command objects that it can
* handle; the rest are passed to the next processing object in the chain. A mechanism also exists for adding new processing bjects * to the end of this chain.In a variation of the standard chain-of-responsibility model, some handlers may act as
* dispatchers,capable of sending commands out in a variety of directions, forming a tree of responsibility. In some cases, this
* can occur recursively, with processing objects calling higher-up processing objects with commands that attempt to solve some
* smaller part of the problem; in this case recursion continues until the command is processed, or the entire tree has been
* explored. An XML interpreter might work in this manner.
*
* This pattern promotes the idea of loose coupling.
*/
//The abstrac Chain
abstract class BookChain{
private $nextBookChain = null;
public function getStore($request){}
public function nextStore(BookChain $nextBookChain){}
}
class SyntagmaStore extends BookChain{
public function nextStore(BookChain $next){
$this->nextBookChain = $next;
}
public function getStore($store){
if($store == "syntagma")
echo "This is the Syntagma book store\r\n";
else
$this->nextBookChain->getStore($store);
}
}
class SounioStore extends BookChain{
public function nextStore(BookChain $next){
$this->nextBookChain = $next;
}
public function getStore($store){
if($store == "sounio")
echo "This is the Sounio book store\r\n";
else
$this->nextBookChain->getStore($store);
}
}
class OmonoiaStore extends BookChain{
public function nextStore(BookChain $nextBookChain){
$this->nextBookChain = $nextBookChain;
}
public function getStore($store){
if($store == "omonoia")
echo "This is the Omonoia book store\r\n";
else
echo "No Store\r\n";
}
}
//Create instance of everything
$bookStore1 = new SyntagmaStore();
$bookStore2 = new SounioStore();
$bookStore3 = new OmonoiaStore();
//Define the chain
$bookStore1->nextStore($bookStore2);
$bookStore2->nextStore($bookStore3);
// This will run the getStore method of SounioStore
$bookStore1->getStore("sounio");
// This will run the getStore method of SyntagmaStore
$bookStore1->getStore("syntagma");
// This will run the getStore method of OmonoiaStore
$bookStore1->getStore("omonoia");
//When everything fails
$bookStore1->getStore("random strint");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment