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 { }: 作用: ...
随机推荐
- Java多线程初学者指南(4):线程的生命周期
与人有生老病死一样,线程也同样要经历开始(等待).运行.挂起和停止四种不同的状态.这四种状态都可以通过Thread类中的方法进行控制.下面给出了Thread类中和这四种状态相关的方法. // 开始线程 ...
- poj 1273
网络流之最大流的基础题: 可以使用dinic算法和EK算法: 分别对着模板敲了一遍: dinic: #include<cstdio> #include<cstring> #in ...
- 【HDU 1542】Atlantis 矩形面积并(线段树,扫描法)
[题目] Atlantis Problem Description There are several ancient Greek texts that contain descriptions of ...
- 【BZOJ 1597】 [Usaco2008 Mar]土地购买 (斜率优化)
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3601 Solved: 1322 Descrip ...
- 【UVA1331】关于最优三角剖分
最近在练习DP专题,学会了很多表示方法和转换方法,今天做最优三角剖分的时候发现脑子卡了,不会表示状态,于是写个博客记录一下. 最优三角剖分的一类题目都是差不多的.给你一个多边形,让你把它分割成若干个三 ...
- C51 的编程规范
编程首要是要考虑程序的可行性,然后是可读性.可移植性.健壮性以及可测试性.这是总则.但是很多人忽略了可读性.可移植性和健壮性(可调试的方法可能歌不相同),这是不对的. 1.当项目比较大时,最好分模块编 ...
- Linux下压缩某个文件夹(文件夹打包)
tar -zcvf /home/xahot.tar.gz /xahottar -zcvf 打包后生成的文件名全路径 要打包的目录例子:把/xahot文件夹打包后生成一个/home/xahot.tar. ...
- Leetcode 240. Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- VM Depot 新功能:直接通过 Windows Azure 管理门户部署虚拟机
发布于 2014-05-09 作者 陈 忠岳 想要尝试 VM Depot 上数以百计的各类开源虚拟机,却因为复杂的命令行操作而感到烦恼?微软开放技术想您所想,及时推出 VM Depot 最新功能 ...
- Linux 单网卡多 IP 的配置方法
Linux 单网卡多 IP 的配置方法 1 .永久配置的方法: 知道在 Linux 下网卡被称为 eth0,eth1,eth2..... ,所有网卡的配置文件都存储在 /etc/sysconfi ...