티스토리 뷰
생성패턴 (Creational Pattern) | 구조패턴 (Structural Pattern) | 행위패턴 (Behavioral Pattern) |
- 추상 팩토리 (Abstract Factory) - 팩토리 메소드 (Factory Method) - 프로토타입 (Prototype) - 빌더 (Builder) - 싱글톤 (Singleton) |
- 어댑터 (Adapter) - 컴포지트 (Composite) - 브리지 (Bridge) - 데코레이터 (Decorator) - 플라이웨이트 (Flyweight) - 프록시 (Proxy) - 퍼사드 (Facade) |
- 책임 연쇄 (Chain of Responsibility) - 커맨드 (Command) - 인터프리터 (Interpreter) - 이터레이터 (Iterator) - 미디에이터 (Mediator) - 비지터 (Visitor) - 템플릿 메소드 (Template Method) - 스테이트 (State) - 옵저버 (Observer) - 메멘토 (Memento) |
생성패턴 (Creational Pattern)
> 객체 생성에 관련된 패턴
> 객체 생성과 조합을 캡슐화해 유연성 제공
Singleton 패턴?
- 인스턴스가 하나만 생성됨을 보증하는 패턴
적용 예제
싱글톤은 클래스 로드와 초기화를 알아야 적용하기 쉽다.
클래스는 언제 로딩되고 초기화되는가? (feat. 싱글톤)
클래스는 언제 로딩되고 초기화되는가?
velog.io
1. Eager Initialization
public class EagerSingleton {
private static EagerSingleton instance = new EagerSingleton();
// private constructor
private EagerSingleton() {
}
public static EagerSingleton getInstance() {
return instance;
}
}
- 간단하게 싱글톤 메소드 생성
- 클라이언트에서 사용하지 않아도 생성됨
- getInstance() 메소드 말고 다른 static 메소드 추가해서 호출하면 EagerSingleton 클래스 로드 되면서 인스턴스 생성
2. Lazy Initialization
public class LazyInitializedSingleton {
private static LazyInitializedSingleton instance;
private LazyInitializedSingleton(){}
public static LazyInitializedSingleton getInstance(){
if(instance == null){
instance = new LazyInitializedSingleton();
}
return instance;
}
}
- Eager Initialization 단점 보완
- 멀티 쓰레드 환경에서 각 쓰레드마다 instance == null일 경우 쓰레드마다 생성됨
3. Thread-Safe Singleton
public class ThreadSafeSingleton {
private static ThreadSafeSingleton instance;
private ThreadSafeSingleton(){}
public static synchronized ThreadSafeSingleton getInstance(){
if(instance == null){
instance = new ThreadSafeSingleton();
}
return instance;
}
}
- 인스턴스 생성하는 메소드에 synchronized 추가
- 하나의 쓰레드만 접근 가능하므로 비효율
4. Double-Checked Locking
public static ThreadSafeSingleton getInstanceUsingDoubleLocking(){
if(instance == null){
synchronized (ThreadSafeSingleton.class) {
if(instance == null){
instance = new ThreadSafeSingleton();
}
}
}
return instance;
}
- synchronized 앞뒤로 인스턴스 체크 (쓰레드 관련된 부분이라 volatile 키워드를 알면 좋을듯 싶다)
5. Bill pugh Solution
public class BillPughSingleton {
private BillPughSingleton(){}
private static class SingletonHelper{
private static final BillPughSingleton INSTANCE = new BillPughSingleton();
}
public static BillPughSingleton getInstance(){
return SingletonHelper.INSTANCE;
}
}
- Lazy Loading 가능
- BillPughSingleton 클래스가 로드되더라도 static SingletonHelper 클래스는 로드되지 않음
- 쓰레드 세이프 (클래스 로드되는 시점이 다른 것을 활용한 방법으로 jvm에게 인스턴스 생성 맡기는 느낌)
6. Relection을 활용한 싱글톤 망가뜨리기
public static void main(String[] args) {
EagerInitializedSingleton instanceOne = EagerInitializedSingleton.getInstance();
EagerInitializedSingleton instanceTwo = null;
try {
Constructor[] constructors = EagerInitializedSingleton.class.getDeclaredConstructors();
for (Constructor constructor : constructors) {
//Below code will destroy the singleton pattern
constructor.setAccessible(true);
instanceTwo = (EagerInitializedSingleton) constructor.newInstance();
break;
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(instanceOne.hashCode());
System.out.println(instanceTwo.hashCode());
}
- constructor.setAccessible(true)를 통해서 싱글톤을 망가뜨리고 해당 싱글톤의 private 생성자 메소드 접근 가능
- 싱글톤 클래스 생성자도 접근 가능해졌으므로 여러 인스턴스 생성가능
7. Enum Singleton
public enum EnumSingleton {
INSTANCE;
public void someMethod(String param) {
// some class member
}
}
- enum이니깐 스레드 세이프
- 직렬화/역직렬화 고민할 필요 없음
reference
All About the Singleton - DZone Java
This guide to the Singleton design pattern will help narrow down some appropriate scopes and uses, such as serialization and eager and lazy loading.
dzone.com
디자인패턴 - 싱글톤 패턴
개요 싱글톤 패턴의 다양한 구현 방법을 알아본다. Thread Safe 한 싱글톤 패턴의 구현도 포함한다. volatile 과 memory consistency 도 조금 알아본다. 순서요약 Eager Initialization (Early Loading) Static Block Initiali
yaboong.github.io
'JAVA > 디자인패턴' 카테고리의 다른 글
[디자인패턴] 어댑터 (Adapter Pattern) (0) | 2022.05.03 |
---|---|
[디자인패턴] 퍼사드 (Facade Pattern) (0) | 2022.03.24 |
[디자인패턴] 이터레이터 (Iterator Pattern) (0) | 2022.03.07 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 객체지향과 디자인패턴
- 알고리즘
- 문자열 내 마음대로 정렬하기
- 카카오인턴
- 예매 알림
- java
- codility
- K번째수
- json
- 맵 api
- 다음 맵 api
- 스프링 부트
- 타겟 넘버
- AWS
- 완주하지 못한 선수
- FrogJmp
- 안드로이드 스튜디오
- 파이팅코리아
- 쇠막대기
- cgv
- 자바
- 기능개발
- 최솟값
- 노션트렐로광고X
- 텔레그램
- Spring
- 더 나은 내일
- API
- 124 나라의 숫자
- 프로그래머스
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함