Mediator 用一个中介者对象来封装一系列的对象交互。中介者使各个对象不需要显式的相互作用,从而使其耦合松散,而且可以独立地改变它们之间的交互。

 Machine类(可以包含容器)
Machine类(可以包含容器)1
 using System;
using System;2

3
 namespace Gof.Test.Mediator
namespace Gof.Test.Mediator4


 {
{5
 public class Machine
    public class Machine6

 
     {
{7
 public Machine(string id)
        public Machine(string id)8

 
         {
{9
 _id = id;
            _id = id;10
 }
        }11
 private TubMediator _mediator = TubMediator.Singleton();
        private TubMediator _mediator = TubMediator.Singleton();12
 private string _id;
        private string _id;13

14
 public void AddTub(Tub t)
        public void AddTub(Tub t)15

 
         {
{16
 _mediator.Set(t,this);
            _mediator.Set(t,this);17
 }
        }18
 public System.Collections.IList GetTubs()
        public System.Collections.IList GetTubs()19

 
         {
{20
 return _mediator.GetTubs(this);
            return _mediator.GetTubs(this);21
 }
        }22
 public override string ToString()
        public override string ToString()23

 
         {
{24
 return _id;
            return _id;25
 }
        }26
 public override int GetHashCode()
        public override int GetHashCode()27

 
         {
{28
 return _id.GetHashCode();
            return _id.GetHashCode();29
 }
        }30
 public override bool Equals(object obj)
        public override bool Equals(object obj)31

 
         {
{32
 if(obj == this)
            if(obj == this)33

 
             {
{34
 return true;
                return true;35
 }
            }36
 if(!(obj is Machine))
            if(!(obj is Machine))37

 
             {
{38
 return false;
                return false;39
 }
            }40
 Machine m = (Machine)obj;
            Machine m = (Machine)obj;41
 return _id.Equals(m._id);
            return _id.Equals(m._id);42
 }
        }43

44
 }
    }45
 }
}
 Tub容器(可以添加到Machine,一个容器只能添加到一个Machine)
Tub容器(可以添加到Machine,一个容器只能添加到一个Machine)1
 using System;
using System;2

3
 namespace Gof.Test.Mediator
namespace Gof.Test.Mediator4


 {
{5
 public class Tub
    public class Tub6

 
     {
{7
 public Tub()
        public Tub()8

 
         {}
{}9
 private string _id;
        private string _id;10
 private TubMediator _mediator = TubMediator.Singleton();
        private TubMediator _mediator = TubMediator.Singleton();11
 public Tub(string id)
        public Tub(string id)12

 
         {
{13
 _id = id;
            _id = id;14
 }
        }15
 public Machine Location
        public Machine Location16

 
         {
{17
 get
            get18

 
             {
{19
 return _mediator.GetMachine(this);
                return _mediator.GetMachine(this);20
 }
            }21
 set
            set22

 
             {
{23
 _mediator.Set(this,value);
                _mediator.Set(this,value);24
 }
            }25
 }
        }26
 public override string ToString()
        public override string ToString()27

 
         {
{28
 return _id;
            return _id;29
 }
        }30
 public override int GetHashCode()
        public override int GetHashCode()31

 
         {
{32
 return _id.GetHashCode();
            return _id.GetHashCode();33
 }
        }34
 public override bool Equals(object obj)
        public override bool Equals(object obj)35

 
         {
{36
 if(obj== this)
            if(obj== this)37

 
             {
{38
 return true;
                return true;39
 }
            }40
 if(!( obj is Tub))
            if(!( obj is Tub))41

 
             {
{42
 return false;
                return false;43
 }
            }44
 Tub t = (Tub)obj;
            Tub t = (Tub)obj;45
 return _id.Equals(t._id);
            return _id.Equals(t._id);46
 }
        }47

48
 }
    }49
 }
}
 中介者
中介者1
 using System;
using System;2

3
 namespace Gof.Test.Mediator
