[디자인 패턴] 싱글톤 패턴

succeeding·2023년 3월 10일
0

디자인 패턴

목록 보기
3/3

구현

const mutexify = require('mutexify/promise')


const lock = mutexify()

class Singleton {
  private static _instance: Singleton

  private constructor() {}

  static async getInstance() {
    if (!this._instance) {
      const release = await lock()
      !this._instance && (this._instance = new A())
      release()
    }
    return this._instance
  }

  log() {
    console.log('싱글이야?')
  }
}

const a = Singleton.getInstance()
a.log()

문제점

SOLID 원칙 중 아래 두 가지를 위반한다.

  • OCP
  • DIP

이유에 대해선 여기를 보면 힌트를 얻을 수 있을 것 같다.

0개의 댓글