【发布时间】:2010-12-02 11:27:30
【问题描述】:
有谁知道如何使 Dojo 类成为单例,或者至少如何在 dojo 类中创建静态方法或变量?
我目前通过为每个类设置一个全局变量和一个在其为空时设置该变量的方法来实现这一点,但这是一个糟糕的解决方案。拥有一个单例类会更好,因为可以从它继承,并且瞧,有一个单例:)
海因里希
【问题讨论】:
标签: javascript dojo
有谁知道如何使 Dojo 类成为单例,或者至少如何在 dojo 类中创建静态方法或变量?
我目前通过为每个类设置一个全局变量和一个在其为空时设置该变量的方法来实现这一点,但这是一个糟糕的解决方案。拥有一个单例类会更好,因为可以从它继承,并且瞧,有一个单例:)
海因里希
【问题讨论】:
标签: javascript dojo
以下文章作为背景信息很好地定义了 JavaScript 中单例对象的模式:
http://kaijaeger.com/articles/the-singleton-design-pattern-in-javascript.html
为了 Dojo-tize 这个,使用 1.7+ 我们需要在闭包中捕获原始构造函数,以便闭包之外的任何人都无法访问原始构造函数并提供一个 总是返回相同实例的访问器方法,无论谁试图获取 参考一下...
将“类”构造函数函数转换为可共享、可重用的代码段的方法也很有意义。我在 Dojo 中的偏好是拥有这个 作为它自己的“模块”作为实用方法(不是 Dojo 类对象),所以我们开始...
MakeSingleton.js
define(['dojo/_base/lang'], function(lang)
{
return function(ctor) { // not defining a class, just a utility method.
var singletonCtor, // our singleton constructor function.
instance = null; // singleton instance provided to any clients.
// define the singleton constructor with accessor method.
// (captures 'ctor' parameter and 'instance' variable in a function
// closure so that they are available whenever the getInstance() method
// on the singleton is called.)
singletonCtor = new function() { // note: 'new' is important here!!
this.getInstance = function() { // our accessor function
if (!instance) { // captures instance in a closure
instance = new ctor(); // create instance using original ctor.
instance.constructor = null; // remove instance's constructor method
} // so you cannot use new operator on it!!
return instance; // this is our singleton instance.
}
};
// Since we are working with Dojo, when declaring a class object, if you
// provide a 'className' as the first parameter to declare(...), Dojo will
// save that value in the 'declaredClass' property on the object's prototype...
// ... and adds a reference to that constructor function in the global namespace
// as defined by the 'className' string.
//
// So if 'declaredClass' has a value, we need to close the hole by making
// sure this also refers to the singleton and not the original constructor !!
//
if (ctor.prototype && ctor.prototype.declaredClass) {
lang.setObject(ctor.prototype.declaredClass, singletonCtor);
}
// return the singleton "constructor" supports only a getInstance()
// method and blocks the use of the 'new' operator.
return singletonCtor;
}; // return "MakeSingleton" method
}; // define(...)
那么当我们想要定义一个 Singleton 类对象时,我们如何在 Dojo 1.7+ 中使用它呢? 很简单,因为我们已经完成了上面的繁重工作......
MySingletonClass.js
define(['dojo/_base_declare', 'MakeSingleton'],
function(declare, MakeSingleton)
{
return MakeSingleton( declare('MySingletonClass', [...], {
// Define your class here as needed...
}));
});
那么这里发生了什么...
调用 declare(...) 的结果直接传递到 MakeSingleton(...) 实用程序
方法,因此 Dojo 创建的原始类构造函数(Function)永远不会公开,如果将“className”传递给 declare(...),MakeSingleton 还确保
不是原来的构造函数,而是单例对象。此外,从出口
这个模块也是单例对象(MakeSingleton的返回值),所以Dojo
loader 在运行工厂方法后只有对单例的引用。这
原始构造函数类在单例对象的闭包中被捕获,所以
没有其他人可以使用它并创建一个额外的实例...
我们真的有一个单身人士。
那么我们如何访问这个单例... 声明你的类,获得它的唯一方法是通过模块依赖引用。如果您确实像上面的示例中那样指定了“className”(耻辱,耻辱),您可以访问它 来自全局名称空间(不是 Dojo 的发展方向,使用模块依赖引用)。
调用 MakeSingleton.js 实用模块的导出方法的结果是 对象上有一个名为 getInstance() 的方法。 getInstance() 将创建 在第一次调用时返回原始类对象的实例,并在每次后续调用时返回相同的实例。如果您尝试在单例类上使用“新”,则会产生错误。如果您尝试在全局命名空间中的引用上使用“新”(如果 你提供了一个'className'来声明),它会产生一个错误。 唯一方法 获取实例就是调用单例的getInstance()方法。
SomeOtherModule.js
define(['dojo/_base/declare', 'MySingletonClass'],
function(declare, MySingletonClass)
{
return declare(null, {
mySingleton: null, // here we will hold our singleton reference.
constructor: function(args) {
...
// capture the singleton...
mySingleton = MySingletonClass.getInstance();
...
mySingleton.doSomething(...);
};
mySpecialSauce: function(...) {
mySingleton.doSomethingElse(...);
};
moreSauce: function(...) {
var x;
x = MySingletonClass.getInstance(); // gets same instance.
x = new window.MySingletonClass(); // generates an error!!
x = new MySingletonClass(); // generates an error!!
// Dojo's loader reference generates an error as well !!
x = new require.modules['MySingletonClass'].result();
};
});
});
无论有多少模块、类、脚本元素等获得对 单例对象,它们都将引用同一个实例,而“新”实例不能 已创建。
【讨论】:
require(["dojo/_base/declare"], function (declare) {
var o =
declare("com.bonashen.Singleton", null, {
say : function (name) {
console.log("hello," + name);
}
});
//define static getInstance function for com.bonashen.Signleton class.
console.debug("define getInstance function. ");
o.getInstance = function () {
if (null == o._instance)
o._instance = new o();
return o._instance;
};});
【讨论】:
好的,对于 dojo 1.7+ AMD(您可以在其他文件中要求,等等),实际上没有人给出一个好的答案 这是我所拥有的:
define([
"dojo/_base/declare",
], function(
declare
) {
var SingletonClass = declare("SingletonClass", [], {
field: 47,
method: function(arg1) {
return field*5;
}
});
if (!_instance) {
var _instance = new SingletonClass();
}
return _instance;
});
它似乎工作得很好,而且很有意义。
【讨论】:
return new SingletonClass(),因为一个模块的值只能确定一次。
如果你使用dojo.declareClass 来创建一个新类,你总是可以使用new 操作符来创建它的一个新实例。在 Java 中,单例是使用私有构造函数实现的,但 JavaScript 没有这种能力。所以你不能在 JavaScript 中创建类似 Java 的单例。
所以我创建单例的典型方法是:
if (typeof dojo.getObject('x.y.z') === 'undefined') {
dojo.setObject('x.y.z', {
//object definitions
});
}
要创建静态变量,只需将变量添加到 dojo 类对象即可。
dojo.declare("x.y.ABC", {});
x.y.ABC.globalV = 'abc';
与dojo.require一起使用时,一个JavaScript文件不会被dojo加载两次,所以你不需要检查变量是否存在。
【讨论】:
为什么不在构造函数中检查单例的创建,例如:
define(['dojo/_base/declare'], function (declare) {
var singletonClass = declare(null, {
someProperty: undefined,
constructor: function () {
if (singletonClass.singleton)
throw new Error('only one instance of singletonClass may be created');
this.someProperty = 'initial value';
}
});
// create the one and only instance as a class property / static member
singletonClass.singleton = new singletonClass();
return singletonClass;
});
然后像这样使用它:
define(["app/singletonClass"], function(singletonClass) {
var instance = singletonClass.singleton; // ok
var newInstance = new singletonClass(); // throws Error
});
【讨论】:
为什么所有这些代码都用来制作 Dojo 单例?在模块中返回一个 new Class() 还不够吗?
例如:
模块:
define(["dojo/_base/declare"], function(declare){
var TestApp = declare(null, {
constructor: function(){
console.log("constructor is called only once");
this.key = Math.random();
console.log("generated key: "+this.key);
},
sayHello : function(){
console.log("sayHello: "+this.key);
}
});
return new TestApp();
});
测试:
<script>
require(['simplemodule.js'], function (amodule) {
amodule.sayHello();
console.log("first require");
});
require(['simplemodule.js'], function (amodule) {
amodule.sayHello();
console.log("second require");
});
require(['simplemodule.js'], function (amodule) {
amodule.sayHello();
console.log("third require");
});
</script>
输出:
constructor is called only once simplemodule.js:4
generated key: 0.6426086786668748 simplemodule.js:6
sayHello: 0.6426086786668748 simplemodule.js:9
first require test.html:15
sayHello: 0.6426086786668748 simplemodule.js:9
second require test.html:20
sayHello: 0.6426086786668748 simplemodule.js:9
third require
【讨论】:
在 javascript 中考虑类的实例并不总是有意义的。如果您不想覆盖任何先前定义的对象,您可以执行以下操作:
something.myObject = something.myObject || {
//Here you build the object
}
因此,如果之前定义的something.myObject 已经定义,您将获得它,或者(使用||)您使用{} 构建新对象。对对象的任何修改都会保留,因为您只构建一个新对象,如果它之前是一个假值。
【讨论】:
这就是我在 Dojo 中制作单身人士的方式:
var makeSingleton = function (aClass) {
aClass.singleton = function () {
var localScope = arguments.callee;
localScope.instance = localScope.instance || new aClass();
return localScope.instance;
};
return aClass;
};
makeSingleton(dojo.declare(...));
然后,使用它:
myClass.singleton()
【讨论】:
考虑以下列方式声明一个单音对象(Dojo 1.10),它使用闭包来保持对模块中实例的引用并将方法添加到函数的原型中。
可以在here找到有关该主题的好文章。
define([
], function (
) {
'use strict';
var _instance;
function _SingleTone() {
}
_SingleTone.prototype = {
init: function () {
}
};
return function _getSingleton() {
return (_instance = (_instance || new _SingleTone()));
};
});
【讨论】: