//-----OBSERVER PATTERN-----//
MainFile.cs
using System;
namespace Pattern_Weather
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("-----Push-----");
Push.WeatherData wd1 = new Push.WeatherData();
Push.StaticDisplay sd1 = new Push.StaticDisplay(wd1);
Push.GraphicDisplay dd1 = new Push.GraphicDisplay(wd1);
wd1.SetMeasurements(3, 2, 5);
wd1.SetMeasurements(1, 2, 3);
wd1.SetMeasurements(2, 3, 4);
Console.WriteLine("-----Pull-----");
Pull.WeatherData wd = new Pull.WeatherData();
Pull.StaticDisplay sd = new Pull.StaticDisplay(wd);
Pull.GraphicDisplay dd = new Pull.GraphicDisplay(wd);
wd.SetMeasurements(3, 2, 5);
wd.SetMeasurements(1, 2, 3);
wd.SetMeasurements(2, 3, 4);
}
}
}
PullMethod.cs
using System;
using System.Collections;
namespace Pattern_Weather.Pull
{
#region Interfaces
public interface ISubject
{
void RegisterObject(IObserver obj);
void UnRegisterObject(IObserver obj);
void NotifyObserver();
void SetChanged();
float GetTemp();
float GetHumidity();
float GetPressure();
}
public interface IObserver
{
void Update(ISubject subject);
}
public interface IDisplay
{
void Display();
}
#endregion
public class WeatherData : ISubject
{
ArrayList observers;
float temp = 1;
float humidity = 2;
float pressure = 3;
bool changed = false;
public WeatherData()
{
observers = new ArrayList();
}
#region ISubject Members
public void RegisterObject(IObserver obj)
{
observers.Add(obj);
}
public void UnRegisterObject(IObserver obj)
{
int i = observers.IndexOf(obj);
if (i >= 0) observers.RemoveAt(i);
}
public void SetChanged()
{
changed = true;
}
public void NotifyObserver()
{
if (changed)
{
foreach (IObserver obj in observers)
{
obj.Update(this);
}
}
}
#endregion
public void MeasurementChanged()
{
SetChanged();
NotifyObserver();
}
public void SetMeasurements(float temp, float humidity, float pressure)
{
this.temp = temp;
this.humidity = humidity;
this.pressure = pressure;
MeasurementChanged();
}
public float GetTemp()
{
return this.temp;
}
public float GetHumidity()
{
return this.humidity;
}
public float GetPressure()
{
return this.pressure;
}
}
public class StaticDisplay : IObserver, IDisplay
{
float temp;
float humidity;
float pressure;
private ISubject weatherData;
public StaticDisplay(ISubject obj)
{
weatherData = obj;
weatherData.RegisterObject(this);
}
#region IDisplay Members
public void Display()
{
Console.WriteLine("Display from :{0}, Temp: {1}, Humidity: {2}, Pressure: {3}", "StaticDisplay", temp, humidity, pressure);
}
#endregion
#region IObserver Members
public void Update(ISubject subject)
{
weatherData = (WeatherData)subject;
this.temp = weatherData.GetTemp();
this.humidity = weatherData.GetHumidity();
this.pressure = weatherData.GetPressure();
Display();
}
#endregion
}
public class GraphicDisplay : IObserver, IDisplay
{
float temp;
float humidity;
float pressure;
private ISubject weatherData;
public GraphicDisplay(ISubject obj)
{
weatherData = obj;
weatherData.RegisterObject(this);
}
#region IObserver Members
public void Update(ISubject subject)
{
weatherData = (WeatherData)subject;
this.temp = weatherData.GetTemp();
this.humidity = weatherData.GetHumidity();
this.pressure = weatherData.GetPressure();
Display();
}
#endregion
#region IDisplay Members
public void Display()
{
Console.WriteLine("Display from :{0}, Temp: {1}, Humidity: {2}, Pressure: {3}", "GraphicDisplays", temp, humidity, pressure);
}
#endregion
}
}
PushMethod.cs
using System;
using System.Collections;
namespace Pattern_Weather.Push
{
public class WeatherData : ISubject
{
ArrayList observers;
float temp = 1;
float humidity = 2;
float pressure = 3;
public WeatherData()
{
observers = new ArrayList();
}
#region ISubject Members
public void NotifyObject()
{
foreach (IObserver obj in observers)
{
obj.Update(this.temp, this.humidity, this.pressure);
}
}
public void RegisterObject(IObserver obj)
{
observers.Add(obj);
}
public void UnRegisterObject(IObserver obj)
{
int i = observers.IndexOf(obj);
if (i >= 0) observers.RemoveAt(i);
}
#endregion
public void MeasurementChanged()
{
NotifyObject();
}
public void SetMeasurements(float temp, float humidity, float pressure)
{
this.temp = temp;
this.humidity = humidity;
this.pressure = pressure;
MeasurementChanged();
}
}
public class StaticDisplay : IObserver, IDisplay
{
float temp;
float humidity;
float pressure;
private ISubject weatherData;
public StaticDisplay(ISubject obj)
{
weatherData = obj;
weatherData.RegisterObject(this);
}
#region IObserver Members
public void Update(float temp, float humidity, float pressure)
{
this.temp = temp;
this.humidity = humidity;
this.pressure = pressure;
Display();
}
#endregion
#region IDisplay Members
public void Display()
{
Console.WriteLine("Display from :{0}, Temp: {1}, Humidity: {2}, Pressure: {3}", "StaticDisplay", temp, humidity, pressure);
}
#endregion
}
public class GraphicDisplay : IObserver, IDisplay
{
float temp;
float humidity;
float pressure;
private ISubject weatherData;
public GraphicDisplay(ISubject obj)
{
weatherData = obj;
weatherData.RegisterObject(this);
}
#region IObserver Members
public void Update(float temp, float humidity, float pressure)
{
this.temp = temp;
this.humidity = humidity;
this.pressure = pressure;
Display();
}
#endregion
#region IDisplay Members
public void Display()
{
Console.WriteLine("Display from :{0}, Temp: {1}, Humidity: {2}, Pressure: {3}", "GraphicDisplays", temp, humidity, pressure);
}
#endregion
}
#region Interfaces
public interface ISubject
{
void NotifyObject();
void RegisterObject(IObserver obj);
void UnRegisterObject(IObserver obj);
}
public interface IObserver
{
void Update(float temp, float humidity, float pressure);
}
public interface IDisplay
{
void Display();
}
#endregion
}