<!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>

jS-模式之简单的订阅者和发布者模式的更多相关文章

  1. ROS学习笔记10-写一个简单的订阅者和发布者(C++版本)

    本文档来源于:http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29 写发布者节点如前所述,节点是连接到RO ...

  2. 实现一个简单的订阅与发布模式的代码块,和redux

    /** * Created by Mrzou on 2018/3/11. */ //实现简单的订阅与发布模式的代码块export function pattern() { let currentLis ...

  3. js之观察者模式和发布订阅模式区别

    观察者模式(Observer) 观察者模式指的是一个对象(Subject)维持一系列依赖于它的对象(Observer),当有关状态发生变更时 Subject 对象则通知一系列 Observer 对象进 ...

  4. vue—你必须知道的 js数据类型 前端学习 CSS 居中 事件委托和this 让js调试更简单—console AMD && CMD 模式识别课程笔记(一) web攻击 web安全之XSS JSONP && CORS css 定位 react小结

    vue—你必须知道的   目录 更多总结 猛戳这里 属性与方法 语法 计算属性 特殊属性 vue 样式绑定 vue事件处理器 表单控件绑定 父子组件通信 过渡效果 vue经验总结 javascript ...

  5. vue.js+webpack在一个简单实例中的使用过程demo

    这里主要记录vue.js+webpack在一个简单实例中的使用过程 说明:本次搭建基于Win 7平台 Node.js 安装官网提供了支持多种平台的的LTS版本下载,我们根据需要来进行下载安装.对于Wi ...

  6. C#使用RabbitMq队列(Sample,Work,Fanout,Direct等模式的简单使用)

    1:RabbitMQ是个啥?(专业术语参考自网络) RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件). RabbitMQ服务器是用Erlang语言编写的, ...

  7. Reactor 模式的简单实现

    Reactor 模式简单实现 在网上有部分文章在描述Netty时,会提到Reactor.这个Reactor到底是什么呢?为了搞清楚Reactor到底是什么鬼,我写了一个简单的Demo,来帮助大家理解他 ...

  8. 【配置】检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为【经典】模式)。

      ×   检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为[经典]模式). 我们将ASP.NET程序从IIS6移植到IIS7,可能运行提示以下错误: HTTP 错误 5 ...

  9. Objective-C 工厂模式(上) -- 简单工厂模式

    简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式,但不属于23种GOF设计模式之一.简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例.简单工厂 ...

随机推荐

  1. NOI2016 山西省省选 第二题序列

    给出一个n(n<=10^18)然后把n拆成若干个数之和(3=1+2=2+1 是两种情况) 然后把这写数字当作斐波那契数列的下标相乘再相加 例如: 3=1+1+1=1+2=2+1=3 所以结果就是 ...

  2. 离散化+线段树 POJ 3277 City Horizon

    POJ 3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18466 Accepted: 507 ...

  3. 边工作边刷题:70天一遍leetcode: day 88-5

    coins in a line I/II/III: check above 1. recursion的返回和dp[left][right]表示什么?假设game是[left,right],那么play ...

  4. Codeforces Round #370 (Div. 2)C. Memory and De-Evolution 贪心

    地址:http://codeforces.com/problemset/problem/712/C 题目: C. Memory and De-Evolution time limit per test ...

  5. 移动web页面使用的字体的思考

    前言 记得做PC端页面的时候,字体一般设置为微软雅黑,现在做起移动端页面来了,设计师们一般都还把字体设置为微软雅黑字体,但是做出来后,测试的时候发现页面中的字体不是微软雅黑,怎么办? 后来了解到的手机 ...

  6. HOJ 1640 Mobile Phone

    题意:有一个n*n的矩阵,op==1时,在(x,y)增加值z,op==2时,求以(x1,y1)和(x2,y2)构成的矩阵的和. 思路:二维线段树. 代码: #include<stdio.h> ...

  7. 用js判断时间的先后顺序

    我们在用户注册信息的时候,有时根据需要往往要加入一些时间上的判断,今天我在这里给大家推荐一款比较实用的时间先后顺序判断的代码,希望对大家有所有帮助. <!DOCTYPE HTML> < ...

  8. unix文件操作函数

    1. fopen函数 #include <stdio.h> FILE *fopen(const char *path, const char *mode) 返回:文件顺利打开后,指向该流的 ...

  9. 会动的大风车(css3)

    今天用css3的写了一个会动的大风车,使用translate和rotate布局,使用animation制作动画效果:分享给大家 <!DOCTYPE html> <html lang= ...

  10. AE二次开发技巧之撤销、重做

    原文地址:http://www.cnblogs.com/wylaok/articles/2363208.html 可以把AE自带的重做.撤销按钮或工具添加到axToolBarControl上,再把ax ...