Skip to content
Woo Jin Jang edited this page Jul 10, 2025 · 1 revision

📚 Singleton Pattern 설명

  • 해당 패턴은 프로그램에서 특정 클래스 객체가 단 하나만 존재해야 할 때 필요하다.
  • 인스턴스 요소(instance)들은 새로운 인스턴스 즉, 객체가 생성될 때마다 함께 생성되어 JVM의 힙 영역에 자리잡게된다.
  • 반면 스태틱 요소(static)들은 객체 개수와 상관없이 딱 1개가 존재하는데 JVM의 클래스 영역에 자리잡게된다.

[Ex1]

public class Theme {

    private static Theme instance;  // 테마가 여러 인스턴스를 가진다면 상태가 뒤죽박죽 섞일 것이다.
    private String themeColor;

    private Theme() {               // 생성자를 private로 선언해 외부에서 호출할 수 없도록 방지한다.
        this.themeColor = "light";  // Default theme
    }

    public static Theme getInstance() {
        if (instance == null) {
            instance = new Theme();
        }
        return instance;
    }

    public String getThemeColor() {
        return themeColor;
    }
    public void setThemeColor(String themeColor) {
        this.themeColor = themeColor;
    }
}
public class Button {
    private String label;

    public Button(String label) {
        this.label = label;
    }

    public void display() {
        String themeColor = Theme.getInstance().getThemeColor();  // 싱글톤이기 때문에 객체가 단 하나만 존재
        System.out.println(
            "Button [" + label + "] displayed in " + themeColor + " theme."
        );
    }
}

public class TextField {
    private String text;

    public TextField(String text) {
        this.text = text;
    }

    public void display() {
        String themeColor = Theme.getInstance().getThemeColor();
        System.out.println(
            "TextField [" + text + "] displayed in " + themeColor + " theme."
        );
    }
}

public class Label {
    private String text;

    public Label(String text) {
        this.text = text;
    }

    public void display() {
        String themeColor = Theme.getInstance().getThemeColor();
        System.out.println(
            "Label [" + text + "] displayed in " + themeColor + " theme."
        );
    }
}

❗싱글톤 패턴은 테스트가 어렵다.

싱글톤은 힙 영역에서 여러 객체로 존재하는 것이 아니라 클래스 영역에서 하나의 객체만으로 존재한다. 그렇기 때문에 클래스 내에 전역 인스턴스를 유지하므로 테스트 간 상태가 공유될 우려가 있다. 그렇게 된다면 다음 테스트에 영향을 줄 수 있어 독립적인 테스트 작성이 어려워진다.

📖 Java

📖 Kotlin

📖 Coroutine

📖 Spring

📖 Spring Security

📖 Spring Batch

📖 Reactive Programming

📖 Database

📖 MySQL

📖 Redis

📖 JPA

📖 QueryDsl

📖 MSA

📖 Kafka

📖 Apache Flink

  • [Apache Flink - Apache Flink Architecture]
  • [Apache Flink - Stream Processing]
  • [Apache Flink - Data Stream API & Window]
  • [Apache Flink - State Management]

📖 HTTP

📖 AWS

📖 Docker

📖 Kubernetes

📖 CI/CD

📖 Nginx

📖 Monitoring🥈

  • [Monitoring - Log Concept]
  • [Monitoring - Log Level & Filter]
  • [Monitoring - Logback]
  • [Monitoring - Log Collection with ELK Stack]
  • [Monitoring - Log Monitoring with Kibana]
  • [Monitoring - Building a Monitoring System with Spring Boot Actuator]
  • [Monitoring - Server Monitoring with Prometheus and Grafana with Discord Alerts]

📖 Test

📖 Effective Java 3/E

📖 Kotlin Academy - Effective Kotlin

📖 Kotlin Academy - 핵심편

📖 스프링으로 시작하는 리액티브 프로그래밍

📖 가상 면접 사례로 배우는 대규모 시스템 설계 기초 1

📖 가상 면접 사례로 배우는 대규모 시스템 설계 기초 2

📖 Clean Code

📖 리팩토링 2판

📖 주니어 백엔드 개발자가 반드시 알아야 할 실무 지식

📖 GraphQL

Clone this wiki locally