Mediator 用一个中介者对象来封装一系列的对象交互。中介者使各个对象不需要显式的相互作用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
Machine类(可以包含容器)
1using System;
2
3namespace Gof.Test.Mediator
4{
5 public class Machine
6 {
7 public Machine(string id)
8 {
9 _id = id;
10 }
11 private TubMediator _mediator = TubMediator.Singleton();
12 private string _id;
13
14 public void AddTub(Tub t)
15 {
16 _mediator.Set(t,this);
17 }
18 public System.Collections.IList GetTubs()
19 {
20 return _mediator.GetTubs(this);
21 }
22 public override string ToString()
23 {
24 return _id;
25 }
26 public override int GetHashCode()
27 {
28 return _id.GetHashCode();
29 }
30 public override bool Equals(object obj)
31 {
32 if(obj == this)
33 {
34 return true;
35 }
36 if(!(obj is Machine))
37 {
38 return false;
39 }
40 Machine m = (Machine)obj;
41 return _id.Equals(m._id);
42 }
43
44 }
45}
1using System;
2
3namespace Gof.Test.Mediator
4{
5 public class Machine
6 {
7 public Machine(string id)
8 {
9 _id = id;
10 }
11 private TubMediator _mediator = TubMediator.Singleton();
12 private string _id;
13
14 public void AddTub(Tub t)
15 {
16 _mediator.Set(t,this);
17 }
18 public System.Collections.IList GetTubs()
19 {
20 return _mediator.GetTubs(this);
21 }
22 public override string ToString()
23 {
24 return _id;
25 }
26 public override int GetHashCode()
27 {
28 return _id.GetHashCode();
29 }
30 public override bool Equals(object obj)
31 {
32 if(obj == this)
33 {
34 return true;
35 }
36 if(!(obj is Machine))
37 {
38 return false;
39 }
40 Machine m = (Machine)obj;
41 return _id.Equals(m._id);
42 }
43
44 }
45}
Tub容器(可以添加到Machine,一个容器只能添加到一个Machine)
1using System;
2
3namespace Gof.Test.Mediator
4{
5 public class Tub
6 {
7 public Tub()
8 {}
9 private string _id;
10 private TubMediator _mediator = TubMediator.Singleton();
11 public Tub(string id)
12 {
13 _id = id;
14 }
15 public Machine Location
16 {
17 get
18 {
19 return _mediator.GetMachine(this);
20 }
21 set
22 {
23 _mediator.Set(this,value);
24 }
25 }
26 public override string ToString()
27 {
28 return _id;
29 }
30 public override int GetHashCode()
31 {
32 return _id.GetHashCode();
33 }
34 public override bool Equals(object obj)
35 {
36 if(obj== this)
37 {
38 return true;
39 }
40 if(!( obj is Tub))
41 {
42 return false;
43 }
44 Tub t = (Tub)obj;
45 return _id.Equals(t._id);
46 }
47
48 }
49}
1using System;
2
3namespace Gof.Test.Mediator
4{
5 public class Tub
6 {
7 public Tub()
8 {}
9 private string _id;
10 private TubMediator _mediator = TubMediator.Singleton();
11 public Tub(string id)
12 {
13 _id = id;
14 }
15 public Machine Location
16 {
17 get
18 {
19 return _mediator.GetMachine(this);
20 }
21 set
22 {
23 _mediator.Set(this,value);
24 }
25 }
26 public override string ToString()
27 {
28 return _id;
29 }
30 public override int GetHashCode()
31 {
32 return _id.GetHashCode();
33 }
34 public override bool Equals(object obj)
35 {
36 if(obj== this)
37 {
38 return true;
39 }
40 if(!( obj is Tub))
41 {
42 return false;
43 }
44 Tub t = (Tub)obj;
45 return _id.Equals(t._id);
46 }
47
48 }
49}
中介者
1using System;
2
3namespace Gof.Test.Mediator
4{
5 public class TubMediator
6 {
7 private System.Collections.Hashtable _tubMachine = new System.Collections.Hashtable();
8 private static TubMediator _mediator;
9 private TubMediator()
10 {}
11 public static TubMediator Singleton()
12 {
13 if(_mediator == null)
14 {
15 _mediator = new TubMediator();
16 }
17 return _mediator;
18 }
19 public Machine GetMachine(Tub t)
20 {
21 return (Machine)_tubMachine[t];
22 }
23 public System.Collections.IList GetTubs(Machine m)
24 {
25 System.Collections.ArrayList al = new System.Collections.ArrayList();
26 System.Collections.IDictionaryEnumerator e = _tubMachine.GetEnumerator();
27 while(e.MoveNext())
28 {
29 if(e.Value.Equals(m))
30 {
31 al.Add(e.Key);
32 }
33 }
34 return al;
35 }
36 public void Set(Tub t,Machine m)
37 {
38 _tubMachine[t] = m;
39 }
40 }
41}
1using System;
2
3namespace Gof.Test.Mediator
4{
5 public class TubMediator
6 {
7 private System.Collections.Hashtable _tubMachine = new System.Collections.Hashtable();
8 private static TubMediator _mediator;
9 private TubMediator()
10 {}
11 public static TubMediator Singleton()
12 {
13 if(_mediator == null)
14 {
15 _mediator = new TubMediator();
16 }
17 return _mediator;
18 }
19 public Machine GetMachine(Tub t)
20 {
21 return (Machine)_tubMachine[t];
22 }
23 public System.Collections.IList GetTubs(Machine m)
24 {
25 System.Collections.ArrayList al = new System.Collections.ArrayList();
26 System.Collections.IDictionaryEnumerator e = _tubMachine.GetEnumerator();
27 while(e.MoveNext())
28 {
29 if(e.Value.Equals(m))
30 {
31 al.Add(e.Key);
32 }
33 }
34 return al;
35 }
36 public void Set(Tub t,Machine m)
37 {
38 _tubMachine[t] = m;
39 }
40 }
41}
客户代码
1using System;
2
3namespace Gof.Test.Mediator
4{
5 public class TubMediator
6 {
7 private System.Collections.Hashtable _tubMachine = new System.Collections.Hashtable();
8 private static TubMediator _mediator;
9 private TubMediator()
10 {}
11 public static TubMediator Singleton()
12 {
13 if(_mediator == null)
14 {
15 _mediator = new TubMediator();
16 }
17 return _mediator;
18 }
19 public Machine GetMachine(Tub t)
20 {
21 return (Machine)_tubMachine[t];
22 }
23 public System.Collections.IList GetTubs(Machine m)
24 {
25 System.Collections.ArrayList al = new System.Collections.ArrayList();
26 System.Collections.IDictionaryEnumerator e = _tubMachine.GetEnumerator();
27 while(e.MoveNext())
28 {
29 if(e.Value.Equals(m))
30 {
31 al.Add(e.Key);
32 }
33 }
34 return al;
35 }
36 public void Set(Tub t,Machine m)
37 {
38 _tubMachine[t] = m;
39 }
40 }
41}
1using System;
2
3namespace Gof.Test.Mediator
4{
5 public class TubMediator
6 {
7 private System.Collections.Hashtable _tubMachine = new System.Collections.Hashtable();
8 private static TubMediator _mediator;
9 private TubMediator()
10 {}
11 public static TubMediator Singleton()
12 {
13 if(_mediator == null)
14 {
15 _mediator = new TubMediator();
16 }
17 return _mediator;
18 }
19 public Machine GetMachine(Tub t)
20 {
21 return (Machine)_tubMachine[t];
22 }
23 public System.Collections.IList GetTubs(Machine m)
24 {
25 System.Collections.ArrayList al = new System.Collections.ArrayList();
26 System.Collections.IDictionaryEnumerator e = _tubMachine.GetEnumerator();
27 while(e.MoveNext())
28 {
29 if(e.Value.Equals(m))
30 {
31 al.Add(e.Key);
32 }
33 }
34 return al;
35 }
36 public void Set(Tub t,Machine m)
37 {
38 _tubMachine[t] = m;
39 }
40 }
41}