namespace Gof.Test.Mediator4


 {
{5
 public class TubMediator
    public class TubMediator6

 
     {
{7
 private System.Collections.Hashtable _tubMachine = new System.Collections.Hashtable();
        private System.Collections.Hashtable _tubMachine = new System.Collections.Hashtable();8
 private static TubMediator _mediator;
        private static TubMediator _mediator;9
 private TubMediator()
        private TubMediator()10

 
         {}
{}11
 public static TubMediator Singleton()
        public static TubMediator Singleton()12

 
         {
{13
 if(_mediator == null)
            if(_mediator == null)14

 
             {
{15
 _mediator = new TubMediator();
                _mediator = new TubMediator();16
 }
            }17
 return _mediator;
            return _mediator;18
 }
        }19
 public Machine GetMachine(Tub t)
        public Machine GetMachine(Tub t)20

 
         {
{21
 return (Machine)_tubMachine[t];
             return (Machine)_tubMachine[t];22
 }
        }23
 public System.Collections.IList GetTubs(Machine m)
        public System.Collections.IList GetTubs(Machine m)24

 
         {
{25
 System.Collections.ArrayList al = new System.Collections.ArrayList();
            System.Collections.ArrayList al = new System.Collections.ArrayList();26
 System.Collections.IDictionaryEnumerator e = _tubMachine.GetEnumerator();
            System.Collections.IDictionaryEnumerator e = _tubMachine.GetEnumerator();27
 while(e.MoveNext())
            while(e.MoveNext())28

 
             {
{29
 if(e.Value.Equals(m))
                if(e.Value.Equals(m))30

 
                 {
{31
 al.Add(e.Key);
                    al.Add(e.Key);32
 }
                }33
 }
            }34
 return al;
            return al;35
 }
        }36
 public void Set(Tub t,Machine m)
        public void Set(Tub t,Machine m)37

 
         {
{38
 _tubMachine[t] = m;
            _tubMachine[t] = m;39
 }
        }40
 }
    }41
 }
}
 客户代码
客户代码1
 using System;
using System;2

3
 namespace Gof.Test.Mediator
namespace Gof.Test.Mediator4


 {
{5
 public class TubMediator
    public class TubMediator6

 
     {
{7
 private System.Collections.Hashtable _tubMachine = new System.Collections.Hashtable();
        private System.Collections.Hashtable _tubMachine = new System.Collections.Hashtable();8
 private static TubMediator _mediator;
        private static TubMediator _mediator;9
 private TubMediator()
        private TubMediator()10

 
         {}
{}11
 public static TubMediator Singleton()
        public static TubMediator Singleton()12

 
         {
{13
 if(_mediator == null)
            if(_mediator == null)14

 
             {
{15
 _mediator = new TubMediator();
                _mediator = new TubMediator();16
 }
            }17
 return _mediator;
            return _mediator;18
 }
        }19
 public Machine GetMachine(Tub t)
        public Machine GetMachine(Tub t)20

 
         {
{21
 return (Machine)_tubMachine[t];
             return (Machine)_tubMachine[t];22
 }
        }23
 public System.Collections.IList GetTubs(Machine m)
        public System.Collections.IList GetTubs(Machine m)24

 
         {
{25
 System.Collections.ArrayList al = new System.Collections.ArrayList();
            System.Collections.ArrayList al = new System.Collections.ArrayList();26
 System.Collections.IDictionaryEnumerator e = _tubMachine.GetEnumerator();
            System.Collections.IDictionaryEnumerator e = _tubMachine.GetEnumerator();27
 while(e.MoveNext())
            while(e.MoveNext())28

 
             {
{29
 if(e.Value.Equals(m))
                if(e.Value.Equals(m))30

 
                 {
{31
 al.Add(e.Key);
                    al.Add(e.Key);32
 }
                }33
 }
            }34
 return al;
            return al;35
 }
        }36
 public void Set(Tub t,Machine m)
        public void Set(Tub t,Machine m)37

 
         {
{38
 _tubMachine[t] = m;
            _tubMachine[t] = m;39
 }
        }40
 }
    }41
 }
}.jpg) 
  
 
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号