root/tags/1.0.4/src/org/puremvc/as3/multicore/patterns/facade/Facade.as
| Revision 46, 11.6 KB (checked in by puremvc, 2 years ago) |
|---|
| Line | |
|---|---|
| 1 | /* |
| 2 | PureMVC MultiCore - Copyright(c) 2006-08 Futurescale, Inc., Some rights reserved. |
| 3 | Your reuse is governed by the Creative Commons Attribution 3.0 United States License |
| 4 | */ |
| 5 | package org.puremvc.as3.multicore.patterns.facade |
| 6 | { |
| 7 | import org.puremvc.as3.multicore.core.*; |
| 8 | import org.puremvc.as3.multicore.interfaces.*; |
| 9 | import org.puremvc.as3.multicore.patterns.observer.*; |
| 10 | |
| 11 | /** |
| 12 | * A base Multiton <code>IFacade</code> implementation. |
| 13 | * |
| 14 | * @see org.puremvc.as3.multicore.core.Model Model |
| 15 | * @see org.puremvc.as3.multicore.core.View View |
| 16 | * @see org.puremvc.as3.multicore.core.Controller Controller |
| 17 | */ |
| 18 | public class Facade implements IFacade |
| 19 | { |
| 20 | /** |
| 21 | * Constructor. |
| 22 | * |
| 23 | * <P> |
| 24 | * This <code>IFacade</code> implementation is a Multiton, |
| 25 | * so you should not call the constructor |
| 26 | * directly, but instead call the static Factory method, |
| 27 | * passing the unique key for this instance |
| 28 | * <code>Facade.getInstance( multitonKey )</code> |
| 29 | * |
| 30 | * @throws Error Error if instance for this Multiton key has already been constructed |
| 31 | * |
| 32 | */ |
| 33 | public function Facade( key:String ) { |
| 34 | if (instanceMap[ key ] != null) throw Error(MULTITON_MSG); |
| 35 | initializeNotifier( key ); |
| 36 | instanceMap[ multitonKey ] = this; |
| 37 | initializeFacade(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Initialize the Multiton <code>Facade</code> instance. |
| 42 | * |
| 43 | * <P> |
| 44 | * Called automatically by the constructor. Override in your |
| 45 | * subclass to do any subclass specific initializations. Be |
| 46 | * sure to call <code>super.initializeFacade()</code>, though.</P> |
| 47 | */ |
| 48 | protected function initializeFacade( ):void { |
| 49 | initializeModel(); |
| 50 | initializeController(); |
| 51 | initializeView(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Facade Multiton Factory method |
| 56 | * |
| 57 | * @return the Multiton instance of the Facade |
| 58 | */ |
| 59 | public static function getInstance( key:String ):IFacade { |
| 60 | if (instanceMap[ key ] == null ) instanceMap[ key ] = new Facade( key ); |
| 61 | return instanceMap[ key ]; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Initialize the <code>Controller</code>. |
| 66 | * |
| 67 | * <P> |
| 68 | * Called by the <code>initializeFacade</code> method. |
| 69 | * Override this method in your subclass of <code>Facade</code> |
| 70 | * if one or both of the following are true: |
| 71 | * <UL> |
| 72 | * <LI> You wish to initialize a different <code>IController</code>.</LI> |
| 73 | * <LI> You have <code>Commands</code> to register with the <code>Controller</code> at startup.</code>. </LI> |
| 74 | * </UL> |
| 75 | * If you don't want to initialize a different <code>IController</code>, |
| 76 | * call <code>super.initializeController()</code> at the beginning of your |
| 77 | * method, then register <code>Command</code>s. |
| 78 | * </P> |
| 79 | */ |
| 80 | protected function initializeController( ):void { |
| 81 | if ( controller != null ) return; |
| 82 | controller = Controller.getInstance( multitonKey ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Initialize the <code>Model</code>. |
| 87 | * |
| 88 | * <P> |
| 89 | * Called by the <code>initializeFacade</code> method. |
| 90 | * Override this method in your subclass of <code>Facade</code> |
| 91 | * if one or both of the following are true: |
| 92 | * <UL> |
| 93 | * <LI> You wish to initialize a different <code>IModel</code>.</LI> |
| 94 | * <LI> You have <code>Proxy</code>s to register with the Model that do not |
| 95 | * retrieve a reference to the Facade at construction time.</code></LI> |
| 96 | * </UL> |
| 97 | * If you don't want to initialize a different <code>IModel</code>, |
| 98 | * call <code>super.initializeModel()</code> at the beginning of your |
| 99 | * method, then register <code>Proxy</code>s. |
| 100 | * <P> |
| 101 | * Note: This method is <i>rarely</i> overridden; in practice you are more |
| 102 | * likely to use a <code>Command</code> to create and register <code>Proxy</code>s |
| 103 | * with the <code>Model</code>, since <code>Proxy</code>s with mutable data will likely |
| 104 | * need to send <code>INotification</code>s and thus will likely want to fetch a reference to |
| 105 | * the <code>Facade</code> during their construction. |
| 106 | * </P> |
| 107 | */ |
| 108 | protected function initializeModel( ):void { |
| 109 | if ( model != null ) return; |
| 110 | model = Model.getInstance( multitonKey ); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /** |
| 115 | * Initialize the <code>View</code>. |
| 116 | * |
| 117 | * <P> |
| 118 | * Called by the <code>initializeFacade</code> method. |
| 119 | * Override this method in your subclass of <code>Facade</code> |
| 120 | * if one or both of the following are true: |
| 121 | * <UL> |
| 122 | * <LI> You wish to initialize a different <code>IView</code>.</LI> |
| 123 | * <LI> You have <code>Observers</code> to register with the <code>View</code></LI> |
| 124 | * </UL> |
| 125 | * If you don't want to initialize a different <code>IView</code>, |
| 126 | * call <code>super.initializeView()</code> at the beginning of your |
| 127 | * method, then register <code>IMediator</code> instances. |
| 128 | * <P> |
| 129 | * Note: This method is <i>rarely</i> overridden; in practice you are more |
| 130 | * likely to use a <code>Command</code> to create and register <code>Mediator</code>s |
| 131 | * with the <code>View</code>, since <code>IMediator</code> instances will need to send |
| 132 | * <code>INotification</code>s and thus will likely want to fetch a reference |
| 133 | * to the <code>Facade</code> during their construction. |
| 134 | * </P> |
| 135 | */ |
| 136 | protected function initializeView( ):void { |
| 137 | if ( view != null ) return; |
| 138 | view = View.getInstance( multitonKey ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Register an <code>ICommand</code> with the <code>Controller</code> by Notification name. |
| 143 | * |
| 144 | * @param notificationName the name of the <code>INotification</code> to associate the <code>ICommand</code> with |
| 145 | * @param commandClassRef a reference to the Class of the <code>ICommand</code> |
| 146 | */ |
| 147 | public function registerCommand( notificationName:String, commandClassRef:Class ):void |
| 148 | { |
| 149 | controller.registerCommand( notificationName, commandClassRef ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Remove a previously registered <code>ICommand</code> to <code>INotification</code> mapping from the Controller. |
| 154 | * |
| 155 | * @param notificationName the name of the <code>INotification</code> to remove the <code>ICommand</code> mapping for |
| 156 | */ |
| 157 | public function removeCommand( notificationName:String ):void |
| 158 | { |
| 159 | controller.removeCommand( notificationName ); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Check if a Command is registered for a given Notification |
| 164 | * |
| 165 | * @param notificationName |
| 166 | * @return whether a Command is currently registered for the given <code>notificationName</code>. |
| 167 | */ |
| 168 | public function hasCommand( notificationName:String ) : Boolean |
| 169 | { |
| 170 | return controller.hasCommand(notificationName); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Register an <code>IProxy</code> with the <code>Model</code> by name. |
| 175 | * |
| 176 | * @param proxyName the name of the <code>IProxy</code>. |
| 177 | * @param proxy the <code>IProxy</code> instance to be registered with the <code>Model</code>. |
| 178 | */ |
| 179 | public function registerProxy ( proxy:IProxy ):void |
| 180 | { |
| 181 | model.registerProxy ( proxy ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Retrieve an <code>IProxy</code> from the <code>Model</code> by name. |
| 186 | * |
| 187 | * @param proxyName the name of the proxy to be retrieved. |
| 188 | * @return the <code>IProxy</code> instance previously registered with the given <code>proxyName</code>. |
| 189 | */ |
| 190 | public function retrieveProxy ( proxyName:String ):IProxy |
| 191 | { |
| 192 | return model.retrieveProxy ( proxyName ); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Remove an <code>IProxy</code> from the <code>Model</code> by name. |
| 197 | * |
| 198 | * @param proxyName the <code>IProxy</code> to remove from the <code>Model</code>. |
| 199 | * @return the <code>IProxy</code> that was removed from the <code>Model</code> |
| 200 | */ |
| 201 | public function removeProxy ( proxyName:String ):IProxy |
| 202 | { |
| 203 | var proxy:IProxy; |
| 204 | if ( model != null ) proxy = model.removeProxy ( proxyName ); |
| 205 | return proxy |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Check if a Proxy is registered |
| 210 | * |
| 211 | * @param proxyName |
| 212 | * @return whether a Proxy is currently registered with the given <code>proxyName</code>. |
| 213 | */ |
| 214 | public function hasProxy( proxyName:String ) : Boolean |
| 215 | { |
| 216 | return model.hasProxy( proxyName ); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Register a <code>IMediator</code> with the <code>View</code>. |
| 221 | * |
| 222 | * @param mediatorName the name to associate with this <code>IMediator</code> |
| 223 | * @param mediator a reference to the <code>IMediator</code> |
| 224 | */ |
| 225 | public function registerMediator( mediator:IMediator ):void |
| 226 | { |
| 227 | if ( view != null ) view.registerMediator( mediator ); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Retrieve an <code>IMediator</code> from the <code>View</code>. |
| 232 | * |
| 233 | * @param mediatorName |
| 234 | * @return the <code>IMediator</code> previously registered with the given <code>mediatorName</code>. |
| 235 | */ |
| 236 | public function retrieveMediator( mediatorName:String ):IMediator |
| 237 | { |
| 238 | return view.retrieveMediator( mediatorName ) as IMediator; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Remove an <code>IMediator</code> from the <code>View</code>. |
| 243 | * |
| 244 | * @param mediatorName name of the <code>IMediator</code> to be removed. |
| 245 | * @return the <code>IMediator</code> that was removed from the <code>View</code> |
| 246 | */ |
| 247 | public function removeMediator( mediatorName:String ) : IMediator |
| 248 | { |
| 249 | var mediator:IMediator; |
| 250 | if ( view != null ) mediator = view.removeMediator( mediatorName ); |
| 251 | return mediator; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Check if a Mediator is registered or not |
| 256 | * |
| 257 | * @param mediatorName |
| 258 | * @return whether a Mediator is registered with the given <code>mediatorName</code>. |
| 259 | */ |
| 260 | public function hasMediator( mediatorName:String ) : Boolean |
| 261 | { |
| 262 | return view.hasMediator( mediatorName ); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Create and send an <code>INotification</code>. |
| 267 | * |
| 268 | * <P> |
| 269 | * Keeps us from having to construct new notification |
| 270 | * instances in our implementation code. |
| 271 | * @param notificationName the name of the notiification to send |
| 272 | * @param body the body of the notification (optional) |
| 273 | * @param type the type of the notification (optional) |
| 274 | */ |
| 275 | public function sendNotification( notificationName:String, body:Object=null, type:String=null ):void |
| 276 | { |
| 277 | notifyObservers( new Notification( notificationName, body, type ) ); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Notify <code>Observer</code>s. |
| 282 | * <P> |
| 283 | * This method is left public mostly for backward |
| 284 | * compatibility, and to allow you to send custom |
| 285 | * notification classes using the facade.</P> |
| 286 | *<P> |
| 287 | * Usually you should just call sendNotification |
| 288 | * and pass the parameters, never having to |
| 289 | * construct the notification yourself.</P> |
| 290 | * |
| 291 | * @param notification the <code>INotification</code> to have the <code>View</code> notify <code>Observers</code> of. |
| 292 | */ |
| 293 | public function notifyObservers ( notification:INotification ):void { |
| 294 | if ( view != null ) view.notifyObservers( notification ); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Set the Multiton key for this facade instance. |
| 299 | * <P> |
| 300 | * Not called directly, but instead from the |
| 301 | * constructor when getInstance is invoked. |
| 302 | * It is necessary to be public in order to |
| 303 | * implement INotifier.</P> |
| 304 | */ |
| 305 | public function initializeNotifier( key:String ):void |
| 306 | { |
| 307 | multitonKey = key; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Remove a Core |
| 312 | * |
| 313 | * @param multitonKey of the Core to remove |
| 314 | */ |
| 315 | public function removeCore( key:String ) : void |
| 316 | { |
| 317 | // remove the model, view, controller |
| 318 | // and facade instances for this key |
| 319 | model.removeModel( key ); |
| 320 | view.removeView( key ); |
| 321 | controller.removeController( key ); |
| 322 | delete instanceMap[ key ]; |
| 323 | } |
| 324 | |
| 325 | // References to Model, View and Controller |
| 326 | protected var controller : IController; |
| 327 | protected var model : IModel; |
| 328 | protected var view : IView; |
| 329 | |
| 330 | // The Multiton Key for this app |
| 331 | protected var multitonKey : String; |
| 332 | |
| 333 | // The Multiton Facade instanceMap. |
| 334 | protected static var instanceMap : Array = new Array(); |
| 335 | |
| 336 | // Message Constants |
| 337 | protected const MULTITON_MSG:String = "Facade instance for this Multiton key already constructed!"; |
| 338 | |
| 339 | } |
| 340 | } |
Note: See TracBrowser
for help on using the browser.
