public class ApplicationFacade extends Facade implements IFacade
{
public static const STARTUP:String = "startup";
public static function getInstance() : ApplicationFacade {
if ( instance == null ) instance = new ApplicationFacade( );
return instance as ApplicationFacade;
}
override protected function initializeController( ) : void
{
super.initializeController();
registerCommand( STARTUP, StartupCommand );
}
public function startup( stage:Object ):void
{
sendNotification( STARTUP, stage );
}
}</pre><br />
</span></div>
</div>
1.定義了ApplicationFacade子類后,就可以通過ApplicationFacade.getInstance().startup( this.stage )來啟動系統了。
2.可以通過override方法initializeModel和initializeView方法來引入Proxy和Mediator,但在實踐中我們通常是在StartupCommand中進行相關操作。且除了必須要在啟動的時候注冊的Command比如StartupCommand外,其他的Command最好都不要在這里注冊。
三.Proxy, Command,Mediator和Notifier
Notifier是一個引用了facade對象的子類,且封裝了facade對象的sendNotification方法。而Proxy, Command和Mediator都繼承自Notifier,這使得這兩個類都能夠發送消息,以使用PureMVC的消息系統。且能夠使用facade對象的接口來調用一些方法,比如注冊和獲取Proxy數據源,注冊Mediator中介等。
四.Proxy和Modal
每個Proxy子類都封裝了對一種數據的操作,多個子類則封裝了多種數據。Modal主要通過維護proxyMap來注冊和獲取數據源,而 Facade也提供了對proxyMap進行操作的接口。Command和Mediator也能隨時通過facade來注冊或獲取需要的數據源。在一個系統中,同一個名稱的Proxy只能有一個實例對象。
PureMVC的這一設計,使得數據源可以輕松的被獲取,操作,此外,Proxy本身也繼承了Notifier的sendNotification方法,當Proxy數據更改時能夠通知相關視圖,使多個視圖能夠共用同一數據源。
在實現Proxy時應該注意,Proxy和視圖的通信應該僅通過sendNotification方法,Proxy不需要知道視圖的狀態,不應該對視圖產生依賴。
五.Mediator,Observer和View
Mediator是系統和視圖之間的中介。View主要通過維護MediatorMap和ObserverMap來支持系統和視圖之間的通信。其中 ObserverMap保存了一個Notifiction消息和Observer數組之間的對應關系,這是利用觀察者模式來實現的消息系統。而 MediatorMap則保存了系統中所有的Mediator,當每個Mediator被加入系統中時,系統都會通過 listNotificationInterests方法查看該Mediator關注哪些消息,并將Mediator加入相應的觀察者中,將其 handleNotification方法注冊為其關注的消息的處理函數。
在實現Mediator時,主要注意的就是需要重寫listNotificationInterests和handleNotification方法,且提供對視圖事件的EventListerner。
六.Command和Cotroller
Command主要負責Proxy和Mediator之間的交互,Controller通過commandMap來保存Notification和 command之間的關系,在registerCommand的同時,會將Controller加入對應Notifiction消息的觀察者中,并使用 Command來處理消息。
Command分為SingleCommand和MacroCommand。MacroCommand讓你可以順序執行多個Command。每個執行都會創建一個Command對象并傳參一個對源Notification的引用。
MacroCommand 在構造方法調用自身的initializeMacroCommand方法。實際應用中,你需重寫這個方法,調用addSubCommand添加子 Command。你可以任意組合SimpleCommand和MacroCommand成為一個新的Command。
</div>
作者:Jingle Guo
出處:http://www.cnblogs.com/studynote/