forked from WeihanLi/DesignPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
38 lines (28 loc) · 1.02 KB
/
Program.cs
File metadata and controls
38 lines (28 loc) · 1.02 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
using System;
namespace ObserverPattern
{
public class Program
{
public static void Main(string[] args)
{
var boss = new Boss();
var stockObserver = new StockObserver("魏关姹", boss);
var nbaObserver = new NBAObserver("易管查", boss);
boss.Attact(stockObserver);
boss.Attact(nbaObserver);
boss.Detach(stockObserver);
boss.SubjectState = "老板我胡汉三回来了";
boss.Notify();
Console.WriteLine();
var newBoss = new NewBoss(); // 通过事件来通知的新老板
var gameObserver = new GamePlayerObserver("西门", newBoss);
// 注册通知事件
newBoss.Update += stockObserver.Update;
newBoss.Update += nbaObserver.Update;
newBoss.Update += gameObserver.CloseGame;
newBoss.SubjectState = "老板我胡汉三回来了";
newBoss.Notify();
Console.ReadLine();
}
}
}