# 싱글턴

![](/files/-LDoRv_kcACld43Xu147)

## 싱글턴 패턴 <a href="#singleton-pattern" id="singleton-pattern"></a>

`private` 접근 제어자를 사용해 `constructor()` 앞에 붙이면 `new` 키워드를 통해 인스턴스를 생성하지 못하도록 제한할 수 있습니다. 대신 공개된 스태틱 메서드 `getInstance()`를 통해 **오직 한 번만 인스턴스를 생성**할 수 있습니다. 이를 싱글턴 패턴이라 부릅니다.

```typescript
class OnlyOne {

  private static instance: OnlyOne;

  public name:string;

  // new 클래스 구문 사용 제한을 목적으로
  // constructor() 함수 앞에 private 접근 제어자 추가
  private constructor(name:string) {
    this.name = name;
  }
  
  // 오직 getInstance() 스태틱 메서드를 통해서만
  // 단 하나의 객체를 생성할 수 있습니다.
  public static getInstance() {
    if (!OnlyOne.instance) {
      OnlyOne.instance = new OnlyOne('싱글턴 객체');
    }
    return OnlyOne.instance;
  }
  
}


/* 인스턴스 생성 ------------------------------------------------ */

// [오류]
// [ts] 'OnlyOne' 클래스의 생성자는 private이며 클래스 선언 내에서만 액세스할 수 있습니다.
// constructor OnlyOne(name: string): OnlyOne
let bad_case = new OnlyOne('오류 발생');

let good_case = OnlyOne.getInstance();
```

{% hint style="danger" %}
컴파일된 JavaScript(ES5) 코드를 살펴보면 싱글턴 패턴과 상관 없이 여러 차례 인스턴스를 생성할 수 있습니다. 하지만 **JavaScript는 객체({}) 자체가 싱글턴**이 될 수 있습니다.
{% endhint %}

**컴파일 코드:**

{% code title="JavaScript" %}

```javascript
var OnlyOne = /** @class */ (function () {
  function OnlyOne(name) {
    this.name = name;
  }
  OnlyOne.getInstance = function () {
    if (!OnlyOne.instance) {
      OnlyOne.instance = new OnlyOne('싱글턴 객체');
    }
    return OnlyOne.instance;
  };
  return OnlyOne;
}());


let only_one    = new OnlyOne('오류 발생'); // 정상 작동
let special_one = new OnlyOne('스페셜 원'); // 새로운 인스턴스 생성!
let normal_one  = new OnlyOne('노멀 원');  // 새로운 인스턴스 생성!!
```

{% endcode %}

## 실습 <a href="#practice" id="practice"></a>

{% embed url="<https://stackblitz.com/edit/ts-singleton?embed=1&file=index.ts&hideExplorer=0&hideNavigation=0&view=editor>" %}

## 참고 <a href="#reference" id="reference"></a>

{% embed url="<https://basarat.gitbooks.io/typescript/docs/tips/singleton.html>" %}
TypeScript - 싱글턴 패턴
{% endembed %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yamoo9.gitbook.io/typescript/classes/singleton.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
