上一篇博客中,我们解析了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.

通过这段注释我们可以看出,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);
}
};

通过以上代码我们可以看出,Notifier对象发送消息实际上是调用facade对象的sendNotification()方法来发送的,包括三个参数,这三个参数可以实例化一个Notification对象。那这个facade对象是干嘛用的呢?暂时不去讲解,会在以后的博客中针对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);
};

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

我们再回过头来看看Notifier类的构造函数:

function Notifier()
{
};

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

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

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

最后,我们可以把Notifier类总结为:
属性:①multitonkey②facade
方法:
①getFacade():返回一个facade对象
②initilizeNotifier():可以理解为初始化Notifier对象,为multitonKey和facade属性赋值
③sendNotification():发送消息,本质是调用facade对象的sendNotification方法。 
 
同样,附上之前学习PureMVC制作的思维导图:

PureMVC(JS版)源码解析(四):Notifier类的更多相关文章

  1. Mybatis源码解析(四) —— SqlSession是如何实现数据库操作的?

    Mybatis源码解析(四) -- SqlSession是如何实现数据库操作的?   如果拿一次数据库请求操作做比喻,那么前面3篇文章就是在做请求准备,真正执行操作的是本篇文章要讲述的内容.正如标题一 ...

  2. Sentinel源码解析四(流控策略和流控效果)

    引言 在分析Sentinel的上一篇文章中,我们知道了它是基于滑动窗口做的流量统计,那么在当我们能够根据流量统计算法拿到流量的实时数据后,下一步要做的事情自然就是基于这些数据做流控.在介绍Sentin ...

  3. Dubbo 源码解析四 —— 负载均衡LoadBalance

    欢迎来我的 Star Followers 后期后继续更新Dubbo别的文章 Dubbo 源码分析系列之一环境搭建 Dubbo 入门之二 --- 项目结构解析 Dubbo 源码分析系列之三 -- 架构原 ...

  4. iOS即时通讯之CocoaAsyncSocket源码解析四

    原文 前言: 本文为CocoaAsyncSocket源码系列中第二篇:Read篇,将重点涉及该框架是如何利用缓冲区对数据进行读取.以及各种情况下的数据包处理,其中还包括普通的.和基于TLS的不同读取操 ...

  5. React的React.createContext()源码解析(四)

    一.产生context原因 从父组件直接传值到孙子组件,而不必一层一层的通过props进行传值,相比较以前的那种传值更加的方便.简介. 二.context的两种实现方式 1.老版本(React16.x ...

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

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

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

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

  8. vuex 源码解析(四) mutation 详解

    mutation是更改Vuex的store中的状态的唯一方法,mutation类似于事件注册,每个mutation都可以带两个参数,如下: state ;当前命名空间对应的state payload ...

  9. AFNetworking2.0源码解析<四>

    结构 AFURLResponseSerialization负责解析网络返回数据,检查数据是否合法,把NSData数据转成相应的对象,内置的转换器有json,xml,plist,image,用户可以很方 ...

  10. Celery 源码解析四: 定时任务的实现

    在系列中的第二篇我们已经看过了 Celery 中的执行引擎是如何执行任务的,并且在第三篇中也介绍了任务的对象,但是,目前我们看到的都是被动的任务执行,也就是说目前执行的任务都是第三方调用发送过来的.可 ...

随机推荐

  1. 你晓得吗?大多数企业根本没有做到 DevOps!

    作为当代 IT 企业提升效率的葵花宝典,DevOps 对 IT 企业效率的提升有目共睹 ,一时之间各大企业纷纷用提升效率的 DevOps 开发.协作.管理工具武装自己. 对比 2014 年上半年,CS ...

  2. CAS单点登录配置[5]:测试与总结

    终于要结束了... 测试 1 我们同时打开Tomcat6和Tomcat7,如果报错请修改. 打 开浏览器,输入http://fighting.com/Client1,进入CAS登录界面,这里我们先输入 ...

  3. windows笔记-一个简单的windows GUI应用程序

    #include<windows.h> // 编写Windows程序必须包含的头文件 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); ...

  4. C++开发与Windows API

    Windows API 向 C++ 开发人员提出了一项挑战. 组成 API 的众多库大都表现为 C 语言风格的函数和句柄或是 COM 风格的接口. 这些用起来都不太方便,需要进行一定的封装或间接操作. ...

  5. 【HDOJ】2546 饭卡

    01背包,需要先对数据升序排序.这样保证优先购买最贵的东西,才满足背包条件. #include <stdio.h> #include <string.h> #include & ...

  6. struts2错误:The Struts dispatcher cannot be found.

    struts2错误:The Struts dispatcher cannot be found. The Struts dispatcher cannot be found. This is usua ...

  7. Legal or Not

    Legal or Not Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  8. 《C语言程序设计现代方法》第2章 C语言基本概念

    C语言的基本概念 第一个C程序例子. /* pun.c */ #include <stdio.h> int main(void) { printf("To C, or not t ...

  9. CodeFirst-数据迁移-Migration

    http://www.cnblogs.com/haogj/archive/2012/02/17/2356537.html 1.安装最新NuGet 2.安装EntityFramework:在程序包管理器 ...

  10. C语言调用汇编实现字符串对换

    1. 前面配置arm交叉编译环境. 2. 配置好qemu-arm C语言代码string-switch.c: #include <stdio.h> #include <stdlib. ...