Tuesday, April 28, 2009

PureMVC improved: commands can be singletons

While working with PureMVC on some games in Flash CS3 I thought it would be quite useful to be able to specify that some commands should not be recreated each time they are run (as is the default behaviour), but rather only once, and the same instance should be used all throughout. In games where certain commands occur often (eg. the one associated with the message 'Deal' in card games) this would be an important memory save.

So I set out to adapt PureMVC. I tweaked the Controller::executeCommand function the most, with some meddling with Controller:registerCommand, Facade::registerCommand, and their respective interfaces. Overall, only 4 files changed, with only the controller suffering essential functionality alteration. The only change in the API is that when you use registerCommand in your implementation of the Facade, you now have a third parameter, called 'singleton', of type Boolean, which defaults to false. If it is set to true, only one instance of that particular command will be created and then reused all throughout the life of the application.

Example of singleton command initialization:

this.registerCommand(STARTUP, StartupCommand, true);


You can download the changed PureMVC framework here.

No comments: