Class 如何创建coffeescript单例子类

Class 如何创建coffeescript单例子类,class,node.js,singleton,coffeescript,extend,Class,Node.js,Singleton,Coffeescript,Extend,我已经创建了一个要扩展的单例类。它(一半)的工作原理是只创建类的单个实例,但添加到子类的属性未定义。以下是原始的singleton: class Singleton _instance = undefined @getInstance: -> if _instance is undefined console.log 'no instance exists, so create one' _instance = new _Sing

我已经创建了一个要扩展的单例类。它(一半)的工作原理是只创建类的单个实例,但添加到子类的属性未定义。以下是原始的singleton:

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
下面是子类:

Singleton = require('./singleton')

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


module.exports = Stinky
现在,如果我在我的节点应用程序中使用以下内容:

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

getInstance()方法的行为与预期一致,但var1未定义。如果我在非独生子女的课堂上做同样的事情,他们会很好地工作。谢谢。

我知道您是如何使用
\u Singleton
类来尝试模拟私有类的,但不幸的是,我不认为您可以在这种情况下使用它

以下是一些有效的代码:

class Singleton
   _instance = undefined

   constructor: ->
      console.log 'new singleton'

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

class Stinky extends Singleton
      constructor: ->
         console.log 'Stinky constructor'
         @var1 = 'var1'


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

console.log "Thing var1: #{thing1.var1}"​​​​​​​​​​​​​​​​​​, thing1, thing2​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
我删除了Node.js(require)代码,但将其添加到中应该很简单。主要区别在于我的代码正在创建的实例是
@
this
的实例。这样做将确保首先调用构造函数,然后继续父链。您的代码显式地创建了
\u Singleton
的实例,因此您的
臭烘烘的
构造函数从未被调用。您最终会注意到的另一个小问题是
getInstance
方法实际上没有返回
\u instance
的实例

我希望这有帮助


Sandro

我把你的代码删减了一点。以下是剩下的两个类:

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 )
我做了以下修改:

  • 合并单例和_单例
  • 将"u instance"更改为"u instance",以便将其连接到Singleton而非其原型
  • 在getInstance中添加了参数splat(以防需要参数)
  • 将getInstance()指向扩展对象而不是单例对象

在本例中,我使用了两个不同的数字来确保从未调用第二个构造函数。

我不确定目标是什么,但通过使
单例
成为真正的单例(普通对象),可以实现相同的结果:


对于
Singleton
来说,有一个返回自身的方法没有多大意义。

var1:'var1'
打字错误吗?应该是
var1='var1'
还是实际上
@var1='var1'
?谢谢Sandro。这是真正的咖啡脚本(和Javascript)风格的正确答案。Javascript是一种基于原型的语言,所以像Singleton这样的常见OOP设计模式没有意义。@Vortico我不同意。CoffeeScript有一个
关键字这一事实告诉您,它的目标是模拟一个更熟悉的基于类的继承模型。社区维护的CoffeeScript食谱甚至有一个。遗憾的是,该页面没有提到继承(
Singleton =
    doNothing: ->
        # ...
    doMoreNothing: ->
        # ...

class Stinky
    constructor: ->
        @var1: 'var1'
    getInstance: ->
        return Singleton