上一篇博客,我们讲解了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类的更多相关文章

  1. Celery 源码解析六:Events 的实现

    在 Celery 中,除了远程控制之外,还有一个元素可以让我们对分布式中的任务的状态有所掌控,而且从实际意义上来说,这个元素对 Celery 更为重要,这就是在本文中将要说到的 Event. 在 Ce ...

  2. Mybatis源码解析3——核心类SqlSessionFactory,看完我悟了

    这是昨晚的武汉,晚上九点钟拍的,疫情又一次来袭,曾经熙熙攘攘的夜市也变得冷冷清清,但比前几周要好很多了.希望大家都能保护好自己,保护好身边的人,生活不可能像你想象的那么好,但也不会像你想象的那么糟. ...

  3. AOP源码解析:AspectJAwareAdvisorAutoProxyCreator类的介绍

    AspectJAwareAdvisorAutoProxyCreator 的类图 上图中一些 类/接口 的介绍: AspectJAwareAdvisorAutoProxyCreator : 公开了Asp ...

  4. java源码解析之Object类

    一.Object类概述   Object类是java中类层次的根,是所有类的基类.在编译时会自动导入.Object中的方法如下: 二.方法详解   Object的方法可以分成两类,一类是被关键字fin ...

  5. Netty源码解析 -- 内存对齐类SizeClasses

    在学习Netty内存池之前,我们先了解一下Netty的内存对齐类SizeClasses,它为Netty内存池中的内存块提供大小对齐,索引计算等服务方法. 源码分析基于Netty 4.1.52 Nett ...

  6. AOP源码解析:AspectJExpressionPointcutAdvisor类

    先看看 AspectJExpressionPointcutAdvisor 的类图 再了解一下切点(Pointcut)表达式,它指定触发advice的方法,可以精确到返回参数,参数类型,方法名 1 pa ...

  7. Bulma 源码解析之 .columns 类

    {说明} 这一部分的源码内容被我简化了,另外我还额外添加了一个辅助类 is-grow. .columns // 修饰类 &.is-centered justify-content: cente ...

  8. java源码解析之String类(二)

    上一节主要介绍了String类的一些构造方法,主要分为四类 无参构造器:String(),创建一个空字符串"",区别于null字符串,""已经初始化,null并 ...

  9. java源码解析之String类(一)

    String是我们接触最多的类,无论是学习中还是工作中,基本每天都会和字符串打交道,从字符串本身的各种拼接.切片.变形,再到和其他基本数据类型的转换,几乎无时无刻都在使用它,今天就让我们揭开Strin ...

  10. muduo源码解析5-mutex相关类

    mutexlock和mutexlockguard class mutexlock:noncopyable { }: class mutexlockguard:noncopyable { }: 作用: ...

随机推荐

  1. PYTHON常见数据类型示例

    shoplist = ['apple', 'mango', 'carrot', 'banana'] print('I have ', len(shoplist), ' items to purchas ...

  2. 【HDU 1133】 Buy the Ticket (卡特兰数)

    Buy the Ticket Problem Description The "Harry Potter and the Goblet of Fire" will be on sh ...

  3. UVA 10048 Audiophobia 任意两点的路径上最大的边

    题目是要求任意给定两点的的路径上最大的边,最终输出这些最大边中最小的值,也就是求一条路径使得这条路径上最大的边在所有连通两点的路径中最短.根据Floyd—Warshall算法改造一下就行了.dp[i] ...

  4. EMMC 简要介绍

    一直想写一篇关于EMMC的文章,但是因为之前弄了很多PPT,所以一直提不起兴趣,索性直接把之前的一个介绍EMMC的PPT贴出来给大家看看,有什么问题可以直接跟帖,我会第一时间进行解答,谢谢

  5. 利用if else咱们结婚吧

    class Program    {        static void Main(string[] args)        {            while (true)           ...

  6. Ext.Net中的Task控件的使用

    在用到Ext.Net中的Task控件的时候,写了一下基本的使用方法: 控件是在TaskManager里面的Tasks下面的Task 此控件的常用属性有,TaskID.Interval(设置间隔时间). ...

  7. Bzoj 1042: [HAOI2008]硬币购物 容斥原理,动态规划,背包dp

    1042: [HAOI2008]硬币购物 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1747  Solved: 1015[Submit][Stat ...

  8. JAVA Web项目的编码过滤器

    首先写一个EncodeFilter的过滤类: package com.djtu.wy.common; import java.io.IOException;import javax.servlet.F ...

  9. /usr/bin/ld: cannot find *** 的处理

    /usr/bin/ld: cannot find *** 的处理

  10. “互联网+”引发IT人才招工荒-新华网安徽频道

    "互联网+"引发IT人才招工荒-新华网安徽频道 "互联网+"引发IT人才招工荒