org.puremvc.java.patterns.facade
Class Facade

java.lang.Object
  extended by org.puremvc.java.patterns.facade.Facade
All Implemented Interfaces:
IFacade, INotifier

public class Facade
extends java.lang.Object
implements IFacade

A base Singleton IFacade implementation.

In PureMVC, the Facade class assumes these responsibilities:

Example usage:

import org.puremvc.patterns.facade.∗; import com.me.myapp.model.~~; import com.me.myapp.view.~~; import com.me.myapp.controller.~~; public class MyFacade extends Facade { // Notification constants. The Facade is the ideal // location for these constants, since any part // of the application participating in PureMVC // Observer Notification will know the Facade. public static const GO_COMMAND:String = "go"; // Override Singleton Factory method public static function getInstance() : MyFacade { if (instance == null) instance = new MyFacade(); return instance as MyFacade; } // optional initialization hook for Facade override public function initializeFacade() : void { super.initializeFacade(); // do any special subclass initialization here } // optional initialization hook for Controller override public function initializeController() : void { // call super to use the PureMVC Controller Singleton. super.initializeController(); // Otherwise, if you're implmenting your own // IController, then instead do: // if ( controller != null ) return; // controller = MyAppController.getInstance(); // do any special subclass initialization here // such as registering Commands registerCommand( GO_COMMAND, com.me.myapp.controller.GoCommand ) } // optional initialization hook for Model override public function initializeModel() : void { // call super to use the PureMVC Model Singleton. super.initializeModel(); // Otherwise, if you're implmenting your own // IModel, then instead do: // if ( model != null ) return; // model = MyAppModel.getInstance(); // do any special subclass initialization here // such as creating and registering Model proxys // that don't require a facade reference at // construction time, such as fixed type lists // that never need to send Notifications. regsiterProxy( new USStateNamesProxy() ); // CAREFUL: Can't reference Facade instance in constructor // of new Proxys from here, since this step is part of // Facade construction! Usually, Proxys needing to send // notifications are registered elsewhere in the app // for this reason. } // optional initialization hook for View override public function initializeView() : void { // call super to use the PureMVC View Singleton. super.initializeView(); // Otherwise, if you're implmenting your own // IView, then instead do: // if ( view != null ) return; // view = MyAppView.getInstance(); // do any special subclass initialization here // such as creating and registering Mediators // that do not need a Facade reference at construction // time. registerMediator( new LoginMediator() ); // CAREFUL: Can't reference Facade instance in constructor // of new Mediators from here, since this is a step // in Facade construction! Usually, all Mediators need // receive notifications, and are registered elsewhere in // the app for this reason. } }

See Also:
Model, View, Controller, Notification, Mediator, Proxy, SimpleCommand, MacroCommand

Field Summary
protected  Controller controller
          Reference to the Controller
protected static Facade instance
          The Singleton instance of the Facade
protected  Model model
          Reference to the Model
protected  View view
          Reference to the View
 
Constructor Summary
protected Facade()
          Constructor.
 
Method Summary
static Facade getInstance()
          Facade Singleton Factory method
protected  void initializeController()
          Initialize the Controller.
protected  void initializeFacade()
          Initialize the Singleton Facade instance.
protected  void initializeModel()
          Initialize the Model.
protected  void initializeView()
          Initialize the View.
 void notifyObservers(INotification notification)
          Notify Observers.
 void registerCommand(java.lang.String noteName, java.lang.Class commandClassRef)
          Register an ICommand with the Controller by Notification name.
 void registerMediator(IMediator mediator)
          Register a IMediator with the View.
 void registerProxy(IProxy proxy)
          Register an IProxy with the Model by name.
 void removeCommand(java.lang.String notificationName)
          Remove a previously registered ICommand to INotification mapping from the Controller.
 IMediator removeMediator(java.lang.String mediatorName)
          Remove an IMediator from the View.
 IProxy removeProxy(java.lang.String proxyName)
          Remove an IProxy from the Model by name.
 IMediator retrieveMediator(java.lang.String mediatorName)
          Retrieve an IMediator from the View.
 IProxy retrieveProxy(java.lang.String proxyName)
          Retrieve an IProxy from the Model by name.
 void sendNotification(java.lang.String notificationName, java.lang.Object body, java.lang.String type)
          Create and send an INotification.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

instance

protected static Facade instance
The Singleton instance of the Facade


controller

protected Controller controller
Reference to the Controller


model

protected Model model
Reference to the Model


view

protected View view
Reference to the View

Constructor Detail

Facade

protected Facade()
Constructor.

This IFacade implementation is a Singleton, so you should not call the constructor directly, but instead call the static Singleton Factory method Facade.getInstance()

Throws:
java.lang.Error - Error if Singleton instance has already been constructed
Method Detail

initializeFacade

protected void initializeFacade()
Initialize the Singleton Facade instance.

Called automatically by the constructor. Override in your subclass to do any subclass specific initializations. Be sure to call super.initializeFacade(), though.


getInstance

public static Facade getInstance()
Facade Singleton Factory method

Returns:
the Singleton instance of the Facade

initializeController

protected void initializeController()
Initialize the Controller.

Called by the initializeFacade method. Override this method in your subclass of Facade if one or both of the following are true:

If you don't want to initialize a different IController, call super.initializeController() at the beginning of your method, then register Commands.


initializeModel

protected void initializeModel()
Initialize the Model.

Called by the initializeFacade method. Override this method in your subclass of Facade if one or both of the following are true:

If you don't want to initialize a different IModel, call super.initializeModel() at the beginning of your method, then register Proxys.

Note: This method is rarely overridden; in practice you are more likely to use a Command to create and register Proxys with the Model, since Proxys with mutable data will likely need to send INotifications and thus will likely want to fetch a reference to the Facade during their construction.


initializeView

protected void initializeView()
Initialize the View.

Called by the initializeFacade method. Override this method in your subclass of Facade if one or both of the following are true:

If you don't want to initialize a different IView, call super.initializeView() at the beginning of your method, then register IMediator instances.

Note: This method is rarely overridden; in practice you are more likely to use a Command to create and register Mediators with the View, since IMediator instances will need to send INotifications and thus will likely want to fetch a reference to the Facade during their construction.


registerCommand

public void registerCommand(java.lang.String noteName,
                            java.lang.Class commandClassRef)
Register an ICommand with the Controller by Notification name.

Specified by:
registerCommand in interface IFacade
Parameters:
noteName - the name of the INotification to associate the ICommand with
commandClassRef - a reference to the Class of the ICommand

removeCommand

public void removeCommand(java.lang.String notificationName)
Remove a previously registered ICommand to INotification mapping from the Controller.

Specified by:
removeCommand in interface IFacade
Parameters:
notificationName - the name of the INotification to remove the ICommand mapping for

registerMediator

public void registerMediator(IMediator mediator)
Register a IMediator with the View.

Specified by:
registerMediator in interface IFacade
Parameters:
mediator - the name to associate with this IMediator

registerProxy

public void registerProxy(IProxy proxy)
Register an IProxy with the Model by name.

Specified by:
registerProxy in interface IFacade
Parameters:
proxy - the name of the IProxy instance to be registered with the Model.

removeMediator

public IMediator removeMediator(java.lang.String mediatorName)
Remove an IMediator from the View.

Specified by:
removeMediator in interface IFacade
Parameters:
mediatorName - name of the IMediator to be removed.
Returns:
the IMediator that was removed from the View

removeProxy

public IProxy removeProxy(java.lang.String proxyName)
Remove an IProxy from the Model by name.

Specified by:
removeProxy in interface IFacade
Parameters:
proxyName - the IProxy to remove from the Model.
Returns:
the IProxy that was removed from the Model

retrieveMediator

public IMediator retrieveMediator(java.lang.String mediatorName)
Retrieve an IMediator from the View.

Specified by:
retrieveMediator in interface IFacade
Parameters:
mediatorName -
Returns:
the IMediator previously registered with the given mediatorName.

retrieveProxy

public IProxy retrieveProxy(java.lang.String proxyName)
Retrieve an IProxy from the Model by name.

Specified by:
retrieveProxy in interface IFacade
Parameters:
proxyName - the name of the proxy to be retrieved.
Returns:
the IProxy instance previously registered with the given proxyName.

sendNotification

public void sendNotification(java.lang.String notificationName,
                             java.lang.Object body,
                             java.lang.String type)
Create and send an INotification.

Keeps us from having to construct new notification instances in our implementation code.

Specified by:
sendNotification in interface INotifier
Parameters:
notificationName - the name of the notiification to send
body - the body of the notification (optional)
type - the type of the notification (optional)

notifyObservers

public void notifyObservers(INotification notification)
Notify Observers.

Specified by:
notifyObservers in interface IFacade
Parameters:
notification - the INotification to have the View notify Observers of.