{ echo $this->name . '->' . __METHOD__ . "();\n"; foreach ($this->observers as $key => $observer) { $observer->update($this); } } function getName() { return $this->name; } function contains($obj) { return $this->observers->contains($obj); } } $sub = new SubjectImpl(); $ob1 = new ObserverImpl("ob1"); $ob2 = new ObserverImpl("ob2"); $ob3 = new ObserverImpl("ob3"); var_dump($sub->contains($ob1)); $sub->attach($ob1); var_dump($sub->contains($ob1)); $sub->attach($ob1); $sub->attach($ob2); $sub->attach($ob3); var_dump($sub->count()); $sub->notify(); $sub->detach($ob3); var_dump($sub->count()); $sub->notify(); $sub->detach($ob2);
unset($this->observers[$idx]); } } function notify() { echo '$sub->' . __METHOD__ . "();\n"; foreach ($this->observers as $observer) { $observer->update($this); } } function getName() { return $this->name; } } $sub = new SubjectImpl(); $ob1 = new ObserverImpl("ob1"); $ob2 = new ObserverImpl("ob2"); $ob3 = new ObserverImpl("ob3"); $sub->attach($ob1); $sub->attach($ob1); $sub->attach($ob2); $sub->attach($ob3); $sub->notify(); $sub->detach($ob3); $sub->notify(); $sub->detach($ob2); $sub->detach($ob1); $sub->notify(); $sub->attach($ob3); $sub->notify();