home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / Beez.swf / scripts / org / puremvc / as3 / patterns / facade / Facade.as
Encoding:
Text File  |  2008-09-03  |  4.3 KB  |  165 lines

  1. package org.puremvc.as3.patterns.facade
  2. {
  3.    import org.puremvc.as3.core.controller.Controller;
  4.    import org.puremvc.as3.core.model.Model;
  5.    import org.puremvc.as3.core.view.View;
  6.    import org.puremvc.as3.interfaces.IController;
  7.    import org.puremvc.as3.interfaces.IFacade;
  8.    import org.puremvc.as3.interfaces.IMediator;
  9.    import org.puremvc.as3.interfaces.IModel;
  10.    import org.puremvc.as3.interfaces.INotification;
  11.    import org.puremvc.as3.interfaces.IProxy;
  12.    import org.puremvc.as3.interfaces.IView;
  13.    import org.puremvc.as3.patterns.observer.Notification;
  14.    
  15.    public class Facade implements IFacade
  16.    {
  17.       
  18.       protected static var instance:IFacade;
  19.        
  20.       
  21.       protected const SINGLETON_MSG:String = "Facade Singleton already constructed!";
  22.       
  23.       protected var controller:IController;
  24.       
  25.       protected var view:IView;
  26.       
  27.       protected var model:IModel;
  28.       
  29.       public function Facade()
  30.       {
  31.          super();
  32.          if(instance != null)
  33.          {
  34.             throw Error(SINGLETON_MSG);
  35.          }
  36.          instance = this;
  37.          initializeFacade();
  38.       }
  39.       
  40.       public static function getInstance() : IFacade
  41.       {
  42.          if(instance == null)
  43.          {
  44.             instance = new Facade();
  45.          }
  46.          return instance;
  47.       }
  48.       
  49.       public function removeProxy(proxyName:String) : IProxy
  50.       {
  51.          var proxy:IProxy = null;
  52.          if(model != null)
  53.          {
  54.             proxy = model.removeProxy(proxyName);
  55.          }
  56.          return proxy;
  57.       }
  58.       
  59.       public function registerProxy(proxy:IProxy) : void
  60.       {
  61.          model.registerProxy(proxy);
  62.       }
  63.       
  64.       protected function initializeController() : void
  65.       {
  66.          if(controller != null)
  67.          {
  68.             return;
  69.          }
  70.          controller = Controller.getInstance();
  71.       }
  72.       
  73.       protected function initializeFacade() : void
  74.       {
  75.          initializeModel();
  76.          initializeController();
  77.          initializeView();
  78.       }
  79.       
  80.       public function retrieveProxy(proxyName:String) : IProxy
  81.       {
  82.          return model.retrieveProxy(proxyName);
  83.       }
  84.       
  85.       public function sendNotification(notificationName:String, body:Object = null, type:String = null) : void
  86.       {
  87.          notifyObservers(new Notification(notificationName,body,type));
  88.       }
  89.       
  90.       public function notifyObservers(notification:INotification) : void
  91.       {
  92.          if(view != null)
  93.          {
  94.             view.notifyObservers(notification);
  95.          }
  96.       }
  97.       
  98.       protected function initializeView() : void
  99.       {
  100.          if(view != null)
  101.          {
  102.             return;
  103.          }
  104.          view = View.getInstance();
  105.       }
  106.       
  107.       public function retrieveMediator(mediatorName:String) : IMediator
  108.       {
  109.          return view.retrieveMediator(mediatorName) as IMediator;
  110.       }
  111.       
  112.       public function removeMediator(mediatorName:String) : IMediator
  113.       {
  114.          var mediator:IMediator = null;
  115.          if(view != null)
  116.          {
  117.             mediator = view.removeMediator(mediatorName);
  118.          }
  119.          return mediator;
  120.       }
  121.       
  122.       public function hasCommand(notificationName:String) : Boolean
  123.       {
  124.          return controller.hasCommand(notificationName);
  125.       }
  126.       
  127.       public function removeCommand(notificationName:String) : void
  128.       {
  129.          controller.removeCommand(notificationName);
  130.       }
  131.       
  132.       public function registerCommand(notificationName:String, commandClassRef:Class) : void
  133.       {
  134.          controller.registerCommand(notificationName,commandClassRef);
  135.       }
  136.       
  137.       public function hasMediator(mediatorName:String) : Boolean
  138.       {
  139.          return view.hasMediator(mediatorName);
  140.       }
  141.       
  142.       public function registerMediator(mediator:IMediator) : void
  143.       {
  144.          if(view != null)
  145.          {
  146.             view.registerMediator(mediator);
  147.          }
  148.       }
  149.       
  150.       protected function initializeModel() : void
  151.       {
  152.          if(model != null)
  153.          {
  154.             return;
  155.          }
  156.          model = Model.getInstance();
  157.       }
  158.       
  159.       public function hasProxy(proxyName:String) : Boolean
  160.       {
  161.          return model.hasProxy(proxyName);
  162.       }
  163.    }
  164. }
  165.