-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuyer.cs
More file actions
39 lines (33 loc) · 893 Bytes
/
Buyer.cs
File metadata and controls
39 lines (33 loc) · 893 Bytes
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
using System;
using System.Collections.Generic;
namespace DesignPatterns.Observer
{
using GoodsReminder = Dictionary<string, int>;
class Buyer : IObserver<GoodsReminder>
{
GoodsReminder _stock = new();
readonly IObservable<GoodsReminder> _observable;
public Buyer(IObservable<GoodsReminder> observable)
{
observable.Register(this);
_observable = observable;
}
public void Update(GoodsReminder value)
{
foreach (var entry in value)
{
if (_stock.TryGetValue(entry.Key, out int amount) &&
amount < 1 && entry.Value > 0)
{
Console.WriteLine($"[Buyer] They just shipped {entry.Key}, time to go shopping.");
}
}
_stock = value;
}
public void Leave()
{
Console.WriteLine($"[Buyer] I'm not shopping here anymore.");
_observable.Unregister(this);
}
}
}