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

ContractedBlock.gif ExpandedBlockStart.gif Machine类(可以包含容器)
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Gof.Test.Mediator
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    public class Machine
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        public Machine(string id)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 9InBlock.gif            _id = id;
10ExpandedSubBlockEnd.gif        }

11InBlock.gif        private TubMediator _mediator = TubMediator.Singleton();
12InBlock.gif        private string _id;
13InBlock.gif
14InBlock.gif        public void AddTub(Tub t)
15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
16InBlock.gif            _mediator.Set(t,this);
17ExpandedSubBlockEnd.gif        }

18InBlock.gif        public System.Collections.IList GetTubs()
19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
20InBlock.gif            return _mediator.GetTubs(this);
21ExpandedSubBlockEnd.gif        }

22InBlock.gif        public override string ToString()
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            return _id;
25ExpandedSubBlockEnd.gif        }

26InBlock.gif        public override int GetHashCode()
27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
28InBlock.gif            return _id.GetHashCode();
29ExpandedSubBlockEnd.gif        }

30InBlock.gif        public override bool Equals(object obj)
31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
32InBlock.gif            if(obj == this)
33ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
34InBlock.gif                return true;
35ExpandedSubBlockEnd.gif            }

36InBlock.gif            if(!(obj is Machine))
37ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
38InBlock.gif                return false;
39ExpandedSubBlockEnd.gif            }

40InBlock.gif            Machine m = (Machine)obj;
41InBlock.gif            return _id.Equals(m._id);
42ExpandedSubBlockEnd.gif        }

43InBlock.gif
44ExpandedSubBlockEnd.gif    }

45ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif Tub容器(可以添加到Machine,一个容器只能添加到一个Machine)
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Gof.Test.Mediator
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    public class Tub
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        public Tub()
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{}
 9InBlock.gif        private string _id;
10InBlock.gif        private TubMediator _mediator = TubMediator.Singleton();
11InBlock.gif        public Tub(string id)
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            _id = id;
14ExpandedSubBlockEnd.gif        }

15InBlock.gif        public Machine Location
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
17InBlock.gif            get
18ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
19InBlock.gif                return _mediator.GetMachine(this);
20ExpandedSubBlockEnd.gif            }

21InBlock.gif            set
22ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
23InBlock.gif                _mediator.Set(this,value);
24ExpandedSubBlockEnd.gif            }

25ExpandedSubBlockEnd.gif        }

26InBlock.gif        public override string ToString()
27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
28InBlock.gif            return _id;
29ExpandedSubBlockEnd.gif        }
30InBlock.gif        public override int GetHashCode()
31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
32InBlock.gif            return _id.GetHashCode();
33ExpandedSubBlockEnd.gif        }
34InBlock.gif        public override bool Equals(object obj)
35ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
36InBlock.gif            if(obj== this)
37ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
38InBlock.gif                return true;
39ExpandedSubBlockEnd.gif            }

40InBlock.gif            if(!( obj is Tub))
41ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
42InBlock.gif                return false;
43ExpandedSubBlockEnd.gif            }

44InBlock.gif            Tub t = (Tub)obj;
45InBlock.gif            return _id.Equals(t._id);
46ExpandedSubBlockEnd.gif        }

47InBlock.gif
48ExpandedSubBlockEnd.gif    }

49ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif 中介者
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Gof.Test.Mediator
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    public class TubMediator
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        private System.Collections.Hashtable _tubMachine = new System.Collections.Hashtable();
 8InBlock.gif        private static TubMediator _mediator;
 9InBlock.gif        private TubMediator()
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{}
11InBlock.gif        public static TubMediator Singleton()
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            if(_mediator == null)
14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
15InBlock.gif                _mediator = new TubMediator();
16ExpandedSubBlockEnd.gif            }

17InBlock.gif            return _mediator;
18ExpandedSubBlockEnd.gif        }

19InBlock.gif        public Machine GetMachine(Tub t)
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif             return (Machine)_tubMachine[t];
22ExpandedSubBlockEnd.gif        }

23InBlock.gif        public System.Collections.IList GetTubs(Machine m)
24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
25InBlock.gif            System.Collections.ArrayList al = new System.Collections.ArrayList();
26InBlock.gif            System.Collections.IDictionaryEnumerator e = _tubMachine.GetEnumerator();
27InBlock.gif            while(e.MoveNext())
28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
29InBlock.gif                if(e.Value.Equals(m))
30ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
31InBlock.gif                    al.Add(e.Key);
32ExpandedSubBlockEnd.gif                }

33ExpandedSubBlockEnd.gif            }

34InBlock.gif            return al;
35ExpandedSubBlockEnd.gif        }

36InBlock.gif        public void Set(Tub t,Machine m)
37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
38InBlock.gif            _tubMachine[t] = m;
39ExpandedSubBlockEnd.gif        }

40ExpandedSubBlockEnd.gif    }

41ExpandedBlockEnd.gif}
ContractedBlock.gif ExpandedBlockStart.gif 客户代码
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Gof.Test.Mediator
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    public class TubMediator
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 7InBlock.gif        private System.Collections.Hashtable _tubMachine = new System.Collections.Hashtable();
 8InBlock.gif        private static TubMediator _mediator;
 9InBlock.gif        private TubMediator()
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{}
11InBlock.gif        public static TubMediator Singleton()
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            if(_mediator == null)
14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
15InBlock.gif                _mediator = new TubMediator();
16ExpandedSubBlockEnd.gif            }
17InBlock.gif            return _mediator;
18ExpandedSubBlockEnd.gif        }
19InBlock.gif        public Machine GetMachine(Tub t)
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21InBlock.gif             return (Machine)_tubMachine[t];
22ExpandedSubBlockEnd.gif        }
23InBlock.gif        public System.Collections.IList GetTubs(Machine m)
24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
25InBlock.gif            System.Collections.ArrayList al = new System.Collections.ArrayList();
26InBlock.gif            System.Collections.IDictionaryEnumerator e = _tubMachine.GetEnumerator();
27InBlock.gif            while(e.MoveNext())
28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
29InBlock.gif                if(e.Value.Equals(m))
30ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
31InBlock.gif                    al.Add(e.Key);
32ExpandedSubBlockEnd.gif                }
33ExpandedSubBlockEnd.gif            }
34InBlock.gif            return al;
35ExpandedSubBlockEnd.gif        }
36InBlock.gif        public void Set(Tub t,Machine m)
37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
38InBlock.gif            _tubMachine[t] = m;
39ExpandedSubBlockEnd.gif        }
40ExpandedSubBlockEnd.gif    }
41ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/nanshouyong326/archive/2007/01/05/612604.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值