PureMVC – part 4: ApplicationFacade

My first application was written on C language with main() as entry point. All applications need an entry point, that’s true. PureMVC projects have two entry points – one is the AVM entry point –  the first class that is actually executed (this is the ‘real’ entry point). The other is the ApplicationFacade which starts the PureMVC framework and this is the class that should be considered the centre of our application.

By default, Fash Develop will generate Main.mxml as an entry point. Main.mxml is extended by mx:Application, but my scheme is avoiding to use mx directly. Extends core component is rather good than using it (I will explain the benefit of “subclassing” by other entry, just follow now ).

Creating myapp folder within src folder if you ‘ve not done it yet, then create MainApplication class extended by mx: application.

package myapp {
import mx.controls.Alert;
import mx.core.Application;
import mx.events.FlexEvent;
import view.components.*;
/**
* …
* @author Lam Do
*/
public class MainApplication extends Application {
private var facade: ApplicationFacade;

public var loginPanel: LoginViewLogic;

public function MainApplication() {
this.addEventListener(FlexEvent.APPLICATION_COMPLETE, applicationCompleteHandler);
}

protected function applicationCompleteHandler(event: FlexEvent):void {
this.facade = ApplicationFacade.getInstance();
this.facade.startup(this);
}
}
}

ApplicationFacade class

package  {
import org.puremvc.as3.interfaces.IFacade;
import org.puremvc.as3.patterns.facade.Facade;
import model.*;
import org.puremvc.as3.patterns.observer.Notification;
import view.*;
import controller.*;
/**
* …
* @author Lam Do
*/
public class ApplicationFacade extends Facade implements IFacade {
// Notification name constants
public static const STARTUP: String = “startup”;
public static const LOGIN: String = “login”;
public static const LOGOUT: String = “logout”;
public static const SEND_MESSAGE: String = “send_message”;
public static const VALID_LOGIN:String = “valid_login”;
public static const INVALID_LOGIN:String = “invalid_login”;
public static const DISCONNECT:String = “disconnect”;
public static const RECEIVE_MESSAGE:String = “receive_message”;

public static function getInstance():ApplicationFacade {
if (instance == null) instance = new ApplicationFacade();
return instance as ApplicationFacade;
}

// Register commands with the controller
override protected function initializeController():void {
super.initializeController();
registerCommand(STARTUP, StartupCommand);
registerCommand(LOGIN, LoginCommand);
registerCommand(LOGOUT, LoginCommand);
registerCommand(SEND_MESSAGE, SendMessageCommand);
}

public function startup(app:Object):void {
notifyObservers(new Notification(STARTUP, app));
}
}

}

Finally we need a StartupCommand

package controller {
import model.MyProxy;
import myapp.MainApplication;
import org.puremvc.as3.interfaces.ICommand;
import org.puremvc.as3.interfaces.INotification;
import org.puremvc.as3.patterns.command.SimpleCommand;
import org.puremvc.as3.patterns.observer.Notification;
import view.ApplicationMediator;

/**
* SimpleCommand
*/
public class StartupCommand extends SimpleCommand implements ICommand {

override public function execute(note:INotification):void {
// do some initializations here
facade.registerMediator(new ApplicationMediator(note.getBody() as MainApplication));
facade.registerProxy(new MyProxy());
}

}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s