zoukankan      html  css  js  c++  java
  • PureMVC(JS版)源码解析(四):Notifier类

         上一篇博客中,我们解析了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制作的思维导图:
  • 相关阅读:
    MySQL + Atlas --- 部署读写分离(实现,其他的技术有参考文档)
    基于Redis、Storm的实时数据查询实践
    Redis 配置
    Redis 安装
    redis简介
    Java-Enumeration总结
    spring源码剖析(四)自定义标签解析流程
    svn 把主干合到分支 分支合到主干
    关于Class.getResource和ClassLoader.getResource的路径问题
    instanceof, isinstance,isAssignableFrom的区别
  • 原文地址:https://www.cnblogs.com/iRavior/p/3355074.html
Copyright © 2011-2022 走看看