Skip to content

Instantly share code, notes, and snippets.

@kamekoopa
Created July 19, 2011 13:40
Show Gist options
  • Save kamekoopa/1092364 to your computer and use it in GitHub Desktop.
Save kamekoopa/1092364 to your computer and use it in GitHub Desktop.
traitを使ったObserverパターン
<?php
interface IListener {
public function notify();
}
class HogeListener implements IListener{
public function notify(){
echo "HogeHoge!!\n";
}
}
class HomuListener implements IListener{
public function notify(){
echo "HomuHomu!!\n";
}
}
trait Notifier {
protected $listeners = array();
protected function addListener(IListener $listener){
$this->listeners[] = $listener;
return $this;
}
protected function clearListener(){
$this->listeners = array();
return $this;
}
protected function notify(){
foreach($this->listeners as $listener){
$listener->notify();
}
}
}
class Something {
use Notifier;
public function __construct(){
$this
->addListener(new HogeListener())
->addListener(new HomuListener())
;
}
public function doSomething(){
echo "do!!\n";
$this->notify();
}
public function clear(){
$this->clearListener();
}
}
$something = new Something();
$something->doSomething();
$something->clear();
$something->doSomething();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment