Skip to content

Commit

Permalink
Merge pull request torokmark#8 from Pluralsights/master
Browse files Browse the repository at this point in the history
new approach for singleton pattern
  • Loading branch information
torokmark authored Dec 5, 2017
2 parents 007b2b8 + 0488f8a commit fb03481
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions singleton/src/singleton.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
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
}
}
}

0 comments on commit fb03481

Please sign in to comment.