设计模式
- 单例模式1
创建型的设计模式,主要考虑的是创建对象的方式。当我们需要创建一种类型或一个类的唯一对象时,就可以使用该模式。
最基础的单例模式实现是使用对象文本标识法,例如:
var single = {};
- 单例模式2
可以用类似类的语法来实现单例模式。使对构造器多次调用new,但实际上创建的对象实例始终只有一个,且后续的构造器调用过程中所返回的始终是这个对象。
a. 全局变量
function Logger() {
if (typeof global_log === 'undefined') {
global_log = this; //全局变量没有global_log,则赋值
}
return global_log;
}
缺点是:可能会覆写全局变量导致实例丢失,也有可能覆写其他变量
b. 构造器属性
将唯一的实例保存到构造器本身的属性。
function Logger2() {
if (!Logger2.single_instance) {
Logger2.single_instance = this;
}
return Logger2.single_instance;
}
缺点是:构造器的属性是公有的,仍然有可能会被覆写,唯一实例可能会被修改或丢失。
c . 私有属性
function Logger3() {
var single_instance;
return (function () {
if (!single_instance) {
single_instance = this;
}
return single_instance;
})();
}
- 工厂模式
- 装饰器模式
var tree = {};
tree.decroate = function () {
alert("Make sure the tree won't fall");
};
tree.getDecorator = function (deco) {
tree[deco].prototype = this;
return new tree[deco]();
};
tree.RedBalls = function () {
this.decroate = function () {
tree.RedBalls.prototype.decroate();
alert('Put on some red balls');
};
};
tree.BlueBalls = function () {
this.decroate = function () {
this.BlueBalls.prototype.decroate();
alert('Add blue balls');
};
};
tree.Angel = function () {
this.decroate = function () {
this.Angel.prototype.decroate();
alert('An angel on the top');
};
};
tree = tree.getDecorator('BlueBalls');
tree = tree.getDecorator('Angel');
tree = tree.getDecorator('RedBalls');
tree.decroate();
观察者模式
var observer = {
addSubscriber: function (callback) {
if (typeof callback === 'function') {
this.subscribers[this.subscribers.length] = callback;
}
},
removeSubscriber: function (callback) {
for (var i = 0; i < this.subscribers.length; i++) {
if (this.subscribers[i] === callback) {
delete this.subscribers[i];
}
}
},
publish: function (what) {
for (var i = 0; i < this.subscribers.length; i++) {
if (typeof this.subscribers[i] === 'function') {
this.subscribers[i](what);
}
}
},
make: function (o) {
for (i in this) {
if (this.hasOwnProperty(i)) {
o[i] = this[i];
o.subscribers = [];
}
}
},
};
var blogger = {
writeBlogPost: function () {
var content = 'Today is ' + new Date();
this.publish(content);
},
};
var la_times = {
newIssue: function () {
var paper = 'Martains have landed on Earth!';
this.publish(paper);
},
};
observer.make(blogger);
observer.make(la_times);
var jack = {
read: function (what) {
console.log('I am just read that ' + what);
},
};
var jill = {
gossip: function (what) {
console.log("You didn't hear it from me, bu " + what);
},
};
blogger.addSubscriber(jack.read);
blogger.addSubscriber(jill.gossip);
blogger.writeBlogPost();
/**
I am just read that Today is Tue Oct 31 2023 13:21:11 GMT+0800 (China Standard Time)
test07.js:49
You didn't hear it from me, bu Today is Tue Oct 31 2023 13:21:11 GMT+0800 (China Standard Time)
test07.js:55
*/
blogger.removeSubscriber(jill.gossip);
blogger.writeBlogPost();
/*
I am just read that Today is Tue Oct 31 2023 13:21:12 GMT+0800 (China Standard Time)
test07.js:49
*/
la_times.addSubscriber(jill.gossip);
la_times.newIssue();
/*You didn't hear it from me, bu Martains have landed on Earth!*/