노현진's Blog

Singleton Pattern

Singleton Pattern에 대해 설명하는 페이지입니다.

Posted
Preview Image
By HyunJinNo

Tags

Design Pattern, Java

1. Introduction

  • Purpose
    • Ensures that only one instance of a class is allowed within a system.
  • Use When
    • Exactly one instance of a class is required.
    • Controlled access to a single object is necessary.

2. How to Use (Example)

  • For single-thread

    • Make the instructor be private
      • private Singleton()
    • Provide a getInstance() method
      • public static Singleton getInstance()
    • Remember the instance once you have created it
      • private static Singleton instance
      • if (instance == null) instance = new Singleton();
    java
    1public class Singleton {
    2    private static Singleton instance;
    3
    4    // other useful instance variables
    5
    6    private Singleton() { //... }
    7
    8    public static Singleton getInstance() {
    9        if (instance == null) {
    10            instance = new Singleton();
    11        }
    12        return instance;
    13    }
    14
    15    // other useful methods
    16}
  • For multi-thread

    • Option 1

      • Use synchronized keyword
        • public static synchronized Singleton getInstance()
      • Cons: It causes small impact on run-time performance due to frequent locking.
      java
      1public class Singleton {
      2    private static Singleton instance;
      3
      4    // other useful instance variables
      5
      6    private Singleton() { //... }
      7
      8    public static synchronized Singleton getInstance() {
      9        if (instance == null) {
      10            instance = new Singleton();
      11        }
      12        return instance;
      13    }
      14
      15    // other useful methods
      16}
    • Option 2

      • Pros: no runtime overhead
      • Cons: resource overhead when the instance is not used
      java
      1public class Singleton {
      2    private static Singleton instance = new Singleton();
      3
      4    // other useful instance variables
      5
      6    private Singleton() { //... }
      7
      8    public static Singleton getInstance() {
      9        return instance;
      10    }
      11
      12    // other useful methods
      13}
    • Option 3

      • Use volatile keyword and double-checked locking
      • Pros: theoretically perfect solution with respect to performance
      • Cons: dependent on the java version (We have to ensure that we are running at least Java 5)
      java
      1public class Singleton {
      2    private volatile static Singleton instance = null;
      3
      4    // other useful instance variables
      5
      6    private Singleton() { //... }
      7
      8    public static Singleton getInstance() {
      9        if (instance == null) {
      10            synchronized(Singleton.class) {
      11                if (instance == null) {
      12                    instance = new Singleton();
      13                }
      14            }
      15        }
      16        return instance;
      17    }
      18
      19    // other useful methods
      20}

© HyunJinNo. Some rights reserved.