PureMVC AS3 Framework - [Standard Version] | All Packages | All Classes | Index | Frames No Frames |
Class Facade

Package | org.puremvc.as3.patterns.facade
Class | public class Facade
Implements | IFacade

A base Singleton `IFacade` implementation.

In PureMVC, the `Facade` class assumes these responsibilities:

- Initializing the `Model`, `View` and `Controller` Singletons.
- Providing all the methods defined by the `IModel, IView, & IController` interfaces.
- Providing the ability to override the specific `Model`, `View` and `Controller` Singletons created.
- Providing a single point of contact to the application for registering `Commands` and notifying `Observers`

Example usage:

      import org.puremvc.as3.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

Protected Properties

| Property | Defined by
--- | --- | ---
|  | controller : IController | Facade
|  | instance : IFacade [static] | Facade
|  | model : IModel | Facade
|  | view : IView | Facade

Public Methods

| Method | Defined by
--- | --- | ---
|  | Facade() Constructor. | Facade
|  | getInstance():IFacade [static] Facade Singleton Factory method | Facade
|  | hasCommand(notificationName:String):Boolean Check if a Command is registered for a given Notification | Facade
|  | hasMediator(mediatorName:String):Boolean Check if a Mediator is registered or not | Facade
|  | hasProxy(proxyName:String):Boolean Check if a Proxy is registered | Facade
|  | notifyObservers(notification:INotification):void Notify `Observer`s. | Facade
|  | registerCommand(notificationName:String, commandClassRef:Class):void Register an `ICommand` with the `Controller` by Notification name. | Facade
|  | registerMediator(mediator:IMediator):void Register a `IMediator` with the `View`. | Facade
|  | registerProxy(proxy:IProxy):void Register an `IProxy` with the `Model` by name. | Facade
|  | removeCommand(notificationName:String):void Remove a previously registered `ICommand` to `INotification` mapping from the Controller. | Facade
|  | removeMediator(mediatorName:String):IMediator Remove an `IMediator` from the `View`. | Facade
|  | removeProxy(proxyName:String):IProxy Remove an `IProxy` from the `Model` by name. | Facade
|  | retrieveMediator(mediatorName:String):IMediator Retrieve an `IMediator` from the `View`. | Facade
|  | retrieveProxy(proxyName:String):IProxy Retrieve an `IProxy` from the `Model` by name. | Facade
|  | sendNotification(notificationName:String, body:Object = null, type:String = null):void Create and send an `INotification`. | Facade

Protected Methods

| Method | Defined by
--- | --- | ---
|  | initializeController():void Initialize the `Controller`. | Facade
|  | initializeFacade():void Initialize the Singleton `Facade` instance. | Facade
|  | initializeModel():void Initialize the `Model`. | Facade
|  | initializeView():void Initialize the `View`. | Facade

Protected Constants

| Constant | Defined by
--- | --- | ---
|  | SINGLETON_MSG : String = "Facade Singleton already constructed!" | Facade

Property detail

controller | property

`protected var controller:IController`

instance | property |

`protected static var instance:IFacade`

model | property |

`protected var model:IModel`

view | property |

`protected var view:IView`
Constructor detail

Facade | () | constructor

