Skip to content

Commit

Permalink
new approach for singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
chandanch authored Nov 18, 2017
1 parent f0a4cf2 commit 0e2785e
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions singleton/src/singleton.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
namespace SingletonPattern {
export class Singleton {
private static instance: Singleton;

constructor() {}

static get Instance() {
if (this.instance === null || this.instance === undefined) {
this.instance = new Singleton();
namespace Singleton {
class Singleton {
// A variable which stores the singleton object. Intially,
// the variable acts like a placeholder
private static __singleton: Singleton = null;
// private constructor so that no instance is created
private constructor() {
}
// This is how we create a singleton object
public static getInstance(): Singleton {
// check if an instance of the class is already created
if (this.__singleton == null) {
// If not created create an instance of the class
// store the instance in the variable
this.__singleton = new Singleton();
}
return this.instance;
// return the singleton object
return this.__singleton
}
public getSingletonName(name: string) {
alert(`Singleton is ${name}`)
}
}
}

0 comments on commit 0e2785e

Please sign in to comment.