Source code using System; using System.Collections.Generic; namespace Observer_Pattern_Weather_Station { publicinterfaceISubject { // the registering of the observer void registerObserver(IObserver o); // the deleting of the observer void removeObserver(IObserver o); // it is called to notify the observers // when the Subject’s state has changed void notifyObservers(); } publicinterfaceIObserver { // the values that the observers get from the Subject, push-solution void update(float temp, float humidity, float pressure); } publicinterfaceIDisplayElement { // we call it when we would like to show the display element void display(); } // the Subject interface is being implemented publicclassWeatherData : ISubject { // we add a list that contains the observers privateList<IObserver> observers; privatefloat temperature; privatefloat humidity; privatefloat pressure; public WeatherData() { // the list containing the observers is being created observers = newList<IObserver>(); } publicvoid registerObserver(IObserver o) { // when an observer registers in, we simply add it to the list observers.Add(o); } publicvoid removeObserver(IObserver o) { // when an observer would like to register out, it’s simply deleted from the list int i = observers.IndexOf(o); if (i >= 0) { observers.Remove(o); }
Source code using System; using System.Collections.Generic; namespace Observer_Pattern_Weather_Station { publicinterfaceISubject { // the registering of the observer void registerObserver(IObserver o); // the deleting of the observer void removeObserver(IObserver o); // it is called to notify the observers // when the Subject’s state has changed void notifyObservers(); } publicinterfaceIObserver { // the values that the observers get from the Subject, push-solution void update(float temp, float humidity, float pressure); } publicinterfaceIDisplayElement { // we call it when we would like to show the display element void display(); } // the Subject interface is being implemented publicclassWeatherData : ISubject { // we add a list that contains the observers privateList<IObserver> observers; privatefloat temperature; privatefloat humidity; privatefloat pressure; public WeatherData() { // the list containing the observers is being created observers = newList<IObserver>(); } publicvoid registerObserver(IObserver o) { // when an observer registers in, we simply add it to the list observers.Add(o); } publicvoid removeObserver(IObserver o) { // when an observer would like to register out, it’s simply deleted from the list int i = observers.IndexOf(o); if (i >= 0) { observers.Remove(o); }
ProgTechLectureNotes.v.1.0.1.docx