Design Patterns in ActionScript-Mediator

My-Documents-256x256 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.

public class Colleague
{
private var mediator:Mediator;

public function Colleague(mediator:Mediator)
{
this.mediator = mediator;
}

public function hasChanged():String
{
return mediator.detectChanged(this);
}
}

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

public function detectChanged(colleague:Colleague):String
{
if(colleague == colleague1)
return colleague2.operation2();
else if(colleague == colleague2)
return colleague1.operation1();

return null;
}

And the class diagram below is from the GoF.

clip_image003

Hope this helps. That’s all for this pattern.Download Download 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.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

Leave a Reply