Design patterns 07.

Creational patterns - Singleton

by Dichter
단일체(Singleton)이란?

우선 에릭 감마를 비롯한 4인방이 소개하는 원형의 의도, 동기 그리고 활용을 읽어보자. 어렵지 않은 단어와 문장이니, 의미를 생각해보며 정독해보자.


Intent

Ensure a class only has one instance, and provide a global point of access to it.


Motivation

It's important for some classes to have exactly one instance. Although there can be many printers in a system, there should be only one printer spooler. There should be only one file system asnd one window manager. A digital filter will have one A/D converter. An accounting system will be dedicated to serving one company.


A global variable makes an object accessible, but it doesn't keep you from instantiating multiple objects.


A better solution is to make the class itself responsible for keeping track of its sole instance. The class can ensure that no ohter instance can be created (by interceptiing requests to create new objects), and it can provide a way to access the instance. This is the Singleton pattern.


Applicability

Use the Singleton pattern when

there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point.

when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.


단일체(Singleton)는 가장 자주 쓰이는 생성 패턴 중 하나이다. 위 설명과 같이 프린터가 제일 쉬운 예제이다. 다양한 프로그램이 운영 프로그램을 통하여 프린터를 호출한다면, 매번 프로그램이 프린터 객체를 생성한다면, 잘못된 결과를 야기할 확률이 크다. 실제 프린터가 하나라면, 단일체를 이용하여 프린터 객체를 정의한다면, 여러 프로그램이 단일체를 이용하여 동일한 프린터 동작을 수행하도록 할 수 있다.


단일체 예제

작성 중...


라파엘악마와 싸우는 성 미카엘
악을 물리치는 신을 상징하는 이 그림은 기독교에서 상징적인 의미를 갖게 되었다. 라파엘은 은밀한 방법으로 신성을 표현하는 그림을 그리는 재능을 가졌으며, 인물의 움직임과 공간을 창조하는 데에 능숙했다.


1쇄. 2020.12.14.

keyword
작가의 이전글Design patterns 06.