当前位置:网站首页>6、 Observer mode
6、 Observer mode
2022-06-10 10:09:00 【Qwe7】
6、 ... and 、 Observer mode
The observer mode is also called publish subscribe mode or message mode . It is a very famous and important design pattern , This model generally defines a subject and many individuals , The main body here can be imagined as a message center , There are all kinds of news in it , Many individuals can subscribe to different messages , When the future news center releases a message , Individuals who have subscribed to him will wait for notification
1、 An example of the observer pattern
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script>
// Message center
var msgCenter = (function () {
var _msg = {}; // Store messages
/*
var _msg = {
'carInfo': [person1.alreadyRegister.carInfo, person2.alreadyRegister.carInfo, person3.alreadyRegister.carInfo, , person5.alreadyRegister.carInfo]
'newInfo': [person1.alreadyRegister.newInfo, person2.alreadyRegister.newInfo, person3.alreadyRegister.newInfo, , person4.alreadyRegister.newInfo]
}
*/
return {
// For subscribing to a message
register: function(type, fn) { // fn Callback function
if (_msg[type]) {
_msg[type].push(fn)
} else {
_msg[type] = [fn]
}
},
// Used to post messages
fire: function(type, args) { // args The information attached to the announcement
if (!_msg[type]) {
return
}
var event = {
type: type,
args: args || {}
}
for(var i = 0; i < _msg[type].length; i++) {
_msg[type][i](event)
}
},
// Message for unsubscribing
cancel: function(type, fn) {
if (!_msg[type]) {
return
}
for(var i = 0; i < _msg[type].length; i++) {
if (_msg[type][i] === fn) {
_msg[type].splice(i, 1)
return
}
}
}
}
})()
function Person() {
this.alreadyRegister = {};
}
Person.prototype.register = function(type, fn) {
if (this.alreadyRegister[type]) {
console.log(' You have subscribed to this message , Please don't subscribe again ')
} else {
msgCenter.register(type, fn);
this.alreadyRegister[type] = fn
}
}
Person.prototype.cancel = function (type) {
msgCenter.cancel(type, this.alreadyRegister[type])
delete this.alreadyRegister[type]
}
var person1 = new Person()
var person2 = new Person()
var person3 = new Person()
var person4 = new Person()
var person5 = new Person()
person1.register('carInfo', function (e) {
console.log('person1 Got about ' + e.type + ' The news of , The message is :' + e.args.info);
})
person1.register('newInfo', function (e) {
console.log('person1 Got about ' + e.type + ' The news of , The message is :' + e.args.info);
})
person2.register('carInfo', function (e) {
console.log('person2 Got about ' + e.type + ' The news of , The message is :' + e.args.info);
})
person2.register('newInfo', function (e) {
console.log('person2 Got about ' + e.type + ' The news of , The message is :' + e.args.info);
})
person3.register('carInfo', function (e) {
console.log('person3 Got about ' + e.type + ' The news of , The message is :' + e.args.info);
})
person3.register('newInfo', function (e) {
console.log('person3 Got about ' + e.type + ' The news of , The message is :' + e.args.info);
})
person4.register('carInfo', function (e) {
console.log('person4 Got about ' + e.type + ' The news of , The message is :' + e.args.info);
})
person5.register('newInfo', function (e) {
console.log('person5 Got about ' + e.type + ' The news of , The message is :' + e.args.info);
})
msgCenter.fire('carInfo', {info: ' New car launch !'})
msgCenter.fire('newInfo', {info: ' So and so is here !'})
person1.register('carInfo', function(e) {
console.log('test')
})
console.log('------------------')
person1.cancel('carInfo')
msgCenter.fire('carInfo', { info: ' Send another one ' })
</script>
</head>
<body>
</body>
</html>边栏推荐
- 隐私计算重要技术突破!亿级数据密态分析可在10分钟内完成
- axure弹框设置
- R语言使用lmPerm包应用于线性模型的置换方法(置换检验、permutation tests)、使用lm模型构建多项式回归模型、使用lmp函数生成置换检验多项式回归模型
- 已经过去多长时间的方式显示时间
- SAP 云平台多目标应用 Multi-Target Application 的开发技术介绍
- Lambda表达式例一
- Why is your next computer a computer? Explore different remote operations
- Four simplest anti reverse circuits
- 威纶通触摸屏直接与台达变频器进行MODBUS RTU通信的具体方法(图文)
- Google Earth Engine(GEE)——GPWv411:平均行政单位面积数据集
猜你喜欢

Do you know all the wonderful functions of the vlookup function?

fastadmin使用PHPExcel导出表格数据到Excel中

Personnalisation de l'équipement d'essai de Chengdu Introduction préliminaire aux types de données du langage C du micro - ordinateur à puce unique

Microsoft exposes another "scandal": watching VR porn in the office, "the father of hololens" is about to leave!

【边缘检测】基于matlab八方向sobel图像边缘检测【含Matlab源码 1865期】

Shutter: custom radio button

Why is your next computer a computer? Explore different remote operations

On how T-level interactive development shines on our hands

axure弹框设置

一行代码制作数据分析交叉表,太便捷了
随机推荐
fastadmin使用PHPExcel导出表格数据到Excel中
HMM详细介绍+举例
解决Typescript文件被识别为视频的问题
5G 联通网管设计思路
How to handle art record? What materials should be prepared for handling the art record?
【GoLang】通过BMI指数学习控制台输入与条件控制
Browser cache forbidden explanation
七、策略模式
5g Unicom network management design idea
一行代码制作数据分析交叉表,太便捷了
工业互联网架构图
Uncaught TypeError: Cannot read properties of undefined (reading ‘colspan‘)
Lambda表达式例一
装饰器模式
Shutter: custom radio button
R language uses rpart package to build decision tree model, selects appropriate tree size (complexity) to check cptable contents of decision tree objects (tree size is defined by splitting times and p
【图像特征提取】基于matlab脉冲耦合神经网络(PCNN)图像特征提取【含Matlab源码 1868期】
威纶通触摸屏直接与台达变频器进行MODBUS RTU通信的具体方法(图文)
axure弹框设置
On the night of the joint commissioning, I beat up my colleagues