设计模式---中介者模式(打出来是“终结者.......”)

设计模式---中介者模式(打出来是“终结者.......”)

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
abstract class FBIAgent
{
public abstract void SendMessage(string m);

public void ReadMessage(string m)
{
Console.WriteLine(m);
}
}

class AgentInRussia : FBIAgent
{
private FBICenter center;

public AgentInRussia(FBICenter f)
{
center = f;
}

public override void SendMessage(string m)
{
center.Send(this, m);
}
}

class AgentInEngland : FBIAgent
{
private FBICenter center;

public override void SendMessage(string m)
{
center.Send(this, m);
}

public AgentInEngland(FBICenter f)
{
center = f;
}
}

abstract class FBICenter
{
public abstract void Send(FBIAgent f, string m);
}

class RealFBICenter : FBICenter
{
private FBIAgent agentinrussia;
private FBIAgent agentinengland;

public FBIAgent russia
{
set { agentinrussia = value; }
}

public FBIAgent england
{
set { agentinengland = value; }
}

public override void Send(FBIAgent f, string m)
{
if (f == agentinengland)
{
agentinrussia.ReadMessage(m);
}
else
{
agentinengland.ReadMessage(m);
}
}
}

class Client
{
public static void Main()
{
RealFBICenter fc = new RealFBICenter();
AgentInEngland eng = new AgentInEngland(fc);
AgentInRussia rus = new AgentInRussia(fc);
fc.england = eng;
fc.russia = rus;
eng.SendMessage("我在英国搞情报,hia~hia~");
rus.SendMessage("我在俄罗斯搞情报,xia~ixa~");
Console.Read();
}
}
}