JS模式之简单的订阅者和发布者模式完整实例


Posted in Javascript onJune 30, 2015

本文实例讲述了JS模式之简单的订阅者和发布者模式。分享给大家供大家参考。具体如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>parten</title>
</head>
<body>
<script>
var singletonTest = SingletonTest.getInstance({
  pointX : 5
});
console.log(singletonTest.pointX);
//easy_Observer_model;
function ObserverList(){
  this.observerList = [];
};
ObserverList.prototype.Add = function(obj){
  return this.observerList.push(obj);
};
ObserverList.prototype.Empty = function(){
  this.observerList = [];
};
ObserverList.prototype.Count = function(){
  return this.observerList.length;
};
ObserverList.prototype.Get = function(index){
  if(index>-1 && index<this.observerList.length)
  return this.observerList[index];
};
ObserverList.prototype.Insert = function(obj,index){
  var pointer = -1;
  if(index == 0){
    this.observerList.unshift(obj);
    pointer = index;
  }else if(index == this.observerList.length){
    this.observerList.push(obj);
    pointer = index;
  };
  return pointer;
};
ObserverList.prototype.IndexOf = function(obj,startIndex){
  var i = startIndex, pointer = -1;
  while(i < this.observerList.length){
    if(this.observerList[i] === obj){
      pointer = i;
    };
    i++
  };
  return pointer;
};
ObserverList.prototype.RemoveIndexAt = function(index){
  if(index === 0){
    this.observerList.shift();
  }else if(index === this.observerList.length-1){
    this.observerList.pop();
  };
  return index;
};
function extend(obj,extension){
  for(var key in obj){
    extension[key] = obj[key];
  }
};
//
function Subject(){
  this.observers = new ObserverList();
};
Subject.prototype.AddObserver = function(obj){
  this.observers.add(obj)
};
Subject.prototype.RemoveObserver = function(observer){
  this.observers.removeIndexAt( this.observers.IndexOf(observer,0) );
};
Subject.prototype.Notify = function(context){
  var observerCount = this.observers.count();
  for(var i=0; i<observerCount; i++){
    this.observers.Get(i).update(context);
  };
}
//Pubsub//subscribe
var Pubsub = {};
(function(q){
  var topics = [],
    subUid = -1;
  q.publish = function(topic,args){
    if(!topics[topic]){
      return false;
    };
    var subscribers = topics[topic],
      len = subscribers ? subscribers.length : 0;
    while(len--){
      subscribers[len].func(topic,args);
    }
    return this;
  };
  q.subscribe = function(topic,func){
    if(!topics[topic]){
      topics[topic] = [];
    };
    var token = (++subUid).toString();
    topics[topic].push({
      token : token,
      func : func
    });
    return token;
  };
  q.unsubscribe = function(token){
    for(var m in topics){
      if(topics[m]){
        for(var i=0; i<topics[m].length; i++){
          if(topics[m][i].token === token){
            topics[m].splice(i,1);
            return token;
          }
        }
      };
    };
    return this;
  }
})(pubsub);
</script>
</body>
</html>

希望本文所述对大家的javascript程序设计有所帮助。

Javascript 相关文章推荐
Prototype中dom对象方法汇总
Sep 17 Javascript
jquery中添加属性和删除属性
Jun 03 Javascript
js代码验证手机号码和电话号码是否合法
Jul 30 Javascript
浅析BootStrap中Modal(模态框)使用心得
Dec 24 Javascript
canvas绘制的直线动画
Jan 23 Javascript
Angular.Js之Scope作用域的学习教程
Apr 27 Javascript
Vue关于数据绑定出错解决办法
May 15 Javascript
bootstrap中日历范围选择插件daterangepicker的使用详解
Apr 17 Javascript
详解处理bootstrap4不支持远程静态框问题
Jul 20 Javascript
解决layui前端框架 form表单,table表等内置控件不显示的问题
Aug 19 Javascript
基于mpvue小程序使用echarts画折线图的方法示例
Apr 24 Javascript
Vue3.0数据响应式原理详解
Oct 09 Javascript
JS模式之单例模式基本用法
Jun 30 #Javascript
js简单工厂模式用法实例
Jun 30 #Javascript
JavaScript判断undefined类型的正确方法
Jun 30 #Javascript
超赞的动手创建JavaScript框架的详细教程
Jun 30 #Javascript
JavaScript中Null与Undefined的区别解析
Jun 30 #Javascript
jQuery结合AJAX之在页面滚动时从服务器加载数据
Jun 30 #Javascript
深入探究使JavaScript动画流畅的一些方法
Jun 30 #Javascript
You might like
PHP用strstr()函数阻止垃圾评论(通过判断a标记)
2013/09/28 PHP
PHP goto语句简介和使用实例
2014/03/11 PHP
PHP生成(支持多模板)二维码海报代码
2018/04/30 PHP
php+layui数据表格实现数据分页渲染代码
2019/10/26 PHP
javascript工具库代码
2012/03/29 Javascript
javascript向flash swf文件传递参数值注意细节
2012/12/11 Javascript
javascript中的void运算符语法及使用介绍
2013/03/10 Javascript
JS过滤url参数特殊字符的实现方法
2013/12/24 Javascript
轻松创建nodejs服务器(2):nodejs服务器的构成分析
2014/12/18 NodeJs
浅谈JavaScript Date日期和时间对象
2014/12/29 Javascript
js实现鼠标触发图片抖动效果的方法
2015/02/27 Javascript
JavaScript校验Number(4,1)格式的数字实例代码
2017/03/13 Javascript
vue.js中过滤器的使用教程
2017/06/08 Javascript
vue-cli webpack2项目打包优化分享
2018/02/07 Javascript
koa router 多文件引入的方法示例
2019/05/22 Javascript
js中switch语句的学习笔记
2020/03/25 Javascript
Vue利用localStorage本地缓存使页面刷新验证码不清零功能的实现
2020/09/04 Javascript
Vue页面渲染中key的应用实例教程
2021/01/12 Vue.js
[01:10:02]IG vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
使用Python构建Hopfield网络的教程
2015/04/14 Python
Python中优化NumPy包使用性能的教程
2015/04/23 Python
Python实现统计单词出现的个数
2015/05/28 Python
深入理解Python中命名空间的查找规则LEGB
2015/08/06 Python
python 基础教程之Map使用方法
2017/01/17 Python
Python使用uuid库生成唯一标识ID
2020/02/12 Python
Tensorflow中批量读取数据的案列分析及TFRecord文件的打包与读取
2020/06/30 Python
Python中全局变量和局部变量的理解与区别
2021/02/07 Python
周仰杰(JIMMY CHOO)英国官方网站:闻名世界的鞋子品牌
2018/10/28 全球购物
家佳咖啡店创业计划书
2013/12/27 职场文书
社区活动总结报告
2014/05/05 职场文书
庆祝国庆节演讲稿2014
2014/09/19 职场文书
学院党委班子四风问题自查报告及整改措施
2014/10/25 职场文书
办公用品管理制度
2015/08/04 职场文书
2016年学校党支部创先争优活动总结
2016/04/05 职场文书
我收到了德劲DE1107
2022/04/05 无线电
数据设计之权限的实现
2022/08/05 MySQL