/*
PureMVC - Copyright(c) 2006-08 Futurescale, Inc., Some rights reserved.
Your reuse is governed by the Creative Commons Attribution 3.0 United States License
*/
package org.puremvc.as3.patterns.facade
{
import org.puremvc.as3.core.*;
import org.puremvc.as3.interfaces.*;
import org.puremvc.as3.patterns.observer.Notification;
/**
* A base Singleton IFacade
implementation.
*
*
* In PureMVC, the Facade
class assumes these
* responsibilities:
*
Model
, View
* and Controller
Singletons.IModel,
* IView, & IController
interfaces.Model
,
* View
and Controller
Singletons created.Commands
and notifying Observers
* Example usage: *
* 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 Error if Singleton instance has already been constructed
*
*/
public function Facade( ) {
if (instance != null) throw Error(SINGLETON_MSG);
instance = this;
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.
Controller
.
*
*
* Called by the initializeFacade
method.
* Override this method in your subclass of Facade
* if one or both of the following are true:
*
IController
.Commands
to register with the Controller
at startup.. IController
,
* call super.initializeController()
at the beginning of your
* method, then register Command
s.
*
*/
protected function initializeController( ):void {
if ( controller != null ) return;
controller = Controller.getInstance();
}
/**
* 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:
*
IModel
.Proxy
s to register with the Model that do not
* retrieve a reference to the Facade at construction time.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.
*
View
.
*
*
* Called by the initializeFacade
method.
* Override this method in your subclass of Facade
* if one or both of the following are true:
*
IView
.Observers
to register with the View
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.
*
ICommand
with the Controller
by Notification name.
*
* @param notificationName the name of the INotification
to associate the ICommand
with
* @param commandClassRef a reference to the Class of the ICommand
*/
public function registerCommand( notificationName:String, commandClassRef:Class ):void
{
controller.registerCommand( notificationName, commandClassRef );
}
/**
* Remove a previously registered ICommand
to INotification
mapping from the Controller.
*
* @param notificationName the name of the INotification
to remove the ICommand
mapping for
*/
public function removeCommand( notificationName:String ):void
{
controller.removeCommand( notificationName );
}
/**
* Check if a Command is registered for a given Notification
*
* @param notificationName
* @return whether a Command is currently registered for the given notificationName
.
*/
public function hasCommand( notificationName:String ) : Boolean
{
return controller.hasCommand(notificationName);
}
/**
* Register an IProxy
with the Model
by name.
*
* @param proxyName the name of the IProxy
.
* @param proxy the IProxy
instance to be registered with the Model
.
*/
public function registerProxy ( proxy:IProxy ):void
{
model.registerProxy ( proxy );
}
/**
* Retrieve an IProxy
from the Model
by name.
*
* @param proxyName the name of the proxy to be retrieved.
* @return the IProxy
instance previously registered with the given proxyName
.
*/
public function retrieveProxy ( proxyName:String ):IProxy
{
return model.retrieveProxy ( proxyName );
}
/**
* Remove an IProxy
from the Model
by name.
*
* @param proxyName the IProxy
to remove from the Model
.
* @return the IProxy
that was removed from the Model
*/
public function removeProxy ( proxyName:String ):IProxy
{
var proxy:IProxy;
if ( model != null ) proxy = model.removeProxy ( proxyName );
return proxy
}
/**
* Check if a Proxy is registered
*
* @param proxyName
* @return whether a Proxy is currently registered with the given proxyName
.
*/
public function hasProxy( proxyName:String ) : Boolean
{
return model.hasProxy( proxyName );
}
/**
* Register a IMediator
with the View
.
*
* @param mediatorName the name to associate with this IMediator
* @param mediator a reference to the IMediator
*/
public function registerMediator( mediator:IMediator ):void
{
if ( view != null ) view.registerMediator( mediator );
}
/**
* Retrieve an IMediator
from the View
.
*
* @param mediatorName
* @return the IMediator
previously registered with the given mediatorName
.
*/
public function retrieveMediator( mediatorName:String ):IMediator
{
return view.retrieveMediator( mediatorName ) as IMediator;
}
/**
* Remove an IMediator
from the View
.
*
* @param mediatorName name of the IMediator
to be removed.
* @return the IMediator
that was removed from the View
*/
public function removeMediator( mediatorName:String ) : IMediator
{
var mediator:IMediator;
if ( view != null ) mediator = view.removeMediator( mediatorName );
return mediator;
}
/**
* Check if a Mediator is registered or not
*
* @param mediatorName
* @return whether a Mediator is registered with the given mediatorName
.
*/
public function hasMediator( mediatorName:String ) : Boolean
{
return view.hasMediator( mediatorName );
}
/**
* Create and send an INotification
.
*
*
* Keeps us from having to construct new notification
* instances in our implementation code.
* @param notificationName the name of the notiification to send
* @param body the body of the notification (optional)
* @param type the type of the notification (optional)
*/
public function sendNotification( notificationName:String, body:Object=null, type:String=null ):void
{
notifyObservers( new Notification( notificationName, body, type ) );
}
/**
* Notify Observer
s.
*
* This method is left public mostly for backward * compatibility, and to allow you to send custom * notification classes using the facade.
** Usually you should just call sendNotification * and pass the parameters, never having to * construct the notification yourself.
* * @param notification theINotification
to have the View
notify Observers
of.
*/
public function notifyObservers ( notification:INotification ):void {
if ( view != null ) view.notifyObservers( notification );
}
// Private references to Model, View and Controller
protected var controller : IController;
protected var model : IModel;
protected var view : IView;
// The Singleton Facade instance.
protected static var instance : IFacade;
// Message Constants
protected const SINGLETON_MSG : String = "Facade Singleton already constructed!";
}
}