参考
1. Pro javascript design patterns/javascript设计模式 (Ross Harmes and Dustin Diaz)单体模式是JavaScript最基本常用的模式之一。它提供了一种将代码组织为一个逻辑单元(对象)然后通过单一的变量(对象名)来访问的方式。
1. 单体模式的主要用途:
(1). 为代码划分命名空间,减少全局变量的数量,提高代码安全性;/* Basic Singleton. */
var Singleton = {
attribute1: true,
attribute2: 10,
method1: function() {
},
method2: function(arg) {
}
// Other methods can go here as well.
};
2.2 访问方法
Singleton.attribute1 = false;
var total = Singleton.attribute2 + 5;
var result = Singleton.method1();
var MyNamespace = {
findProduct: function(id) {
...
},
// Other methods can go here as well.
}
/* DataParser singleton, converts character delimited strings into arrays. */
GiantCorp.DataParser = {
// Private methods.
_stripWhitespace: function(str) {
return str.replace(/\s+/, '');
},
_stringSplit: function(str, delimiter) {
return str.split(delimiter);
},
// Public method.
stringToArray: function(str, delimiter, stripWS) {
if(stripWS) {
str = this._stripWhitespace(str);
}
var outputArray = this._stringSplit(str, delimiter);
return outputArray;
}
};
MyNamespace.Singleton = function() {
return {};
}();
//or
MyNamespace.Singleton = (function() {
return {};
})();
MyNamespace.Singleton = (function() {
return { // Public members.
publicAttribute1: true,
publicAttribute2: 10,
publicMethod1: function() {
...
},
publicMethod2: function(args) {
...
}
};
})();
MyNamespace.Singleton = (function() {
// Private members.
var privateAttribute1 = false;
var privateAttribute2 = [1, 2, 3];
function privateMethod1() {
...
}
function privateMethod2(args) {
...
}
return { // Public members.
publicAttribute1: true,
publicAttribute2: 10,
publicMethod1: function() {
...
},
publicMethod2: function(args) {
...
}
};
})();
MyNamespace.Singleton = (function() {
// Private members.
var privateAttribute1 = false;
var privateAttribute2 = [1, 2, 3];
function privateMethod1() {
...
}
function privateMethod2(args) {
...
}
return { // Public members.
publicAttribute1: true,
publicAttribute2: 10,
publicMethod1: function() {
...
},
publicMethod2: function(args) {
...
}
};
})();
/* General skeleton for a lazy loading singleton, step 1. */
MyNamespace.Singleton = (function() {
function constructor() { // All of the normal singleton code goes here.
// Private members.
var privateAttribute1 = false;
var privateAttribute2 = [1, 2, 3];
function privateMethod1() {
...
}
function privateMethod2(args) {
...
}
return { // Public members.
publicAttribute1: true,
publicAttribute2: 10,
publicMethod1: function() {
...
},
publicMethod2: function(args) {
...
}
}
}
})();
/* General skeleton for a lazy loading singleton, step 2. */
MyNamespace.Singleton = (function() {
function constructor() { // All of the normal singleton code goes here.
...
}
return {
getInstance: function() {
// Control code goes here.
}
}
})();
/* General skeleton for a lazy loading singleton, step 3. */
MyNamespace.Singleton = (function() {
var uniqueInstance; // Private attribute that holds the single instance.
function constructor() { // All of the normal singleton code goes here.
...
}
return {
getInstance: function() {
if(!uniqueInstance) { // Instantiate only if the instance doesn't exist.
uniqueInstance = constructor();
}
return uniqueInstance;
}
}
})();
MyNamespace.Singleton.getInstance().publicMethod1();
/* Branching Singleton (skeleton). */
MyNamespace.Singleton = (function() {
var objectA = {
method1: function() {
...
},
method2: function() {
...
}
};
var objectB = {
method1: function() {
...
},
method2: function() {
...
}
};
return (someCondition) ? objectA : objectB;
})();
单体模式是最常用的设计模式之一,特别是在JavaScript编程中,其优点在前文已见简单介绍过来,不足之处在于有时可能会导致模块间的强耦合,不利于单元测试。在实际应用中单体模式可以和其它设计模式结合使用。不同的设计模式有自己的优点,在不同的具体环境和需求中可以选择最合适的。