Singleton 이란?
- Object Creational Patter (객체 생성 패턴)
: 객체의 인스턴스를 오직 하나만 갖게 하기 위한 패턴. To make a class only have one instance.
ex)) 컴퓨터와 연결되어있는 프린터 여러가지가 있다고 상상해보자. 여러가지가 연결되어있다 하더라도
결국 사용할때는 하나의 프린터만 사용할 수 있다.
Let's say your computer may have access to many printers, but there should only be one print queue.
구조

구조 코드

선언 코드

결과값

장점
- 쉽게 클래스를 많은 인스턴스로 변경시킬수있다.
Very easy to alter the class to allow for a variable number of instances
- static 클래스 메소드보다 훨씬 유연하다 왜냐하면 인스턴스를 많이 만들수 없게 제한하기때문이다.
More flexible than simply using static class methods, as that would make it much more difficult to have variable numbers of instances
단점
- 전역변수와 딱히 크게 다를바가 없다.
Not much better than having a global variable
'개인공부 > 소프트웨어 개발 패턴' 카테고리의 다른 글
| (구조패턴) Adapter 어댑터 패턴 (2) | 2024.10.17 |
|---|---|
| (구조패턴) Composite 컴포짓 패턴 (0) | 2024.10.16 |
| (생성패턴) Builder 빌더 디자인 패턴 (0) | 2024.10.16 |
| (생성패턴) Abstract Factory 추상 팩토리 패턴 (1) | 2024.10.15 |
| (생성패턴) Factory Method : 팩토리메서드 패턴 (1) | 2024.10.15 |