<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- <script>
let rose = {
user: [],
addUser: function (fn) {
if (typeof (fn) !== 'function') {
throw '非法操作!'
}
this.user.push(fn);
},
removeUser: function (fn) {
if (typeof (fn) != 'function') {
throw '非法操作!'
}
for (let i = 0; i < this.user.length; i++) {
if (this.user[i] == fn) {
this.user.splice(i, 1);
}
}
},
eat: function () {
for (let i = 0; i < this.user.length; i++) {
this.user[i]()
}
}
}
let jack_eat = {
eat: function () {
console.log('jack去吃饭了!')
}
}
let tom_eat = {
eat: function () {
console.log('tom去吃饭了!')
}
}
rose.addUser(jack_eat.eat)
rose.addUser(tom_eat.eat)
rose.eat()
rose.removeUser(tom_eat.eat)
rose.eat()
</script> -->
<!-- <script>
let publiUser = {
user: [],
addUser: function (fn) {
if (typeof fn != 'function') {
throw '非法参数!'
}
this.user.push(fn)
},
removeUser: function (fn) {
for (let i = 0; i < this.user.length; i++) {
if (this.user[i] == fn) {
this.user.splice(i, 1)
}
}
},
eat: function () {
for (let j = 0; j < this.user.length; j++) {
this.user[j]()
}
}
}
let rose = {};
let wml = {};
function makePublisher(obj) {
for (let key in publiUser) {
if (publiUser.hasOwnProperty(key) && typeof (publiUser[key]) == 'function') {
obj[key] = publiUser[key]
}
}
obj.user = []
}
makePublisher(rose);
console.log(rose, '=========rose')
makePublisher(wml);
let jack = {
eat: function () {
console.log('jack,吃饭了')
}
}
let tom = {
eat: function () {
console.log('tom,吃饭了')
}
}
rose.addUser(jack.eat)
rose.addUser(tom.eat)
rose.eat()
wml.eat()
</script> -->
<!-- <script>
let rose = {
user: {
sleep: [],
run: []
},
adduser: function (fn, type) {
if (typeof (fn) != 'function') {
throw '非法操作!'
}
if (type in this.user) {
this.user[type].push(fn)
}
},
removeUser: function (fn, type) {
if (!this.user.hasOwnProperty(type)) {
for (let i = 0; this.user[type].length; i++) {
if (this.user[type][i] == fn) {
this.user[type].splice(i, 1)
break;
}
}
}
},
sleep: function () {
for (let j = 0; j < this.user['sleep'].length; j++) {
this.user['sleep'][j]()
}
},
run: function () {
for (let k = 0; k < this.user['run'].length; k++) {
this.user['run'][k]()
}
}
}
let jack = {
sleep: function () {
console.log('jack 睡觉了!')
},
run: function () {
console.log('jack 跑步了')
}
}
let tom = {
sleep: function () {
console.log('tom,睡觉!')
},
run: function () {
console.log('tom,跑步!')
}
}
rose.adduser(jack.sleep, 'sleep')
rose.adduser(jack.run, 'run')
rose.sleep()
rose.run()
console.log('=============')
rose.adduser(tom.sleep, 'sleep')
rose.adduser(tom.run, 'run')
rose.sleep()
rose.run()
</script> -->
<script>
let rose = {
user: {},
event: {},
adduser: function (fn, type) {
if (typeof (fn) != 'function') {
throw '非法操作!'
}
if (this.user.hasOwnProperty(type)) {
this.user[type].push(fn)
} else {
this.user[type] = [fn]
}
this.event = this.all()
},
removeUser: function (fn, type) {
if (!this.user.hasOwnProperty(type)) {
for (let i = 0; this.user[type].length; i++) {
if (this.user[type][i] == fn) {
this.user[type].splice(i, 1)
break;
}
}
}
this.event = this.all()
},
all: function () {
let obj = {}
let that = this;
for (let key in this.user) {
obj[key] = function () {
for (let j = 0; j < that.user[key].length; j++) {
that.user[key][j]()
}
}
}
return obj
}
}
let jack = {
sleep: function () {
console.log('jack 睡觉了!')
},
run: function () {
console.log('jack 跑步了')
},
pig: function () {
console.log('tom,pig!')
}
}
let tom = {
sleep: function () {
console.log('tom,睡觉!')
},
run: function () {
console.log('tom,跑步!')
},
pig: function () {
console.log('tom,pig!')
}
}
rose.adduser(jack.sleep, 'sleep')
rose.adduser(jack.run, 'run')
rose.adduser(jack.pig, 'pig')
rose.event.run()
rose.event.sleep()
rose.event.pig()
console.log('=============')
rose.adduser(tom.sleep, 'sleep')
rose.adduser(tom.run, 'run')
rose.event.sleep()
rose.event.run()
</script>
</script>
</body>
</html>
观察者模式实现(多个状态)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>11-观察者模式实现01(基础版本)</title>
<style>
* {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
步骤:
1.提供一个发布者
2.提供一个观察者
3.注册观察者(建立发布者和观察者之间的联系)
4.发布者发布信息
女神: Rose
男生 :
Jack
Tom
思考:2个发布者
提供一个发布者模板!
思考:多个状态??????
<script>
var publisher = {
user : {
eat : [],
sleep : []
},
addUser : function (fn,type) {
var type = type || 'eat';
if(typeof fn != 'function'){
throw '非法操作!';
}
this.user[type].push(fn);
},
removeUser : function (fn,type) {
var type = type || 'eat';
for (var i = 0; i < this.user[type].length; i++) {
if(this.user[type][i] == fn){
this.user[type].splice(i,1);
}
}
},
eat : function () {
for (var i = 0; i < this.user['eat'].length; i++) {
this.user['eat'][i]();
}
},
sleep : function () {
for (var i = 0; i < this.user['sleep'].length; i++) {
this.user['sleep'][i]();
}
}
}
var rose = {};
var wml = {};
function makePublisher(obj) {
for(var key in publisher){
if(publisher.hasOwnProperty(key) && (typeof publisher[key] == 'function')){
obj[key] = publisher[key];
}
}
obj.user = {
eat : [],
sleep : []
};
}
makePublisher(rose);
makePublisher(wml);
var jack = {
jack_eat : function () {
console.log('邀请女神吃大餐!--Jack');
},
jack_sleep : function () {
console.log('晚安!-rose!-Jack');
}
}
var tom = {
tom_eat : function () {
console.log('邀请女神吃麻辣烫!--tom');
},
tom_sleep : function () {
console.log('带你去看星星!-tom');
}
}
rose.addUser(jack.jack_eat,'eat');
rose.addUser(jack.jack_sleep,'sleep');
rose.addUser(tom.tom_sleep,'sleep');
rose.sleep();
rose.removeUser(tom.tom_sleep,'sleep');
console.log('---');
rose.sleep();
</script>
</body>
</html>
观察者模式实现(优化)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>11-观察者模式实现01(基础版本)</title>
<style>
* {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
步骤:
1.提供一个发布者
2.提供一个观察者
3.注册观察者(建立发布者和观察者之间的联系)
4.发布者发布信息
女神: Rose
男生 :
Jack
Tom
思考:2个发布者
提供一个发布者模板!
思考:多个状态??????
<script>
var publisher = {
addUser : function (fn,type) {
var type = type || 'eat';
if(typeof fn != 'function'){
throw '非法操作!';
}
if(this.user[type] == undefined){
this.user[type] = [];
}
this.user[type].push(fn);
},
removeUser : function (fn,type) {
this.publish(type,fn);
},
publish : function (type,fn) {
var type = type || 'eat';
for (var i = 0; i < this.user[type].length; i++) {
if(typeof fn == 'function'){
if(this.user[type][i] == fn){
this.user[type].splice(i,1);
}
} else {
this.user[type][i]();
}
}
}
}
var rose = {
eat : function () {
this.publish('eat');
},
sleep : function () {
this.publish('sleep');
},
run : function () {
this.publish('run');
}
};
var wml = {};
function makePublisher(obj) {
for(var key in publisher){
if(publisher.hasOwnProperty(key) && (typeof publisher[key] == 'function')){
obj[key] = publisher[key];
}
}
obj.user = {};
}
makePublisher(rose);
makePublisher(wml);
var jack = {
jack_eat : function () {
console.log('邀请女神吃大餐!--Jack');
},
jack_sleep : function () {
console.log('晚安!-rose!-Jack');
}
}
var tom = {
tom_eat : function () {
console.log('邀请女神吃麻辣烫!--tom');
},
tom_sleep : function () {
console.log('带你去看星星!-tom');
},
tom_run : function () {
console.log('带你去天河公园跑步!-tom');
}
}
rose.addUser(tom.tom_run,'run');
rose.run();
</script>
</body>
</html>
观察者模式实现(发布者成为观察者)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>11-观察者模式实现01(基础版本)</title>
<style>
* {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
步骤:
1.提供一个发布者
2.提供一个观察者
3.注册观察者(建立发布者和观察者之间的联系)
4.发布者发布信息
女神: Rose
男生 :
Jack
Tom
思考:2个发布者
提供一个发布者模板!
<script>
var publisher = {
addUser : function (fn,type) {
var type = type || 'eat';
if(typeof fn != 'function'){
throw '非法操作!';
}
if(this.user[type] == undefined){
this.user[type] = [];
}
this.user[type].push(fn);
},
removeUser : function (fn,type) {
this.publish(type,fn);
},
publish : function (type,fn) {
var type = type || 'eat';
for (var i = 0; i < this.user[type].length; i++) {
if(typeof fn == 'function'){
if(this.user[type][i] == fn){
this.user[type].splice(i,1);
}
} else {
this.user[type][i]();
}
}
}
}
var rose = {
eat : function () {
this.publish('eat');
},
sleep : function () {
this.publish('sleep');
},
run : function () {
this.publish('run');
},
rose_lol : function () {
console.log('是我重要还是游戏重要!-Rose');
}
};
function makePublisher(obj) {
for(var key in publisher){
if(publisher.hasOwnProperty(key) && (typeof publisher[key] == 'function')){
obj[key] = publisher[key];
}
}
obj.user = {};
}
makePublisher(rose);
var jack = {
jack_eat : function () {
console.log('邀请女神吃大餐!--Jack');
},
jack_sleep : function () {
console.log('晚安!-rose!-Jack');
},
lol : function () {
this.publish('lol');
}
}
var tom = {
tom_eat : function () {
console.log('邀请女神吃麻辣烫!--tom');
},
tom_sleep : function () {
console.log('带你去看星星!-tom');
},
tom_run : function () {
console.log('带你去天河公园跑步!-tom');
},
tom_lol : function () {
console.log('别管Rose了,来开黑把!-tom');
}
}
makePublisher(jack);
jack.addUser(rose.rose_lol,'lol');
jack.addUser(tom.tom_lol,'lol');
jack.lol();
</script>
</body>
</html>