Skip to content

Instantly share code, notes, and snippets.

@melvynhills
Created January 29, 2013 13:27
Show Gist options
  • Save melvynhills/4664216 to your computer and use it in GitHub Desktop.
Save melvynhills/4664216 to your computer and use it in GitHub Desktop.
CoffeeScript Singleton base class
singletonCheck = {}
class Singleton
@getInstance: -> @instance ?= new @ singletonCheck
constructor: (check) ->
if check isnt singletonCheck
throw new Error "Singleton must be instantiated with getInstance()"
return
console.log "Singleton", "init"
@init?()
class MySingletonClass extends Singleton
init: ->
console.log "MySingletonClass", "init"
MySingletonClass.getInstance() # OK
new MySingletonClass() # Error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment