Skip to content

Commit

Permalink
Support naming convention and typescript features
Browse files Browse the repository at this point in the history
  • Loading branch information
rugpanov committed Jun 19, 2018
1 parent 00fff1c commit 5a943b7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions singleton/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ namespace SingletonPattern {
export namespace Demo {

export function show() : void {
var singleton1 = SingletonPattern.Singleton.Instance();
var singleton2 = SingletonPattern.Singleton.Instance();
const singleton1 = SingletonPattern.Singleton.getInstance();
const singleton2 = SingletonPattern.Singleton.getInstance();

if (singleton1 === singleton2) {
console.log("two singletons are equivalent");
Expand Down
8 changes: 5 additions & 3 deletions singleton/singleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ namespace SingletonPattern {
export class Singleton {
// A variable which stores the singleton object. Intially,
// the variable acts like a placeholder
private static singleton: Singleton = null;
private static singleton: Singleton;

// private constructor so that no instance is created
private constructor() {
}

// This is how we create a singleton object
public static Instance(): Singleton {
public static getInstance(): Singleton {
// check if an instance of the class is already created
if (this.singleton == null) {
if (!this.singleton) {
// If not created create an instance of the class
// store the instance in the variable
this.singleton = new Singleton();
Expand Down

0 comments on commit 5a943b7

Please sign in to comment.