Call Encore now on +44 (0) 1273 722 544 or contact us
Monday 6th, February 2012

NSNotificationCenter JavaFX port

Posted by Jethro Grassie on March 3, 2009

Following on from my last post, here I have now added a JavaFX port!

Download the latest zip which has actionscript 3, C/C++ and now also JavaFX version of the super NSNotificationCenter.

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.

Flex architectural frameworks

Posted by Jethro Grassie on February 24, 2009

Flash/flex developers have been bombarded by so called MVC frameworks (I say ‘so called’ because they are not really MVC frameworks, but rather a collection of design patterns, including the MVC pattern).

This is partly to do with the evolution of actionscript, from something very rudimentary, simply to help add some interactivity to animations, to procedural, to semi-object orientated and then finally to the fully object orientated language it is today.

It is also partly to do with the fact that actionscript has mostly been used by designers, not developers, and hence many designers have become developers in-line with actionscript’s growth.

So why have all these architectural frameworks been raining on the actionscript community? No other language attracts this kind of attention. Its not because actionscript is better than other languages or because its in greater need for these kinds of frameworks than any other language.

For the most part, other OOP languages are largely used by developers who already understand the principals of OOP and design patterns.
There is no need to develop an MVC framework for developers to use – MVC is a design pattern.
Developers make use of design patterns to create object orientated solutions to common problems in software design. This does not mean the liberal application of every design pattern ever conceived to every project you work on, but use them where they solve a problem, where they make sense.

I guess this is my biggest point against frameworks like PuveMVC and Cairngorm.
By attempting to create a basis for good software design, they can actually fail by creating over-designed, or even badly designed, applications by failing to recognise an applications specific domain needs, requirements and problems.

There are positive benefits of these architectural frameworks too however (if used where appropriate).
Firstly they enforce development teams to work to the same prescribed architecture.
This mostly benefits teams with some novices in the wings. They need not understand why the patterns are used, or how the framework implements it, they just read the documentation and the tutorials of the framework and off they go.
Secondly, and due to the first point, it makes it easier to hire people (and I am speaking specifically for flex/actionscript developers). If they have a working experience of one of these frameworks, it makes it less risky for the employer. There are plenty of very inexperienced actionscript developers out there (due to the evolution of actionscript and its community).
Therefore, with a mixture of well skilled and not so well skilled developers, these frameworks really help bridge the gap.

So, lets say you already have highly skilled developers.
There can be good cases to not use these architectural frameworks for some, even several, applications.
In this scenario, your team will author code that makes use of the correct design patterns as and where needed.

My next post will introduce what should be a highly useful class to actionscript developers not using one of these architectural frameworks.

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