如何创建coffeescript singleton子类

Bra*_*odt 6 singleton class extend node.js coffeescript

我创建了一个我想要扩展的单例类.它(一半)的工作原理是它只创建一个类的实例,但是添加到子类的属性是未定义的.这是最初的单身人士:

class Singleton
   _instance = undefined
   @getInstance: ->
      if _instance is undefined
         console.log 'no instance exists, so create one'
         _instance = new _Singleton()
      else
         console.log 'an instance already exists.'

class _Singleton
   constructor: ->
      console.log 'new singelton'

module.exports = Singleton
Run Code Online (Sandbox Code Playgroud)

这是子类:

Singleton = require('./singleton')

class Stinky extends Singleton
      constructor: ->
         var1 : 'var1'


module.exports = Stinky
Run Code Online (Sandbox Code Playgroud)

现在,如果我在节点应用程序中使用以下内容:

Stinky = require './stinky'
thing1 = Stinky.getInstance()
thing2 = Stinky.getInstance()
console.log "Thing var1: #{thing1.var1}"
Run Code Online (Sandbox Code Playgroud)

getInstance()方法按预期运行,但var1未定义.如果我在非单件类上做同样的事情,他们工作正常.谢谢.

Rob*_*rle 12

我把你的代码缩减了一点.以下是剩下的2个课程:

class Singleton
  @_instance: null
  @getInstance: ->
    @_instance or= new @( arguments... )

class Stinky extends Singleton
  constructor: ( @num ) ->

thing1 = Stinky.getInstance( 1 )
thing2 = Stinky.getInstance( 2 )

console.log( thing1.num, thing2.num )
Run Code Online (Sandbox Code Playgroud)

我做了以下更改:

  • 合并Singleton和_Singleton
  • 将_instance更改为@_instance,以便将其附加到Singleton而不是其原型
  • 在getInstance中添加了参数splat(如果需要参数)
  • 将getInstance()指向扩展对象而不是Singleton

在这个例子中,我使用了2个不同的数字来确保永远不会调用第二个构造函数.


San*_*dro 2

我看到您如何使用该类_Singleton来尝试模拟私有类,但不幸的是,我认为您不能在这种情况下使用它。

\n\n

这是一些有效的代码:

\n\n
class Singleton\n   _instance = undefined\n\n   constructor: ->\n      console.log \'new singleton\'\n\n   @getInstance: ->\n      if _instance is undefined\n         console.log \'no instance exists, so create one\'\n         _instance = new @()\n      else\n         console.log \'an instance already exists.\'\n      _instance\n\nclass Stinky extends Singleton\n      constructor: ->\n         console.log \'Stinky constructor\'\n         @var1 = \'var1\'\n\n\nthing1 = Stinky.getInstance()\nthing2 = Stinky.getInstance()\n\nconsole.log "Thing var1: #{thing1.var1}"\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b, thing1, thing2\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\n
Run Code Online (Sandbox Code Playgroud)\n\n

我删除了 Node.js(必需)代码,但添加该代码应该很简单。主要区别在于我的代码创建的实例是@or的实例this。这样做将确保首先调用您的构造函数,然后继续沿着父链向上。您的代码显式创建了一个实例,_Singleton因此您的Stinky构造函数从未被调用。您最终会注意到的另一个小问题是您的getInstance方法实际上并未返回_instance.

\n\n

我希望这有帮助,

\n\n

桑德罗

\n