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版)源码解析04:Notifier类

PureMVC

上篇文章,分析了Observer(观察者)类,现在来分析Notifier(通知者)类。关于Notifier类,源码注释上有这么一段:

* @class puremvc.Notifier
 * A Base Notifier implementation.
 * {@link puremvc.MacroCommand MacroCommand}, 
 * {@link puremvc.SimpleCommand SimpleCommand}, 
 * {@link puremvc.Mediator Mediator} and 
 * {@link puremvc.Proxy Proxy}
 * all have a need to send Notifications
 * The Notifier interface provides a common method called #sendNotification that 
 * relieves implementation code of the necessity to actually construct 
 * Notifications.

1
2
3
4
5
6
7
8
9
10
11

通过这段注释我们可以看出,Notifier类是MacroCommand类、SimpleCommand类、Mediator类和Proxy类的基类,它通过sendNotification()方法用来发送消息。

那先来看一下sendNotification()方法:

**
 * Create and send a Notification.
 *
 * Keeps us from having to construct new Notification instances in our 
 * implementation code.
 * 
 * @param {string} notificationName
 *  A notification name
 * @param {Object} [body]
 *  The body of the notification
 * @param {string} [type]
 *  The notification type
 * @return {void}
 */
Notifier.prototype.sendNotification = function(notificationName, body, type)
{
    var facade = this.getFacade();
    if(facade)
    {
        facade.sendNotification(notificationName, body, type);
    }
};

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

通过以上代码可以看出,Notifier对象发送消息实际上是调用Facade对象的sendNotification()方法来发送的,包括三个参数,这三个参数可以实例化一个Notification对象。那这个Facade对象是干嘛用的呢?暂时不管它,后面再单独分析这个类?(大家有兴趣的话,可以搜一下Facade设计模式)。可以先看一下getFacade()方法,这个方法是返回一个Facade对象。

/**
 * Retrieve the Multiton Facade instance
 * @protected
 * @return {puremvc.Facade}
 */
Notifier.prototype.getFacade = function()
{
    if(this.multitonKey == null)
    {
        throw new Error(Notifier.MULTITON_MSG);
    };

    return Facade.getInstance(this.multitonKey);
};

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

Notifier对象有一个multitonkey属性和一个facade属性:

  • multitonkey: 全局Facade的唯一键值,通过这个key值我们可以索引到一个全局Facade
  • facade 属性就是以后要讲解的Facade类的实例化对象
Notifier.prototype.multitonKey = null;
Notifier.prototype.facade;

1
2
3

Notifier类的构造函数:

function Notifier()
{
};

1
2
3
4

Notifier类的构造函数没有进行任何处理,但Notifier类有一个初始化方法initializeNotifier()方法:

Notifier.prototype.initializeNotifier = function(key)
{
    this.multitonKey = String(key);
    this.facade= this.getFacade();
};

1
2
3
4
5
6

这个方法主要用于为multitonkey属性和facade属性赋值。

最后,可以把Notifier类总结为:

  • 属性:

    • multitonkey
    • facade
  • 方法:

    • getFacade():返回一个facade对象
    • initilizeNotifier():可以理解为初始化Notifier对象,为multitonKey和facade属性赋值
    • sendNotification():发送消息,本质是调用facade对象的sendNotification方法。
#PureMVC
上次更新: 2022/12/01, 11:09:34
PureMVC(JS版)源码解析03:Observer类
PureMVC(JS版)源码解析05:SimpleCommand类

← PureMVC(JS版)源码解析03:Observer类 PureMVC(JS版)源码解析05:SimpleCommand类→

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