Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Design Pattern & UML  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 关于Mediator模式的一点想法
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-12-03 15:46 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
既然是封装一系列的对象交互,使其耦合松散,我想在这些对象里就不要引用Mediator对象了,只是Mediator引用这些要交户的对象。不知道大家怎么看?


"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:关于Mediator模式的一点想法 [Re:jameszhang]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2004-12-03 23:31 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:关于Mediator模式的一点想法 [Re:jameszhang]
WeiterWay





发贴: 25
积分: 0
于 2004-12-07 10:39 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
"我想在这些对象里就不要引用Mediator对象了,只是Mediator引用这些要交户的对象"
应该说对象需要引用Mediator了,想象一下,如果中间人都不清楚,那找谁协调呀。但是需要预防Mediator成了“万能”对象。所以使用模式时刻记住有反模式向你招手:)


WeiterWay edited on 2004-12-07 10:55

作者 Re:关于Mediator模式的一点想法 [Re:WeiterWay]
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-12-07 10:53 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
WeiterWay wrote:
"我想在这些对象里就不要引用Mediator对象了,只是Mediator引用这些要交户的对象"
应该说对象需要引用Mediator了,想象一下,如果中间人都不清楚,那找谁协调呀。但是对象看到的mediator只是abstract的,这样达到decoupling的目的。

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



"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:关于Mediator模式的一点想法 [Re:jameszhang]
WeiterWay





发贴: 25
积分: 0
于 2004-12-07 10:57 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
jameszhang wrote:
我的意思是说 : 一个武功高强的大侠看到了不平事,由他来 全办了,双方 都 不 认识他 ,这样不好吗?

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



作者 Re:关于Mediator模式的一点想法 [Re:jameszhang]
WeiterWay





发贴: 25
积分: 0
于 2004-12-07 11:00 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
其实我想mediator的出发点还是:对象与其他对象之间的交互错综复杂的情况下,采用一个mediator处理这种关系,这样对象与其他对象之间交互间接地转化为对象与mediator之间交互。


作者 Re:关于Mediator模式的一点想法 [Re:jameszhang]
yamakasy



CJSDN高级会员


发贴: 527
积分: 63
于 2004-12-07 12:16 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
jameszhang wrote:
我的意思是说 : 一个武功高强的大侠看到了不平事,由他来 全办了,双方 都 不 认识他 ,这样不好吗?


是的阿!这样就对了。

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



old Russian saying: If you are so smart, why are you so poor?


Sun Certified Java Programer
yamakasy.blogone.net
作者 Re:关于Mediator模式的一点想法 [Re:WeiterWay]
yamakasy



CJSDN高级会员


发贴: 527
积分: 63
于 2004-12-07 12:19 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
WeiterWay wrote:
其实我想mediator的出发点还是:对象与其他对象之间的交互错综复杂的情况下,采用一个mediator处理这种关系,这样对象与其他对象之间交互间接地转化为对象与mediator之间交互。


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



old Russian saying: If you are so smart, why are you so poor?


Sun Certified Java Programer
yamakasy.blogone.net
作者 Re:关于Mediator模式的一点想法 [Re:yamakasy]
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-12-07 12:22 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
yamakasy wrote:
是的阿!这样就对了。

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

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



"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:关于Mediator模式的一点想法 [Re:jameszhang]
WeiterWay





发贴: 25
积分: 0
于 2004-12-07 12:43 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这其实就是你看问题站的角度不一样罢了。这里Mediator mediator=new Mediator(comp,dataset)语句是谁调用?如果一个是一个FrameContainer,它调用这条语句,那么mediator不还是被FrameContainer引用么?即使是任何一个Client对象调用这条语句,这个client都需要通过mediator来与swing和ds发生关系。

WeiterWay edited on 2004-12-07 12:45

作者 Re:关于Mediator模式的一点想法 [Re:jameszhang]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2004-12-07 23:37 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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.



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:关于Mediator模式的一点想法 [Re:floater]
jameszhang



CJSDN高级会员


发贴: 1594
积分: 111
于 2004-12-08 08:01 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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在调用什么模式就随便了,不知对不?



"First they ignore u, then they laugh at u, then they fight u, then u will win

Mahatma Gandhi"

作者 Re:关于Mediator模式的一点想法 [Re:jameszhang]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2004-12-08 23:47 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
事件来引用 mediator - my 2 cents, this goes too far apart, no need to do so. However, I haven't thought the benefit deeper.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code
作者 Re:关于Mediator模式的一点想法 [Re:jameszhang]
zyzhang

Explorer



发贴: 155
积分: 30
于 2004-12-20 21:05 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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.




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

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