Skip to content

Instantly share code, notes, and snippets.

@nsbingham
Created August 20, 2013 16:50
Show Gist options
  • Save nsbingham/6284049 to your computer and use it in GitHub Desktop.
Save nsbingham/6284049 to your computer and use it in GitHub Desktop.
Example module with singletons
module ConfigManager {
export class Browser {}
export class Config {
name:String;
}
export class BrowserManager {
private static _instance:BrowserManager = null;
public config:Config;
public browser:Browser;
constructor() {
if(BrowserManager._instance){
throw new Error("Error: Instantiation failed: Use BrowserManager.getInstance() instead of new.");
}
BrowserManager._instance = this;
}
public static getInstance():BrowserManager
{
if(BrowserManager._instance === null) {
BrowserManager._instance = new BrowserManager();
BrowserManager._instance.config = new Config();
BrowserManager._instance.browser = new Browser();
}
return BrowserManager._instance;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment