/** * */ public function testObserverCanBeAttachedAndDetach() { $subject = new Subject(); $observerA = $this->getMockForAbstractClass('\\SplObserver'); $subject->attach($observerA); $observerB = $this->getMockForAbstractClass('\\SplObserver'); $subject->attach($observerB); $subject->detach($observerB); $this->assertContains($observerA, $subject->getObservers()); $this->assertNotContains($observerB, $subject->getObservers()); }
public function __construct() { $subject = new Subject(); $o1 = new Observer1(); $o2 = new Observer2(); $o3 = new Observer3(); $subject->setData('Data for each 3 attached observers!'); $subject->attach($o1); $subject->attach($o2); $subject->attach($o3); $subject->notify(); $subject->detach($o3); $subject->setData('Data not for 3rd detached one!'); $subject->notify(); }
<?php namespace Observer; require_once 'AbstractObserver.php'; require_once 'AbstractSubject.php'; require_once 'Observer.php'; require_once 'Subject.php'; $patternGossiper = new Subject(); $patternGossipFan = new Observer(); $patternGossiper->attach($patternGossipFan); $patternGossiper->updateFavorites('abstract factory, decorator, visitor'); $patternGossiper->updateFavorites('abstract factory, observer, decorator'); $patternGossiper->detach($patternGossipFan); $patternGossiper->updateFavorites('abstract factory, observer, paisley');
/** * Same as {@link Subject::detach}, but it takes only one argument * and that argument must be a class which implements the SplObserver interface. */ public function detach(SplObserver $obj) { return parent::detach($obj); }