1、普通单例模式
var Singleton = function (name) {
this.name = name
this.instance = null
}
Singleton.getInstance = function (name) {
if (!this.instance) {
this.instance = new Singleton(name)
}
return this.instance
}
2、惰性单例模式
var lazySingleton = function (name) {
this.name = name
}
lazySingleton.getInstance = (function (name) {
var _instance = null
return function (name) {
if (!_instance) {
_instance = new lazySingleton(name)
}
return _instance
}
})()
网友评论