PureMVC(JS版)源碼解析(五):SimpleCommand類

      以前咱們對PureMVC中涉及到觀察者模式的三個基本類(Notification/Observer/Notifier)進行了分析,接下來將對PureMVC源碼中的其餘類進行分析,首先咱們講解SimpleCommand類。mvc

      SimpleCommand在MVC類中屬於C部分,用於一些複雜的邏輯處理,SimpleCommand類在PureMVC被設計成了一個雙面角色,它既能夠扮演通知者(Notifier)的角色(便可以發送Notification),也能夠扮演觀察者(Observer)接受消息。
 
經過分析源碼可知,SimpleCommand類,繼承了Notifier類:
function SimpleCommand () { };

SimpleCommand.prototype= new Notifier;
SimpleCommand.prototype.constructor= SimpleCommand;

因爲繼承了Notifier類,SimpleCommand類繼承了Notifier類的sendNotification()方法,故說它能夠扮演通知者角色。app

另外,經過源碼咱們發現,SimpleCommand類,還有個execute()方法:
/**
 * Fulfill the use-case initiated by the given Notification
 * 
 * In the Command Pattern, an application use-case typically begins with some
 * user action, which results in a Notification is handled by the business logic
 * in the #execute method of a command.
 * 
 * @param {puremvc.Notification} notification
 *  The notification to handle.
 * @return {void}
 */
SimpleCommand.prototype.execute= function (notification) { };

execute()方法,接受一個notifcation對象做爲參數,可是至於什麼時候會調用execute()方法咱們如今還不清楚,先放着,記着SimpleCommand有這個方法。spa

由於execute方法接受一個notification對象做爲參數,所以,SimpleCommand類能夠接受消息。 
 
SimpleCommand類的結構很簡單,咱們暫時只須要記住SimpleCommand類繼承自Notifier類,有一個execute方法,execute方法接受一個notification對象做爲參數。
 
最後,附上一張思惟導圖:
相關文章
相關標籤/搜索