-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
#################################### | ||
##### OBSERVER DESIGN PATTERN ##### | ||
#################################### | ||
class Model implements SplSubject | ||
{ | ||
protected $observers; | ||
|
||
/** | ||
* Store observers in SplObjectStorage | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->observers = new SplObjectStorage(); | ||
} | ||
|
||
/** | ||
* Attach observer | ||
* @param SplObserver $observer | ||
* @return void | ||
*/ | ||
public function attach(SplObserver $observer) | ||
{ | ||
$this->observers->attach($observer); | ||
} | ||
|
||
/** | ||
* Detach observer | ||
* @param SplObserver $observer | ||
* @return void | ||
*/ | ||
public function detach(SplObserver $observer) | ||
{ | ||
$this->observers->detach($observer); | ||
} | ||
|
||
/** | ||
* When model subject is updated notify the observer | ||
* @return void | ||
*/ | ||
public function notify() | ||
{ | ||
foreach ($this->observers as $observer) { | ||
$observer->update($this); | ||
} | ||
} | ||
|
||
/** | ||
* This method set any anonymous property | ||
* @param $name | ||
* @param $value | ||
* @return void | ||
*/ | ||
public function __set($name, $value) | ||
{ | ||
$this->data[$name] = $value; | ||
$this->notify(); | ||
} | ||
} | ||
|
||
|
||
class ModelObserver implements SplObserver | ||
{ | ||
/** | ||
* Notify when subject is updated | ||
* @param SplSubject $subject | ||
* @return void | ||
*/ | ||
public function update(SplSubject $subject) | ||
{ | ||
echo get_class($subject) . ' has been updated' . '<br>'; | ||
} | ||
} | ||
|
||
class Observer2 implements SplObserver | ||
{ | ||
/** | ||
* Notify when subject is udpated | ||
* @param SplSubject $subject | ||
* @return void | ||
*/ | ||
public function update(SplSubject $subject) | ||
{ | ||
echo get_class($subject) . ' has been updated' . '<br>'; | ||
} | ||
} | ||
|
||
|
||
// Instantiate the model class for 2 different objects | ||
$model1 = new Model(); | ||
$model2 = new Model(); | ||
|
||
// Instantiate the observers | ||
$modelObserver = new ModelObserver(); | ||
$observer2 = new Observer2(); | ||
|
||
// Attach the observers to $model1 | ||
$model1->attach($modelObserver); | ||
$model2->attach($observer2); | ||
|
||
// Attach the observers to $model2 | ||
$model2->attach($observer2); | ||
|
||
// Change the subject properties | ||
$model1->title = 'Ba Ba Black Ship..'; | ||
$model2->body = 'Twinkle Twinkle Little Star..'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
#################################### | ||
##### OBSERVER DESIGN PATTERN ##### | ||
#################################### | ||
abstract class AbstractSubject | ||
{ | ||
/** | ||
* Attach observer | ||
* @param AbstractObserver $observer_in | ||
* @return mixed | ||
*/ | ||
abstract function attach(AbstractObserver $observer_in); | ||
|
||
/** | ||
* Detach observer | ||
* @param AbstractObserver $observer_in | ||
* @return mixed | ||
*/ | ||
abstract function detach(AbstractObserver $observer_in); | ||
|
||
/** | ||
* Notify observer | ||
* @return mixed | ||
*/ | ||
abstract function notify(); | ||
} | ||
|
||
abstract class AbstractObserver | ||
{ | ||
/** | ||
* Subject updated | ||
* @param AbstractSubject $subject_in | ||
* @return mixed | ||
*/ | ||
abstract function update(AbstractSubject $subject_in); | ||
} | ||
|
||
class Subject extends AbstractSubject | ||
{ | ||
|
||
private $observers = array(); | ||
|
||
function __construct(){} | ||
|
||
/** | ||
* Attach observer | ||
* @param AbstractObserver $observer_in | ||
* @return mixed|void | ||
*/ | ||
function attach(AbstractObserver $observer_in) | ||
{ | ||
//could also use array_push($this->observers, $observer_in); | ||
$this->observers[] = $observer_in; | ||
} | ||
|
||
/** | ||
* Detach observer | ||
* @param AbstractObserver $observer_in | ||
* @return mixed|void | ||
*/ | ||
function detach(AbstractObserver $observer_in) | ||
{ | ||
//$key = array_search($observer_in, $this->observers); | ||
foreach ($this->observers as $okey => $oval) { | ||
if ($oval == $observer_in) { | ||
unset($this->observers[$okey]); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Notify observer | ||
* @return mixed|void | ||
*/ | ||
function notify() | ||
{ | ||
foreach ($this->observers as $obs) { | ||
$obs->update($this); | ||
} | ||
} | ||
|
||
/** | ||
* Update subject property and notify observer | ||
* @param $newFavorites | ||
* @return void | ||
*/ | ||
function updateFavorites($newFavorites) | ||
{ | ||
$this->favorites = $newFavorites; | ||
$this->notify(); | ||
} | ||
|
||
/** | ||
* Get updated info | ||
* @return mixed | ||
*/ | ||
function getFavorites() | ||
{ | ||
return $this->favorites; | ||
} | ||
|
||
} | ||
|
||
class Observer extends AbstractObserver | ||
{ | ||
/** | ||
* Notify when subject is updated | ||
* @param AbstractSubject $subject | ||
* @return mixed|void | ||
*/ | ||
public function update(AbstractSubject $subject) | ||
{ | ||
echo get_class($subject) . ' has been updated' . '<br>'; | ||
} | ||
} | ||
|
||
// Instantiate the subject | ||
$subject = new Subject(); | ||
|
||
// Instantiate the observers | ||
$observer = new Observer(); | ||
|
||
// Attach the observers to subject | ||
$subject->attach($observer); | ||
$subject->updateFavorites('Hello Subject Updated'); | ||
$subject->updateFavorites('Hello Subject Again Updated'); | ||
|
||
// Detach the observer | ||
$subject->detach($observer); | ||
|
||
?> |