新随笔
1 abstract class Mediator
2 {
3 public abstract void Register(People people);
4 public abstract void SetMessage(string message, People from, string to);
5 }
6
7 class MatrimonialMediator:Mediator
8 {
9 private List<People> _list = new List<People>();
10 public override void Register(People people)
11 {
12 _list.Add(people);
13 }
14 public override void SetMessage(string message, People from, string to)
15 {
16 Console.WriteLine("{0}通过中介对{1}说:{2}", from.Name, to, message);
17 foreach (var item in _list)
18 {
19 if (item.Name == to)
20 {
21 item.RecevieMessage(message,from.Name);
22 break;
23 }
24 }
25 }
26 }
27
28 abstract class People
29 {
30 private string _name;
31 private Mediator _mediator;
32 public Mediator Mediator
33 {
34 get { return _mediator; }
35 }
36 public string Name
37 {
38 get { return _name; }
39 }
40 public People(string name, Mediator mediator)
41 {
42 _name = name;
43 _mediator = mediator;
44 _mediator.Register(this);
45 }
46 public abstract void SetMessage(string message,string to);
47 public abstract void RecevieMessage(string message,string from);
48 }
49
50 class Man:People
51 {
52 public Man(string name,Mediator mediator)
53 : base(name, mediator)
54 { }
55 public override void SetMessage(string message,string to)
56 {
57 this.Mediator.SetMessage(message, this, to);
58 }
59 public override void RecevieMessage(string message, string from)
60 {
61 Console.WriteLine(string.Format("{0}(男)收到了{1}的消息:{2}", this.Name, from, message));
62 }
63 }
64
65 class Woman:People
66 {
67 public Woman(string name, Mediator mediator)
68 : base(name, mediator)
69 { }
70 public override void SetMessage(string message, string to)
71 {
72 this.Mediator.SetMessage(message, this, to);
73 }
74 public override void RecevieMessage(string message, string from)
75 {
76 Console.WriteLine("{0}(女)收到了{1}的消息:{2}");
77 }
78 }
79
80 static void Main(string[] args)
81 {
82 MatrimonialMediator matrimonialMediator = new MatrimonialMediator();
83 People HanMeimei = new Woman("韩美美", matrimonialMediator);
84 People LiLei = new Man("李雷", matrimonialMediator);
85 HanMeimei.SetMessage("约在XXXX见面", "李雷");
86 Console.Read();
87 }
88
89
90
91
2 {
3 public abstract void Register(People people);
4 public abstract void SetMessage(string message, People from, string to);
5 }
6
7 class MatrimonialMediator:Mediator
8 {
9 private List<People> _list = new List<People>();
10 public override void Register(People people)
11 {
12 _list.Add(people);
13 }
14 public override void SetMessage(string message, People from, string to)
15 {
16 Console.WriteLine("{0}通过中介对{1}说:{2}", from.Name, to, message);
17 foreach (var item in _list)
18 {
19 if (item.Name == to)
20 {
21 item.RecevieMessage(message,from.Name);
22 break;
23 }
24 }
25 }
26 }
27
28 abstract class People
29 {
30 private string _name;
31 private Mediator _mediator;
32 public Mediator Mediator
33 {
34 get { return _mediator; }
35 }
36 public string Name
37 {
38 get { return _name; }
39 }
40 public People(string name, Mediator mediator)
41 {
42 _name = name;
43 _mediator = mediator;
44 _mediator.Register(this);
45 }
46 public abstract void SetMessage(string message,string to);
47 public abstract void RecevieMessage(string message,string from);
48 }
49
50 class Man:People
51 {
52 public Man(string name,Mediator mediator)
53 : base(name, mediator)
54 { }
55 public override void SetMessage(string message,string to)
56 {
57 this.Mediator.SetMessage(message, this, to);
58 }
59 public override void RecevieMessage(string message, string from)
60 {
61 Console.WriteLine(string.Format("{0}(男)收到了{1}的消息:{2}", this.Name, from, message));
62 }
63 }
64
65 class Woman:People
66 {
67 public Woman(string name, Mediator mediator)
68 : base(name, mediator)
69 { }
70 public override void SetMessage(string message, string to)
71 {
72 this.Mediator.SetMessage(message, this, to);
73 }
74 public override void RecevieMessage(string message, string from)
75 {
76 Console.WriteLine("{0}(女)收到了{1}的消息:{2}");
77 }
78 }
79
80 static void Main(string[] args)
81 {
82 MatrimonialMediator matrimonialMediator = new MatrimonialMediator();
83 People HanMeimei = new Woman("韩美美", matrimonialMediator);
84 People LiLei = new Man("李雷", matrimonialMediator);
85 HanMeimei.SetMessage("约在XXXX见面", "李雷");
86 Console.Read();
87 }
88
89
90
91