Creational Patterns ( 생성 패턴 )
These design patterns provides way to create objects while hiding the creation logic, rather than instantiating objects directly using new operator. This gives program more flexibility in deciding which objects need to be created for a given use case.
생성패턴은 객체의 생성로직을 숨기고 new 명령어를 통하지 않고 객체를 생성하는 방법들을 제공한다. 이는 특정 상황에서 어떤 방법으로 객체를 생성해야할지를 결정하는데 있어서 유연성을 제공한다.
Structural Patterns ( 구조적 패턴 )
These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities.
구조적 패턴들은 클래스와 객체의 구성에 관여한다.
Behavioral Patterns ( 행위적 패턴 )
These design patterns are specifically concerned with communication between objects.
이 패턴들은 객체들 사이의 커뮤니케이션에 관심을 가진다.
오늘 살펴볼 Singleton 패턴은 Creational 패턴에 속한다.
Singleton Pattern Structure
패턴의 목적 ( Intent ) :
Ensure a class only has one instance, and provide a global point of access to it.
프로그램상에서 단 하나의 인스턴스만을 요구하는 클래스를 만들 필요가 있어서 나온 패턴이다. global하게 access가능한 단 인스턴스 생성이 이 패턴의 목적이다.
패턴이 나오게 된 동기 ( Motivation ) :
목적에서 말한 바와 같이 단 하나의 유일한 객체를 생성하고 이를 사용할 목적으로 나오게 된 패턴이다. 한번 생성하면 그 이후에는 생성된 객체를 이용만 할 뿐이다.
유용성 ( 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.
등장 인물 ( Participants ) :
· Singleton
o
defines an Instance operation that lets clients access its unique
instance. Instance is a class operation (that is, a class method in
Smalltalk and a static member function in C++).
o may be responsible for creating its own unique instance.
원리 ( Collaborations ) :
클라이언트들은 이 싱글턴객체에서 제공하는 getter메소드에 의해 오직 혼자만 접근한다.
패턴 사용의 장단점 ( Consequences ):
The Singleton pattern has several benefits:
소스 예제 :
1. 일반적인 경우 ( synchronizing할 필요가 없는 싱글턴 객체의 경우, 싱글쓰레드(single thread) )
// 싱글턴객체가 필요할 때는 인스턴스를 직접 만드는게 아니고 인스턴스를 달라고 요청을 하는 것이다. public class Singleton { private static Singleton uniqueInstance; private Singleton() {} // <--- 생성자는 private -- 외부에서 new 를 사용하여 객체를 생성할 수 없다... public static Singleton getInstance() { // 객체를 생성하는 static 메소드 하나 if ( uniqueInstance == null ) { // 이 부분은 객체를 처음 생성할 때만 필요하다~~ uniqueInstance = new Singleton(); } return uniqueInstance; } }
2. 여러명이 동시접근할 수도 있는
싱글턴 객체
의 경우 ( 멀티쓰레드, multi thread )public class Singleton { private static Singleton uniqueInstance; private Singleton() {} public static Singleton getInstance() { if ( uniqueInstance == null ) { // A Thread가 이 코드를 실행할 때 uniqueInstance = new Singleton(); // B Thread가 객체 생성을 완료하지 않은 경우 } // 두개의 객체가 생성될 수 있다. return uniqueInstance; } }
B.Method synchronizing 으로 문제 해결 가능
public class Singleton { private static Singleton uniqueInstance; private Singleton() {} // 메소드 자체에 synchronized 를 거는 방법 public static synchronized Singleton getInstance() { if ( uniqueInstance == null ) { uniqueInstance = new Singleton(); } return uniqueInstance; } }
이 방법의 문제점 )
C. Double-checking Locking
public class Singleton { private volatile static Singleton uniqueInstance; private Singleton() {} // 부분적으로 synchronized를 걸어 객체가 생성된 이후에는 synchronized 구문을 수행하지 않는다. public static Singleton getInstance() { if ( uniqueInstance == null ) { synchronized( Singleton.class ) { if ( uniqueInstance == null ) { uniqueInstance = new Singleton(); } } } return uniqueInstance; } }
관련 패턴들 :
Many patterns can be implemented using the Singleton pattern. See Abstract Factory, Builder, and Prototype.
추가 정보 :
출처 : http://sourcemaking.com/design_patterns/singleton
Design Patterns : Element of Reusable Object Oriented Software ( by GoF, 1994 )
[디자인패턴] 빌더 패턴 ( Builder Pattern ) (0) | 2015.11.03 |
---|---|
[디자인패턴] 플라이웨이트 패턴 ( Flyweight Pattern ) (0) | 2015.11.03 |
[디자인패턴] 프로토타입 패턴 ( Prototype Pattern ) (0) | 2015.11.03 |
[디자인패턴] 팩토리 메소드 패턴 ( Factory Method Pattern ) (2) | 2015.11.03 |
[디자인패턴] 추상 팩토리 패턴 ( Abstract Factory Pattern ) (0) | 2015.11.03 |