TypeScript로 작성한 싱글턴
class Singleton {
private static instance: Singleton | null = null;
// private 생성자로 외부에서의 인스턴스 생성을 막음
private constructor() {}
// 정적 메서드를 통해 인스턴스에 접근
public static getInstance(): Singleton {
if (!Singleton.instance) {
Singleton.instance = new Singleton();
}
return Singleton.instance;
}
// 다른 메서드들...
public doSomething(): void {
console.log('Doing something...');
}
}
const instance1 = Singleton.getInstance();
const instance2 = Singleton.getInstance();
console.log(instance1 === instance2); // true, 동일한 인스턴스
instance1.doSomething(); // Doing something...
instance2.doSomething(); // Doing something...'Design Pattern' 카테고리의 다른 글
| [DesignPattern] Pub-Sub in TypeScript (0) | 2024.03.16 |
|---|---|
| [DesignPattern] State Pattern in TypeScript (0) | 2024.03.16 |
| [DesignPattern] 디자인 패턴 종류 (0) | 2023.10.13 |