usingSystem;usingSystem.Collections;namespaceWikipedia.Patterns.Strategy{// IObserver --> interface for the observerpublicinterfaceIObserver{// called by the subject to update the observer of any change// The method parameters can be modified to fit certain criteriavoidUpdate(stringmessage);}publicclassSubject{// use array list implementation for collection of observersprivateArrayListobservers;// constructorpublicSubject(){observers=newArrayList();}publicvoidRegister(IObserverobserver){// if list does not contain observer, addif(!observers.Contains(observer)){observers.Add(observer);}}publicvoidDeregister(IObserverobserver){// if observer is in the list, removeif(observers.Contains(observer)){observers.Remove(observer);}}publicvoidNotify(stringmessage){// call update method for every observerforeach(IObserverobserverinobservers){observer.Update(message);}}}// Observer1 --> Implements the IObserverpublicclassObserver1:IObserver{publicvoidUpdate(stringmessage){Console.WriteLine("Observer1:"+message);}}// Observer2 --> Implements the IObserverpublicclassObserver2:IObserver{publicvoidUpdate(stringmessage){Console.WriteLine("Observer2:"+message);}}// Test classpublicclassObserverTester{ [STAThread]publicstaticvoidMain(){SubjectmySubject=newSubject();IObservermyObserver1=newObserver1();IObservermyObserver2=newObserver2();// register observersmySubject.Register(myObserver1);mySubject.Register(myObserver2);mySubject.Notify("message 1");mySubject.Notify("message 2");}}}

<?phpclassStudentimplementsSplObserver{protected$tipo="Student";private$nome;private$endereco;private$telefone;private$email;private$_classes=array();publicfunctionGET_tipo(){return$this->tipo;}publicfunctionGET_nome(){return$this->nome;}publicfunctionGET_email(){return$this->email;}publicfunctionGET_telefone(){return$this->nome;}function__construct($nome){$this->nome=$nome;}publicfunctionupdate(SplSubject$object){$object->SET_log("Comes from ".$this->nome.": I'm a student of ".$object->GET_materia());}}?>

class TEACHER

<?phpclassTeacherimplementsSplObserver{protected$tipo="Teacher";private$nome;private$endereco;private$telefone;private$email;private$_classes=array();publicfunctionGET_tipo(){return$this->tipo;}publicfunctionGET_nome(){return$this->nome;}publicfunctionGET_email(){return$this->email;}publicfunctionGET_telefone(){return$this->nome;}function__construct($nome){$this->nome=$nome;}publicfunctionupdate(SplSubject$object){$object->SET_log("Comes from ".$this->nome.": I teach in ".$object->GET_materia());}}?>

<?phprequire_once("teacher.class.php");require_once("student.class.php");require_once("subject.class.php");$subject=newSubject("Math");$marcus=newTeacher("Marcus Brasizza");$rafael=newStudent("Rafael");$vinicius=newStudent("Vinicius");// Include observers in the math Subject$subject->attach($rafael);$subject->attach($vinicius);$subject->attach($marcus);$subject2=newSubject("English");$renato=newTeacher("Renato");$fabio=newStudent("Fabio");$tiago=newStudent("tiago");// Include observers in the english Subject$subject2->attach($renato);$subject2->attach($vinicius);$subject2->attach($fabio);$subject2->attach($tiago);// Remove the instance "Rafael from subject"$subject->detach($rafael);// Notify both subjects$subject->notify();$subject2->notify();echo"First Subject <br />";echo"<pre>";print_r($subject->GET_log());echo"</pre>";echo"<hr>";echo"Second Subject <br />";echo"<pre>";print_r($subject2->GET_log());echo"</pre>";?>

OUTPUT

First Subject

Array (

[0] => Subject Math was included
[1] => The Student Rafael was included
[2] => The Student Vinicius was included
[3] => The Teacher Marcus Brasizza was included
[4] => The Student Rafael was removed
[5] => Comes from Vinicius: I'm a student of Math
[6] => Comes from Marcus Brasizza: I teach in Math

)

Second Subject

Array (

[0] => Subject English was included
[1] => The Teacher Renato was included
[2] => The Student Vinicius was included
[3] => The Student Fabio was included
[4] => The Student tiago was included
[5] => Comes from Renato: I teach in English
[6] => Comes from Vinicius: I'm a student of English
[7] => Comes from Fabio: I'm a student of English
[8] => Comes from tiago: I'm a student of English