Dojo Singleton或至少是静态方法/变量?

Eri*_*rik 5 javascript dojo

有没有人知道如何使Dojo类成为单例,或者至少如何在dojo类中创建静态方法或变量?

我目前通过为每个类设置一个全局变量以及一个设置此变量的方法来实现这一点,如果它是null,但这是一个糟糕的解决方案.拥有一个单独的类会更好,因为一个人可以继承它并且voilá有一个单身:)

海因里希

Jon*_*onS 15

以下文章作为背景信息很好地定义了JavaScript中单例对象的模式:http:
//kaijaeger.com/articles/the-singleton-design-pattern-in-javascript.html

为了使用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(...)
Run Code Online (Sandbox Code Playgroud)

那么当我们想要定义一个Singleton类对象时,我们如何在Dojo 1.7+中使用它呢?很简单,因为我们已经完成了上面的繁重工作......

MySingletonClass.js

define(['dojo/_base_declare', 'MakeSingleton'], 
       function(declare, MakeSingleton)
{
    return MakeSingleton( declare('MySingletonClass', [...], {
                          // Define your class here as needed...
                        }));  
});
Run Code Online (Sandbox Code Playgroud)

那么这里发生了什么...调用declare(...)的结果直接传递给MakeSingleton(...)实用程序方法,因此Dojo创建的原始类构造函数(Function)永远不会公开,并且如果'className'被传递给declare(...),MakeSingleton也确保它不是原始构造函数,而是单例对象.此外,从该模块导出也是单例对象(MakeSingleton的返回值),因此Dojo加载器在运行工厂方法后只有对单例的引用.原始构造函数类是在单例对象的闭包内捕获的,因此没有其他人可以访问它并创建一个额外的实例......
我们真的有一个单例.

那么我们如何访问这个单例...如果你在声明类时没有指定'className',那么获得它的唯一方法是通过模块依赖引用.如果您确实指定了一个'className',如上例所示(羞耻,羞耻),您可以从全局名称空间访问它(这不是 Dojo的方式,使用模块依赖项引用).

调用MakeSingleton.js实用程序模块的导出方法的结果是一个对象,它上面有一个名为getInstance()的方法.getInstance()将在第一次调用时创建原始类对象的实例,并在每次连续调用时返回相同的实例.如果您尝试在单例类上使用"new",则会生成错误.如果您尝试在全局命名空间中的引用上使用"new"(如果您提供了'className'来声明),则会生成错误.获取实例的唯一方法是调用singleton的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(); 
        };
    });
});
Run Code Online (Sandbox Code Playgroud)

无论有多少模块,类,脚本元素等都引用了单例对象,它们都将引用同一个实例,并且无法创建"新"元素.


小智 8

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;
};});
Run Code Online (Sandbox Code Playgroud)


小智 8

好吧,没有人真正为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;
});
Run Code Online (Sandbox Code Playgroud)

它似乎工作得很好,并且很有道理.

  • 你可以只返回新的SingletonClass()`,因为模块的值只确定一次. (4认同)

Ale*_*eng 7

如果您使用dojo.declareClass创建新类,则始终可以使用new运算符创建它的新实例.在Java中,单例是使用私有构造函数实现的,但JavaScript没有这种功能.所以你不能在JavaScript中创建类似Java的单例.

所以我创建单例的典型方法是:

if (typeof dojo.getObject('x.y.z') === 'undefined') {
    dojo.setObject('x.y.z', {
       //object definitions
    });
}
Run Code Online (Sandbox Code Playgroud)

要创建静态变量,只需将该变量添加到dojo类对象即可.

dojo.declare("x.y.ABC", {});
x.y.ABC.globalV = 'abc';
Run Code Online (Sandbox Code Playgroud)

使用时dojo.require,dojo不会两次加载JavaScript文件,因此您无需检查变量是否存在.