PureMVC(JS版)源码解析(六):MacroCommand类
上一篇博客,我们讲解了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类的更多相关文章
- Celery 源码解析六:Events 的实现
在 Celery 中,除了远程控制之外,还有一个元素可以让我们对分布式中的任务的状态有所掌控,而且从实际意义上来说,这个元素对 Celery 更为重要,这就是在本文中将要说到的 Event. 在 Ce ...
- Mybatis源码解析3——核心类SqlSessionFactory,看完我悟了
这是昨晚的武汉,晚上九点钟拍的,疫情又一次来袭,曾经熙熙攘攘的夜市也变得冷冷清清,但比前几周要好很多了.希望大家都能保护好自己,保护好身边的人,生活不可能像你想象的那么好,但也不会像你想象的那么糟. ...
- AOP源码解析:AspectJAwareAdvisorAutoProxyCreator类的介绍
AspectJAwareAdvisorAutoProxyCreator 的类图 上图中一些 类/接口 的介绍: AspectJAwareAdvisorAutoProxyCreator : 公开了Asp ...
- java源码解析之Object类
一.Object类概述 Object类是java中类层次的根,是所有类的基类.在编译时会自动导入.Object中的方法如下: 二.方法详解 Object的方法可以分成两类,一类是被关键字fin ...
- Netty源码解析 -- 内存对齐类SizeClasses
在学习Netty内存池之前,我们先了解一下Netty的内存对齐类SizeClasses,它为Netty内存池中的内存块提供大小对齐,索引计算等服务方法. 源码分析基于Netty 4.1.52 Nett ...
- AOP源码解析:AspectJExpressionPointcutAdvisor类
先看看 AspectJExpressionPointcutAdvisor 的类图 再了解一下切点(Pointcut)表达式,它指定触发advice的方法,可以精确到返回参数,参数类型,方法名 1 pa ...
- Bulma 源码解析之 .columns 类
{说明} 这一部分的源码内容被我简化了,另外我还额外添加了一个辅助类 is-grow. .columns // 修饰类 &.is-centered justify-content: cente ...
- java源码解析之String类(二)
上一节主要介绍了String类的一些构造方法,主要分为四类 无参构造器:String(),创建一个空字符串"",区别于null字符串,""已经初始化,null并 ...
- java源码解析之String类(一)
String是我们接触最多的类,无论是学习中还是工作中,基本每天都会和字符串打交道,从字符串本身的各种拼接.切片.变形,再到和其他基本数据类型的转换,几乎无时无刻都在使用它,今天就让我们揭开Strin ...
- muduo源码解析5-mutex相关类
mutexlock和mutexlockguard class mutexlock:noncopyable { }: class mutexlockguard:noncopyable { }: 作用: ...
随机推荐
- webservice 的权限验证
1. http://blog.csdn.net/jaune161/article/details/25602655 2. http://wcp88888888.iteye.com/blog/13993 ...
- USB数据线上的“疙瘩”:原来有这么大用处!
在不少键盘.鼠标或是游戏外设的数据线末端我们都能见到一小段金属圆环.虽然这算得上是习以为常的一个设计,但如果说到其具体作用的话很多人一下子还真回答不上来.反正笔者在这里先可以告诉大家,这货肯定不是简简 ...
- 【Xamarin挖墙脚系列:时刻下载最新的Mac环境下的Xamarin安装包】
原文:[Xamarin挖墙脚系列:时刻下载最新的Mac环境下的Xamarin安装包] 打开这两个地址,就能看到最新的安装包了.... http://www.jianshu.com/p/c67c14b3 ...
- 《ruby编程语言》笔记2 对象
ruby是一门非常纯粹的面向对象的语言:所有值都是对象,而且没有基本类型(primitive type)和对象类型的区别,这一点不同于其他语言.在Ruby中,所有对象都继承一个Object类,而且共享 ...
- Tomcat J2ee 发布步骤
1.找到要发布的工程,并发布到本地tomcat下,测试完全没有问题,找到tomcat下webapps下 并找到该工程,进入该工程目录,全选添加到 drivingSchool.zip 或 drivi ...
- Android textAppearance的属性设置及TextView属性详解
textAppearance的属性设置 android:textAppearance="?android:attr/textAppearanceSmall" android:tex ...
- 在服务 ObtainData 实现的协定列表中找不到协定名称 "IMetadataExchange"。将 ServiceMetadataBehavior 添加到配置文件或直接添加到 ServiceHost,以启用对该协定的支持。
第一种解决方法:最暴力的 配置去掉<endpoint address="mex" binding="mexHttpBinding" contract=&q ...
- Android调试时, "adb devices"命令提示 adb server is out of date. killing...
C:\Users\xxxx>adb devicesadb server is out of date. killing... 查看端口, 发现被占用 C:\Users\xxxx>adb n ...
- cocos2d-x 遇到的错误与解决方法。make: *** No rule to make target `/cygdrive/d/android/cocos2d-x-master/T12/proj.android/../../cocos2dx/xxxxx.cpp'
cocos2d-x 遇到的错误与解决方法.make: *** No rule to make target `/cygdrive/d/android/cocos2d-x-master/T12/proj ...
- RIDE常用快捷键
重命名: F2 搜索关键字: F5 执行用例: F8 创建新工程: Ctrl+N 创建新测试套: Ctrl+Shift+F 创建新用例: Ctrl+Shift+T 创建新关键字: Ctrl+Shift ...