Call CJT now on +44 (0) 1273 722 544 or contact us
Friday 3rd, September 2010

A better way

Posted by Jethro Grassie on March 2, 2009

In my previous post, I described how the architectural frameworks that have sprung up for flex/actionscript developers are not the best thing since sliced bread for implementing well designed applications.

They attempt to take the design pattern, MVC, along with some other design patterns and bring them all together into one monolithic architectural framework.

However, you do not need to use these frameworks to program using the MVC pattern or indeed any of the other patterns used in these frameworks.

So for those developers who understand the various design patterns already, and are opting not to use these architectural frameworks, I present something I feel rather useful in terms of actionscript and the MVC pattern inparticular.

As you will already be aware, in the MVC pattern, you ideally want the actors (the Model, the View and the Controller) highly decoupled.
Therefore its common to see the coupling achieved via notifications (the observer pattern AKA publish-subscribe) or some other kind of eventing.
In actionscript 3, its a very common practice to make use of events, as the event model (an implementation of the W3C DOM event model) is built in.
However, the bubbling and capture phases of the event flow only work when the dispatchers are part of the display list. This means events can only flow when the dispatcher is a view element.
Therefore anywhere else, you need to tightly couple to the dispatcher in order to receive events from it. Not ideal.

This brings me onto the Objective-C/C++ NSNotificationCenter found in Apple’s Cocoa API (and of course the GNUstep framework).
This a very elegant, lightweight, fast and easy to use class.
The usage is simple…

  1. Any object which wishes to send a notification, can easily send one via the NSNotificationCenter’s postNotification method (or its variations).
  2. Any object wishing to observe notifications can observe via NSNotificationCenter’s addObserver method.

The most useful part of this is that the observer can choose to listen for specifically named notifications, specific senders of notifications, both by name and sender or listen for *all* notifications regardless of name or sender.
It is up to the developer what level of coupling to use.

Users of the NSNotificationCenter most commonly make you of a static instance retrieved via NSNotificationCenter’s defaultCenter class method, though of course a more compartmented usage can be achieved via instantiating multiple NSNotificationCenter’s. One such usage could be a more modular application whereby a module wishes to have certain module private notifications, yet also publish and subscribe to the rest of the application.

Actual notifications can either be NSNotification instances or instances of sub-classes of NSNotification.
By creating sub-classes, you achieve a stricter interface, though as you see in Cocoa, this is an uncommon practice.

I am sure anyone already familiar with these classes will understand, they can be very useful indeed.
As such, I have ported NSNotificationCenter (and as such also NSNotification), into actionscript 3 (and also into C/C++ while I was at it) for all to use.

Full source code including example/test applications for download here.

Update: Download link now also contains JavaFX version as well.

7 Comments »

  1. didier

    March 3, 2009 // 12:01 am

    This is great for propagating events in a non-tree structure–which falls out of the scope of the W3C DOM Event model!
    Thanks for porting this. (It may be useful in JAVA, too!)

    On the architecture side, one must be careful, though, not to overuse the dependency to the Notification Center across independent subsystems of an application. The “defaultCenter” has drawbacks similar to the Singleton’s. The easy coupling to the Notification Center may reduce the re-usability of involved classes. So, for Flash/FLex programming, as great as this is, I would restrict its use for (internal) data notification within a self-contained business vertical subsystem, and use regular (Flash/Flex) events for notifying outside of that subsystem.

    This is a great tool, which should be part of the AS3 API. Thanks!

  2. Jethro Grassie

    March 3, 2009 // 8:36 am

    @didier, I didnt bother to do the Java version as I found a port online. Though I may have a look at a JavaFX specific version soon.

    As for usage of Flex events compared to NSNotificationCenter, personally I think Flex events should be used where they fit best and that certainly means anything on the display list.
    There is no point sending a notification for a button click when the W3C event model is already there, implemented and works well.
    Another place is data binding.

    However, I feel anywhere else is game for NSNotificationCenter because of its lose coupling. It makes classes far *more* reusable rather than less.

    Specific to modular Flex applications, the defaultCenter should only be used where inter-module communication is needed.

    On a side note… didier your blog theme looks great.

  3. NSNotificationCenter JavaFX port

    March 3, 2009 // 9:08 pm

    [...] on from my last post, here I have now added a JavaFX [...]

  4. flashape

    March 3, 2009 // 9:52 pm

    Am I missing something, or is this just a global event dispatcher? Doesn’t seem that much different than CairngormEventDispatcher or even the SystemController from Grant Davies’ Arp extensions from way back when (http://theresidentalien.typepad.com/ginormous/2005/10/extending_arp.html).

  5. Jethro Grassie

    March 3, 2009 // 10:02 pm

    @flashape You are certainly missing something (though easily missed)!
    There is a great deal of flexibility in the NSNotificationCenter in that it can be a global dispatcher/manager of notifications but can also be modular.
    It also allows for a great deal of flexibility of the observers as to which notifications they receive.
    In this respect the coupling can also be way looser.
    You are not forced to subclass anything nor use anything else.
    These are very lightweight very reusable classes.

  6. RaiulBaztepo

    March 28, 2009 // 10:32 pm

    Hello!
    Very Interesting post! Thank you for such interesting resource!
    PS: Sorry for my bad english, I’v just started to learn this language ;)
    See you!
    Your, Raiul Baztepo

  7. Sanan Murad

    March 31, 2010 // 9:19 pm

    Great work!
    I had application developed in flex and then I had to develop iphone version of it. And it appears that using notification center allows more flexibility and objects decoupling than AS event dispatcher. I was going to develop it myself but luckily you did it already. I’ll definitely use it on next update.
    Thanks a lot again.
    Sanan Murad

Leave a Comment

Like what you see? Then get in touch;
t. +44 (0) 1273 722 544   e. contact us