设计模式分为三大类:
* 创建型模式:涉及对象的创建与初始化
* 结构型模式:描述了如何组合对象以提供新的功能
* 行为型模式:描述了对象之间如何通信
单件模式1:单件是一个创建型的设计模式,主要考虑的是创建对象的方式。
JavaScript没有类的概念,所以单件属于默认行为,也是最自然的模式var single = {};
JavaScript中最基本的单件模式是 对象文本标识法
单间模式2:有一个叫做 Logger()的构造器
var my_log = new Logger();
my_log.log('some event');
//...1000 lines of code later
var other_log = new Logger();
other_log.log('some new event');
console.log(other_log === my_log); //true
/*这段代码的意思:尽管多次使用了new,但是实际上所创建的对象实例却始终只有一个, 后续的构造器调用过程中所返回的始终是同一个对象*/
方法一:全局变量--用全局变量保存这个唯一的实例
function Logger() {
if (typeof global_log === 'undefined'){
global_log = this;
}
return global_log;
}
var a = new Logger();
var b = new Logger();
console.log(a === b); //true
方法二:构造器属性--函数也是一种对象,本身也有属性,
所以将实例设置为构造器的属性
function Logger() {
if (!Logger.single_instance){
Logger.single_instance = this;
}
return Logger.single_instance;
}
工厂模式:工厂模式也属于创建对象的创建型模式,
当我们有对各相似的对象而又不知道应该先使用哪种时,就可以使用工厂模式 假设有三个不同的构造器,所使用的功能相似。所创建的对象都接受一个URL类型的参数,但处理细节稍有不同。 分别创建一个文本DOM节点、一个链接以及一个图像。 var MYAPP = {};
MYAPP.dom = {};
MYAPP.dom.Text = function (url) {
this.url = url;
this.insert = function (where) {
var txt = document.createTextNode(this.url);
where.appendChild(txt);
}
};
MYAPP.dom.Link = function (url) {
this.url = url;
this.insert = function (where) {
var link = document.createElement('a');
link.href = this.url;
link.appendChild(document.createTextNode(this.url));
where.appendChild(link);
}
};
MYAPP.dom.Image =function (url) {
this.url = url;
this.insert = function (where) {
var im = document.createElement('img');
img.src = this.url;
where.appendChild(im);
}
};
//三个构造器的方法都一样:设置url 属性并调用insert()方法
var url = 'http:www.phpied.com/images/covers/oojs.jpg';
var o = new MYAPP.dom.Image(url);
o.insert(document.body);
var o = new MYAPP.dom.Text(url);
o.insert(document.body);
var o = new MYAPP.dom.Link(url);
o.insert(document.body);
//随机创建一种类型image Link Text,创建一个工厂函数
MYAPP.dom.factory = function (type, url) {
return new MYAPP.dom[type](url);
}
var image = MYAPP.dom.factory('Image',url);
image.insert(document.body);
/*//复杂方式
var o;
if (type === 'Image'){
o = new MYAPP.dom.Image();
}
if (type === 'Link'){
o = new MYAPP.dom.Link();
}
if (type === 'Text'){
o = new MYAPP.dom.Text();
}
o.url = 'http://...'
o.insert();*/
装饰器模式:
装饰器模式是一种结构型模式,它与对象的创建无关,主要考虑如何扩展对象的功能。
也就是说,说了使用线性式(父-子-孙)继承方式之外,我们也可以为一个基础对象创建若干个装饰
对象扩展其功能。
var obj = {
doSomething:function () {
console.log('sure,asap');
}
//...
};
obj = obj.getDecorator('decol');
obj = obj.getDecorator('decol3');
obj = obj.getDecorator('deco5');
obj.doSomething();
//装饰一个圣诞树
var tree = {};
tree.decorate = 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.decorate = function () {
this.RedBalls.prototype.decorate();
alert('Put on some red balls');
};
};
tree.BlueBalls = function () {
this.decorate = function () {
this.BlueBalls.prototype.decorate();
alert('Add blue balls');
};
};
tree.Angel = function () {
this.decorate = function () {
this.Angel.prototype.decorate();
alert('An angel on the top');
};
};
tree = tree.getDecorator('BlueBalls');
tree = tree.getDecorator('Angel');
tree = tree.getDecorator('RedBalls');
//最后,运行decorate()方法
tree.decorate();
/*/依次弹出
* Make sure the tree won't fall
* Add blue balls
* An angel on the top
* Put on some red balls*/