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对象做为参数。
 
最后,附上一张思惟导图:
相关文章
相关标签/搜索