`public function 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

| `` — Error if Singleton instance has already been constructed

Method detail

getInstance | () | method

`public static function getInstance():IFacade`

Facade Singleton Factory method

Returns

| `IFacade` — the Singleton instance of the Facade

hasCommand | () | method |

`public function hasCommand(notificationName:String):Boolean`

Check if a Command is registered for a given Notification

Parameters

| `notificationName:String`

Returns

| `Boolean` — whether a Command is currently registered for the given `notificationName`.

hasMediator | () | method |

`public function hasMediator(mediatorName:String):Boolean`

Check if a Mediator is registered or not

Parameters

| `mediatorName:String`

Returns

| `Boolean` — whether a Mediator is registered with the given `mediatorName`.

hasProxy | () | method |

`public function hasProxy(proxyName:String):Boolean`

Check if a Proxy is registered

Parameters

| `proxyName:String`

Returns

| `Boolean` — whether a Proxy is currently registered with the given `proxyName`.

initializeController | () | method |

`protected function initializeController():void`

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:

- You wish to initialize a different `IController`.
- You have `Commands` to register with the `Controller` at startup..

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

initializeFacade | () | method |

`protected function initializeFacade():void`

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.

initializeModel | () | method |

`protected function initializeModel():void`

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:

- You wish to initialize a different `IModel`.
- You have `Proxy`s to register with the Model that do not retrieve a reference to the Facade at construction time.

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

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

initializeView | () | method |

`protected function initializeView():void`

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:

- You wish to initialize a different `IView`.
- You have `Observers` to register with the `View`

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 `Mediator`s with the `View`, since `IMediator` instances will need to send `INotification`s and thus will likely want to fetch a reference to the `Facade` during their construction.

notifyObservers | () | method |

`public function notifyObservers(notification:INotification):void`

Notify `Observer`s.

This method is left public mostly for backward compatibility, and to allow you to send custom notification classes using the facade.

P> Usually you should just call sendNotification and pass the parameters, never having to construct the notification yourself. Parameters

| `notification:INotification` — the `INotification` to have the `View` notify `Observers` of.

registerCommand | () | method |

`public function registerCommand(notificationName:String, commandClassRef:Class):void`

Register an `ICommand` with the `Controller` by Notification name.

Parameters

| `notificationName:String` — the name of the `INotification` to associate the `ICommand` with
| `commandClassRef:Class` — a reference to the Class of the `ICommand`

registerMediator | () | method |

`public function registerMediator(mediator:IMediator):void`

Register a `IMediator` with the `View`.

Parameters

| `mediator:IMediator` — the name to associate with this `IMediator`

registerProxy | () | method |

`public function registerProxy(proxy:IProxy):void`

Register an `IProxy` with the `Model` by name.

Parameters

| `proxy:IProxy` — the name of the `IProxy`.

removeCommand | () | method |

`public function removeCommand(notificationName:String):void`

Remove a previously registered `ICommand` to `INotification` mapping from the Controller.

Parameters

| `notificationName:String` — the name of the `INotification` to remove the `ICommand` mapping for

removeMediator | () | method |

`public function removeMediator(mediatorName:String):IMediator`

Remove an `IMediator` from the `View`.

Parameters

| `mediatorName:String` — name of the `IMediator` to be removed.

Returns

| `IMediator` — the `IMediator` that was removed from the `View`

removeProxy | () | method |

`public function removeProxy(proxyName:String):IProxy`

Remove an `IProxy` from the `Model` by name.

Parameters

| `proxyName:String` — the `IProxy` to remove from the `Model`.

Returns

| `IProxy` — the `IProxy` that was removed from the `Model`

retrieveMediator | () | method |

`public function retrieveMediator(mediatorName:String):IMediator`

Retrieve an `IMediator` from the `View`.

Parameters

| `mediatorName:String`

Returns

| `IMediator` — the `IMediator` previously registered with the given `mediatorName`.

retrieveProxy | () | method |

`public function retrieveProxy(proxyName:String):IProxy`

Retrieve an `IProxy` from the `Model` by name.

Parameters

| `proxyName:String` — the name of the proxy to be retrieved.

Returns

| `IProxy` — the `IProxy` instance previously registered with the given `proxyName`.

sendNotification | () | method |

`public function sendNotification(notificationName:String, body:Object = null, type:String = null):void`

Create and send an `INotification`.

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

Parameters

| `notificationName:String` — the name of the notiification to send
| `body:Object` (default = `null`)`` — the body of the notification (optional)
| `type:String` (default = `null`)`` — the type of the notification (optional)

Constant detail

SINGLETON_MSG | constant

`protected const SINGLETON_MSG:String = "Facade Singleton already constructed!"`