Topic: 关于Mediator模式的一点想法

  Print this page

1.关于Mediator模式的一点想法 Copy to clipboard
Posted by: jameszhang
Posted on: 2004-12-03 15:46

既然是封装一系列的对象交互,使其耦合松散,我想在这些对象里就不要引用Mediator对象了,只是Mediator引用这些要交户的对象。不知道大家怎么看?

2.Re:关于Mediator模式的一点想法 [Re: jameszhang] Copy to clipboard
Posted by: floater
Posted on: 2004-12-03 23:31

mediator is just a way to loosely couple the actions in a structure of objects. It makes the actions relatively invariant to the change of the structure of objects. For example, due to the need of layouts, we may put a button in a panel, or put a button in a panel within another panel. So mediators concern only the button, or the like, not the panels(mainly for layout purpose). It consolidates the actions too.

3.Re:关于Mediator模式的一点想法 [Re: jameszhang] Copy to clipboard
Posted by: WeiterWay
Posted on: 2004-12-07 10:39

"我想在这些对象里就不要引用Mediator对象了,只是Mediator引用这些要交户的对象"
应该说对象需要引用Mediator了,想象一下,如果中间人都不清楚,那找谁协调呀。但是需要预防Mediator成了“万能”对象。所以使用模式时刻记住有反模式向你招手:)

4.Re:关于Mediator模式的一点想法 [Re: WeiterWay] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-12-07 10:53

WeiterWay wrote:
"我想在这些对象里就不要引用Mediator对象了,只是Mediator引用这些要交户的对象"
应该说对象需要引用Mediator了,想象一下,如果中间人都不清楚,那找谁协调呀。但是对象看到的mediator只是abstract的,这样达到decoupling的目的。

我的意思是说 : 一个武功高强的大侠看到了不平事,由他来 全办了,双方 都 不 认识他 ,这样不好吗?

5.Re:关于Mediator模式的一点想法 [Re: jameszhang] Copy to clipboard
Posted by: WeiterWay
Posted on: 2004-12-07 10:57

jameszhang wrote:
我的意思是说 : 一个武功高强的大侠看到了不平事,由他来 全办了,双方 都 不 认识他 ,这样不好吗?

这就有可能成为万能对象,呵呵。

6.Re:关于Mediator模式的一点想法 [Re: jameszhang] Copy to clipboard
Posted by: WeiterWay
Posted on: 2004-12-07 11:00

其实我想mediator的出发点还是:对象与其他对象之间的交互错综复杂的情况下,采用一个mediator处理这种关系,这样对象与其他对象之间交互间接地转化为对象与mediator之间交互。

7.Re:关于Mediator模式的一点想法 [Re: jameszhang] Copy to clipboard
Posted by: yamakasy
Posted on: 2004-12-07 12:16

jameszhang wrote:
我的意思是说 : 一个武功高强的大侠看到了不平事,由他来 全办了,双方 都 不 认识他 ,这样不好吗?


是的阿!这样就对了。

比如说,一个Swing组件和DataSet绑定的例子,用一个Mediator绑定这两个之间的关系,Mediator mediator=new Mediator(comp,dataset),然后所有的处理都放在Mediator中处理,Swing组件和DataSet并不需要做什么改变,甚至不知道Mediator在使用他们。

8.Re:关于Mediator模式的一点想法 [Re: WeiterWay] Copy to clipboard
Posted by: yamakasy
Posted on: 2004-12-07 12:19

WeiterWay wrote:
其实我想mediator的出发点还是:对象与其他对象之间的交互错综复杂的情况下,采用一个mediator处理这种关系,这样对象与其他对象之间交互间接地转化为对象与mediator之间交互。


你的这样的观点只是把对象的交叉依赖转化为对单个对象的依赖,并没有做到真正的decouple,还不如用依赖倒置转化为对接口的依赖。

9.Re:关于Mediator模式的一点想法 [Re: yamakasy] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-12-07 12:22

yamakasy wrote:
是的阿!这样就对了。

比如说,一个Swing组件和DataSet绑定的例子,用一个Mediator绑定这两个之间的关系,Mediator mediator=new Mediator(comp,dataset),然后所有的处理都放在Mediator中处理,Swing组件和DataSet并不需要做什么改变,甚至不知道Mediator在使用他们。

我噻!我就这个意思,当我Mediator换了,其他的类不需要改,可是我看到资料给出的例子都互相引用,所以问问,呵呵

10.Re:关于Mediator模式的一点想法 [Re: jameszhang] Copy to clipboard
Posted by: WeiterWay
Posted on: 2004-12-07 12:43

这其实就是你看问题站的角度不一样罢了。这里Mediator mediator=new Mediator(comp,dataset)语句是谁调用?如果一个是一个FrameContainer,它调用这条语句,那么mediator不还是被FrameContainer引用么?即使是任何一个Client对象调用这条语句,这个client都需要通过mediator来与swing和ds发生关系。

11.Re:关于Mediator模式的一点想法 [Re: jameszhang] Copy to clipboard
Posted by: floater
Posted on: 2004-12-07 23:37

jameszhang wrote:
既然是封装一系列的对象交互,使其耦合松散,我想在这些对象里就不要引用Mediator对象了,只是Mediator引用这些要交户的对象。不知道大家怎么看?

When you click a button, the button needs to tell the mediator, so it would be better to have a reference in the button, otherwise any look up will introduce other dependencies.

12.Re:关于Mediator模式的一点想法 [Re: floater] Copy to clipboard
Posted by: jameszhang
Posted on: 2004-12-08 08:01

floater wrote:
When you click a button, the button needs to tell the mediator, so it would be better to have a reference in the button, otherwise any look up will introduce other dependencies.

我觉得是 事件来引用 mediator,不是button本身, 这种调用 用 command 不是很好吗,至于command在调用什么模式就随便了,不知对不?

13.Re:关于Mediator模式的一点想法 [Re: jameszhang] Copy to clipboard
Posted by: floater
Posted on: 2004-12-08 23:47

事件来引用 mediator - my 2 cents, this goes too far apart, no need to do so. However, I haven't thought the benefit deeper.

14.Re:关于Mediator模式的一点想法 [Re: jameszhang] Copy to clipboard
Posted by: zyzhang
Posted on: 2004-12-20 21:05

I think mediator contributes on the promotion of the complex of many to many relationship. It encapsulates the communication between other objects. On the other hand, Observer design pattern handle one-to-many relationship by introducing "observer" and "subject" objects. From reusability point of view, Observer is easier than Mediator.

Actually, you can see the idea of Mediator design pattern applied in many places, such as j2ee session facade, enforce the relational integrity,and ect.

Command design pattern generally specifies a sender-receiver connection with a subclass through interface. Instead, Mediator makes senders and receivers reference each other indirectly.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923