上一篇博客,我们讲解了SimpleCommand类,接下来我们看一下与SimpleCommand类很相似的MacroCommand类。

MacroCommand类和SimpleCommand类一样,都继承Notifier类(通知者),都可以发送消息。

/* subclass Notifier */
MacroCommand.prototype= new Notifier;
MacroCommand.prototype.constructor= MacroCommand;

MacroCommand类比SimpleCommand类多了一个subCommands属性。它是干嘛用的呢?其实,通过类名我们就可以看出MacroCommand类和SimpleCommand类的异同。"SimpleCommand"顾名思义可以理解为”简单的命令,"MacroCommand"可以理解为"巨大的、大量的命令".subCommands是一个数组,它里面存放者多个SimpleCommand类和MacroCommand类(我们把SimpleCommand类和MacroCommand类统称为命令类),在MacroCommand对象接收到消息时,他就会依次调用subCommands里面所有命令的execute()方法(执行命令)。

MacroCommand类和SimpleCommand类一样,都有一个execute()方法(执行命令),但是MacroCommand类的execute()方法比SimpleCommand类的execute()要复杂很多:

/**
* Execute this MacroCommands *SubCommands*
*
* The *SubCommand*s will be called in First In / First Out (FIFO) order
* @param {puremvc.Notification} note
* The Notification object to be passed to each *SubCommand*
*/
MacroCommand.prototype.execute= function(note)
{
// SIC- TODO optimize
while(this.subCommands.length > 0)
{
var ref= this.subCommands.shift();
var cmd= new ref;
cmd.initializeNotifier(this.multitonKey);
cmd.execute(note);
}
};

MacroCommand类有一个addSubCommand()方法,用来往subCommands数组里面添加命令类。

/**
* @protected
* Add a *SubCommand*
* The *SubCommand*s will be called in First In / First Out (FIFO) order
* @param {Function} commandClassRef
* A reference to a subclassed SimpleCommand or MacroCommand constructor
*/
MacroCommand.prototype.addSubCommand= function(commandClassRef)
{
this.subCommands.push(commandClassRef);
};

我们再看看MacroCommand类的构造函数:

 /*
* If your subclass does define a constructor, be sure to call "super" like so
*
* function MyMacroCommand ()
* {
* MacroCommand.call(this);
* };
* @constructor
*/
function MacroCommand()
{
this.subCommands= [];
this.initializeMacroCommand();
};

在MacCommand类的构造函数中,先对subCommands属性进行了初始化,然后调用了initializeMacroCommand()方法。【我们注意注释,可以知道我们继承MacCommand类的,需要在自雷的构造函数中调用MacroCommand的构造函数(MacroComand.call(this)】。

我们看看initializeMacroCommand()方法:

MacroCommand.prototype.initializeMacroCommand= function() {}

initializeMacroCommand类,主要是MacroCommand对象的初始化,在继承MacroCommand的子类中我们需要重写这个方法,源码中有这么一段注释:

* In your subclass, override this method to
* initialize the MacroCommand's *SubCommand*
* list with command class references like
* this:
*
* // Initialize MyMacroCommand
* MyMacroCommand.prototype.initializeMacroCommand= function ()
* {
* this.addSubCommand( com.me.myapp.controller.FirstCommand );
* this.addSubCommand( com.me.myapp.controller.SecondCommand );
* this.addSubCommand( com.me.myapp.controller.ThirdCommand );
* };
*
* Note that *SubCommand*s may be any command implementor,
* MacroCommands or SimpleCommands are both acceptable.

其实,initializeMacroCommand类其实就是往subCommands数组中添加命令类(可以是SimpleCommand类也可以是MacroCommand类,记住添加的是类名,不是类的实例化对象,MacroCommand对象接收到消息后,会在execute()方法中实例化这些命令类)。

在实际开发过程中,我们需要写一些复杂的逻辑处理单元(Command类),这写逻辑处理类要么继承SimpleCommand,要么继承MacroCommand类,哪什么时候继承MacroCommand类,什么时候继承SimpleCommand类,需要看我们逻辑的复杂度,如果一个逻辑单元可以拆分为多个子逻辑单元,那我们可以继承MacroCommand类,如果一个逻辑单元就可以处理,那我们只需要继承SimpleCommand类。

关于,SimleCommand类和MacroCommand类的不同,源码中有这么一段注释:

* Unlike {@link puremvc.SimpleCommand SimpleCommand},
* your subclass should not override #execute but instead, should
* override the #initializeMacroCommand method, calling #addSubCommand once for
* each *SubCommand* to be executed.
*

上面的注释说的很清楚,就是继承MacroCommand类的子类需要重写initializeMacroCommand方法,不需要重写execute方法,继承SimpleCommand类的子类需要重写execute方法。

最后,附上MacroCommand类的思维导图:

PureMVC(JS版)源码解析(六):MacroCommand类-LMLPHP

05-02 16:14