Created
          January 29, 2013 13:27 
        
      - 
      
- 
        Save melvynhills/4664216 to your computer and use it in GitHub Desktop. 
    CoffeeScript Singleton base class
  
        
  
    
      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
    
  
  
    
  | 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