I feel the same thing you do... having a rather generic sounding IObserver
interface have a specific method signature that really only applies when observing WeatherData
feels icky!
I'd much rather have something like this:
public interface IObserver<T>
{
void Update(T updatedData);
}
With an observer that would look something like this (snipped some extra code here):
public class CurrentConditionDisplay : IObserver<WeatherUpdate>, IDisplay
{
public CurrentConditionDisplay(ISubject<WeatherUpdate> weatherData)
{
this.weatherData = weatherData;
this.weatherData.RegisterObserver(this);
}
public void Update(WeatherUpdate update)
{
this.temperature = update.Temperature;
this.humidity = update.Humidity;
this.pressure = update.Pressure;
}
}
And just to make myself clear, my generic T
for IObserver<T>
would be an object that encapsulates a weather update:
public WeatherUpdate
{
public float Temperature;
public float Humidity;
public float Pressure;
}
And ISubject
would have to be changed to include the generic parameter as well:
public interface ISubject<T>
{
void RegisterObserver(IObserver<T> obs);
void RemoveObserver(IObserver<T> obs);
void NotifyObservers();
}