-
Notifications
You must be signed in to change notification settings - Fork 0
/
Observer.cs
62 lines (47 loc) · 1.25 KB
/
Observer.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Singleton
{
//Idea: Think of a game where there are many players, and whenever the game master says,
//"Game over," all the players need to stop.The game master is like the "subject," and the players
//are the "observers."
//When the subject changes (game master says something), all observers get notified.
internal class Observer
{
}
public class GameMaster
{
private List<IPlayer> players = new List<IPlayer>();
public void RegisterPlayer(IPlayer player)
{
players.Add(player);
}
public void GameOver()
{
foreach (var player in players)
{
player.StopPlaying();
}
}
}
public interface IPlayer
{
void StopPlaying();
}
public class Player : IPlayer
{
public string Name { get; set; }
public Player(string name)
{
Name = name;
}
public void StopPlaying()
{
Console.WriteLine($"Stop Playing Player : {Name}");
}
}
}