-
Notifications
You must be signed in to change notification settings - Fork 617
/
WeatherSupplier.cs
42 lines (37 loc) · 1.13 KB
/
WeatherSupplier.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Collections.Generic;
namespace ObserverPattern
{
class WeatherSupplier : IObservable<Weather>
{
private readonly List<IObserver<Weather>> _observers;
private List<Weather> Screens { get; }
private List<Weather> GetScreens()
{
return Screens;
}
public WeatherSupplier()
{
_observers = new List<IObserver<Weather>>();
Screens = new List<Weather>();
}
public IDisposable Subscribe(IObserver<Weather> observer)
{
if (!_observers.Contains(observer))
{
_observers.Add(observer);
foreach (var item in GetScreens())
{
observer.OnNext(item);
}
}
return new Unsubscriber<Weather>(_observers, observer);
}
public void WeatherConditions(double temp = 0, double humd = 0, double pres = 0)
{
var conditions = new Weather(humd, pres, temp);
foreach (var item in _observers)
item.OnNext(conditions);
}
}
}