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 相关文章推荐
Javascript 验证上传图片大小[客户端]
Aug 01 Javascript
JavaScript为对象原型prototype添加属性的两种方式
Aug 01 Javascript
js制作的鼠标悬浮时产生的下拉框效果
Oct 27 Javascript
在JavaScript的AngularJS库中进行单元测试的方法
Jun 23 Javascript
javascript实现简单的分页特效
Aug 12 Javascript
JS实现合并两个数组并去除重复项只留一个的方法
Dec 17 Javascript
javascript实现图片左右滚动效果【可自动滚动,有左右按钮】
Sep 19 Javascript
用JavaScript做简易的购物车的代码示例
Oct 20 Javascript
vue 标签属性数据绑定和拼接的实现方法
May 17 Javascript
JS异步执行结果获取的3种解决方式
Feb 19 Javascript
JS中的算法与数据结构之栈(Stack)实例详解
Aug 20 Javascript
p5.js绘制创意自画像
Nov 04 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设计模式  Command(命令模式)
2011/06/17 PHP
PHP使用mkdir创建多级目录的方法
2015/12/22 PHP
PHP获取用户客户端真实IP的解决方案
2016/10/10 PHP
一个JS翻页效果
2007/07/23 Javascript
JS window.opener返回父页面的应用
2009/10/24 Javascript
Js切换功能的简单方法
2010/11/23 Javascript
了解Javascript的模块化开发
2015/03/02 Javascript
浅谈javascript获取元素transform参数
2015/07/24 Javascript
浅谈jQuery 中的事件冒泡和阻止默认行为
2016/05/28 Javascript
用Angular实时获取本地Localstorage数据,实现一个模拟后台数据登入的效果
2016/11/09 Javascript
Javascript 链式作用域详细介绍
2017/02/23 Javascript
关于jQuery EasyUI 中刷新Tab选项卡后一个页面变形的解决方法
2017/03/02 Javascript
jQuery实现碰到边缘反弹的动画效果
2018/02/24 jQuery
JavaScript从原型到原型链深入理解
2019/06/03 Javascript
微信公众号获取用户地理位置并列出附近的门店的示例代码
2019/07/25 Javascript
Vue+Bootstrap收藏(点赞)功能逻辑与具体实现
2020/10/22 Javascript
[53:52]OG vs EG 2018国际邀请赛淘汰赛BO3 第二场 8.23
2018/08/24 DOTA
Python中获取网页状态码的两个方法
2014/11/03 Python
讲解Python的Scrapy爬虫框架使用代理进行采集的方法
2016/02/18 Python
python中星号变量的几种特殊用法
2016/09/07 Python
Python编程实现的图片识别功能示例
2017/08/03 Python
使用Python的turtle模块画图的方法
2017/11/15 Python
python数字图像处理之高级滤波代码详解
2017/11/23 Python
python版学生管理系统
2018/01/10 Python
python实现读取大文件并逐行写入另外一个文件
2018/04/19 Python
详解Python基础random模块随机数的生成
2019/03/23 Python
pandas 对日期类型数据的处理方法详解
2019/08/08 Python
python中str内置函数用法总结
2020/12/27 Python
德国滑雪和户外用品网上商店:XSPO
2019/10/30 全球购物
UNOde50美国官网:西班牙珠宝品牌
2020/08/15 全球购物
出纳岗位职责
2013/11/09 职场文书
经贸日语专业个人求职信
2013/12/13 职场文书
就业表自我评价分享
2014/02/06 职场文书
学校安全生产月活动总结
2014/07/05 职场文书
党员评议个人总结
2014/10/20 职场文书
七个Python必备的GUI库
2021/04/27 Python