-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTestObserverPattern.cs
More file actions
43 lines (36 loc) · 1.2 KB
/
TestObserverPattern.cs
File metadata and controls
43 lines (36 loc) · 1.2 KB
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
43
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BehavioralPatterns.Observer
{
public class TestObserverPattern
{
public static void Run()
{
WeatherData data = new WeatherData(13);
CurrentConditionDisplay display1 = new CurrentConditionDisplay("Display1", data);
// OUTPUT --> Display1 - Temperature: 13
CurrentConditionDisplay display2 = new CurrentConditionDisplay("Display2", data);
// OUTPUT --> Display2 - Temperature: 13
// update the temperature.
data.Temperature = 14;
// Display 1 and 2 show the new temperature
// OUTPUT:
/* Weather data is updated
Display1 - Temperature: 14
Weather data is updated
Display2 - Temperature: 14
*/
data.removeObserver(display2);
data.Temperature = 15;
// Only Display 1 shows the new temperature
// OUTPUT:
/* Weather data is updated
Display1 - Temperature: 15
*/
Console.ReadLine();
}
}
}