Gitlib Gitlib
首页
  • 分类
  • 标签
  • 归档
  • Golang开发实践万字总结
  • MySQL核心知识汇总
  • Redis实践总结
  • MQ实践万字总结
  • Docker数据持久化总结
  • Docker网络模式深度解读
  • 常用游戏反外挂技术总结
  • 读书笔记
  • 心情杂货
  • 行业杂谈
  • 友情链接
关于我
GitHub (opens new window)

Ravior

以梦为马,莫负韶华
首页
  • 分类
  • 标签
  • 归档
  • Golang开发实践万字总结
  • MySQL核心知识汇总
  • Redis实践总结
  • MQ实践万字总结
  • Docker数据持久化总结
  • Docker网络模式深度解读
  • 常用游戏反外挂技术总结
  • 读书笔记
  • 心情杂货
  • 行业杂谈
  • 友情链接
关于我
GitHub (opens new window)
  • PHP

  • Golang

  • Python

  • Javascript

    • PureMVC

      • PureMVC(JS版)源码解析:认识PureMVC
      • PureMVC(JS版)源码解析01:观察者模式解析
      • PureMVC(JS版)源码解析02:Notification类
      • PureMVC(JS版)源码解析03:Observer类
      • PureMVC(JS版)源码解析04:Notifier类
      • PureMVC(JS版)源码解析05:SimpleCommand类
      • PureMVC(JS版)源码解析06:MacroCommand类
      • PureMVC(JS版)源码解析07:Mediator类
      • PureMVC(JS版)源码解析08:Proxy类
      • PureMVC(JS版)源码解析09:View类
      • PureMVC(JS版)源码解析10:Controller类
      • PureMVC(JS版)源码解析11:Model类
      • PureMVC(JS版)源码解析12:Facade类
      • PureMVC(JS版)源码解析:总结
    • 从一个基础Javascript面试题谈起
    • Javascript垃圾回收机制
    • Javascript实现双向数据绑定
    • 浏览器渲染网页过程
    • 浏览器资源缓存机制总结
  • 其他语言

  • 编程语言
  • Javascript
  • PureMVC
Ravior
2013-10-07

PureMVC(JS版)源码解析06:MacroCommand类

PureMVC

上篇文章,分析了SimpleCommand类,接下来分析与SimpleCommand类很相似的MacroCommand类。

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

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

1
2
3
4

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);
    }
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

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);
};

1
2
3
4
5
6
7
8
9
10
11
12

再看看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();
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

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

我们看看initializeMacroCommand()方法:

MacroCommand.prototype.initializeMacroCommand= function() {}

1
2

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

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

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

关于,SimpleCommand类和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.
 * 
1
2
3
4
5

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

#PureMVC
上次更新: 2022/12/01, 11:09:34
PureMVC(JS版)源码解析05:SimpleCommand类
PureMVC(JS版)源码解析07:Mediator类

← PureMVC(JS版)源码解析05:SimpleCommand类 PureMVC(JS版)源码解析07:Mediator类→

最近更新
01
常用游戏反外挂技术总结
11-27
02
Golang开发实践万字总结
11-11
03
Redis万字总结
10-30
更多文章>
Theme by Vdoing | Copyright © 2011-2022 Ravior | 粤ICP备17060229号-3 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式