Skip to content

Instantly share code, notes, and snippets.

@meltingice
Created May 18, 2011 19:55
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save meltingice/979402 to your computer and use it in GitHub Desktop.
Save meltingice/979402 to your computer and use it in GitHub Desktop.
Example singleton class and example for Coffeescript
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()
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();
@bumbu
Copy link

bumbu commented Sep 8, 2013

It seems that something is wrong as instances are different

a = Singleton.get()
b = Singleton.get()
console.log a is b # false

@nilaft
Copy link

nilaft commented Jan 26, 2014

@get: ->
    if not @instance?

should be

@get: ->
    if not instance?

@pbihler
Copy link

pbihler commented Sep 23, 2014

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/

@pbihler
Copy link

pbihler commented Sep 23, 2014

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

(from https://coderwall.com/p/nsyuia)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment