Created
May 18, 2011 19:55
-
-
Save meltingice/979402 to your computer and use it in GitHub Desktop.
Example singleton class and example for Coffeescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Singleton | |
# We can make private variables! | |
instance = null | |
# Static singleton retriever/loader | |
@get: -> | |
if not @instance? | |
instance = new @ | |
instance.init() | |
instance | |
# Example class method for debugging | |
init: (name = "unknown") -> | |
console.log "#{name} initialized" | |
class Test extends Singleton | |
init: -> | |
super "Test" | |
run: -> | |
alert "OHAI" | |
class OtherTest extends Singleton | |
init: -> | |
super "Other Test" | |
run: -> | |
alert "WUT" | |
Test.get().run() | |
OtherTest.get().run() | |
Test.get().run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var OtherTest, Singleton, Test; | |
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { | |
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } | |
function ctor() { this.constructor = child; } | |
ctor.prototype = parent.prototype; | |
child.prototype = new ctor; | |
child.__super__ = parent.prototype; | |
return child; | |
}; | |
Singleton = (function() { | |
var instance; | |
function Singleton() {} | |
instance = null; | |
Singleton.get = function() { | |
if (!(this.instance != null)) { | |
instance = new this; | |
instance.init(); | |
} | |
return instance; | |
}; | |
Singleton.prototype.init = function(name) { | |
if (name == null) { | |
name = "unknown"; | |
} | |
return console.log("" + name + " initialized"); | |
}; | |
return Singleton; | |
})(); | |
Test = (function() { | |
__extends(Test, Singleton); | |
function Test() { | |
Test.__super__.constructor.apply(this, arguments); | |
} | |
Test.prototype.init = function() { | |
return Test.__super__.init.call(this, "Test"); | |
}; | |
Test.prototype.run = function() { | |
return alert("OHAI"); | |
}; | |
return Test; | |
})(); | |
OtherTest = (function() { | |
__extends(OtherTest, Singleton); | |
function OtherTest() { | |
OtherTest.__super__.constructor.apply(this, arguments); | |
} | |
OtherTest.prototype.init = function() { | |
return OtherTest.__super__.init.call(this, "Other Test"); | |
}; | |
OtherTest.prototype.run = function() { | |
return alert("WUT"); | |
}; | |
return OtherTest; | |
})(); | |
Test.get().run(); | |
OtherTest.get().run(); | |
Test.get().run(); |
@get: ->
if not @instance?
should be
@get: ->
if not instance?
Did you try Test.get() == OtherTest.get()
on the coffeescript example? It should return false
, but it doesn't, since the private variable in the Singleton constructor gets reused on every call,
And in the Javascript example, no instance is reused... http://jsfiddle.net/f22qfa9q/
This works:
class Singleton
@getInstance: ->
@_instance ?= new @(@,arguments...)
constructor: (args...) ->
unless @constructor == args.shift()
throw new Error('Cannot call new on a Singleton')
return args
class Manager extends Singleton
constructor: ->
[@arg] = super
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems that something is wrong as instances are different