Design Patterns in ActionScript-Mediator

Ok, the last pattern now. Let’s take a look the intent directly.

Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

– By THE GOF BOOK

From this intent, we can get that this pattern is use for encapsulating the interaction of objects.

 

 

For example, when you build a GUI application, you may enable or disable the button when the user input something, you may add a listener for the text change event. Then, when the event happens, you change the state of that button. It means that you must know the button object, and some details about the button. As you application going further, you may do many things in that function, also, you need to know many classes’ detail. When the requirement changed, you need to fix many things. And your classes maybe look like as follows.

clip_image001

With the help of Mediator pattern, you reduce the complexity of each widget, they don’t even need to know the existence of other widgets, and they only need to know the mediator. When an object changes, it tells the mediator, then the mediator notify the other objects. In this pattern, you put the complexity from the widget to the mediator. The mediator needs to know everything. And the classes changes to below.

clip_image002

In GoF, all the widgets will inherit a super class Colleague, and the widgets is called concrete colleague. When the widget’s state changes, it calls the widgetChanged() method. My simple implementation of colleague is as follows.

  1. public class Colleague
  2. {
  3. private   var mediator : Mediator ;
  4.  
  5. public   function Colleague ( mediator : Mediator )
  6. {
  7. this . mediator = mediator ;
  8. }
  9.  
  10. public   function hasChanged () : String
  11. {
  12. return   mediator . detectChanged ( this ) ;
  13. }
  14. }

In the concrete mediator, we need to distinguish which objects call the method, and here is my implementation.

  1. public function detectChanged ( colleague : Colleague ) : String
  2. {
  3. if ( colleague == colleague1 )
  4. return   colleague2 . operation2 () ;
  5. else   if ( colleague == colleague2 )
  6. return   colleague1 . operation1 () ;
  7.  
  8. return   null ;
  9. }

And the class diagram below is from the GoF.

clip_image003

Hope this helps. That’s all for this pattern.DownloadDownload Full Project

Thanks for you kindness in reading those articles, especially for those who make comments. Actually, I’m a fresh man in design patterns; the main reason that I wrote those articles is to help me learn more quickly. If there is any wrong that troubles you, I’ve to say sorry here, sincerely! If you have any questions, contact me, liubo.cs@hotmail.com .

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值