ITEM 3: Singleton

singleton ์ด๋ž€ ์ธ์Šคํ„ด์Šค๋ฅผ ์˜ค์ง ํ•˜๋‚˜๋งŒ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค์ด๋‹ค. ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ Singleton ๊ฐ์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด mock ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†์–ด ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์–ด๋ ค์›Œ ์งˆ ์ˆ˜ ์žˆ๋‹ค.

Mock ๊ฐ์ฒด๋ž€?

์‹ค์ œ ๊ฐ์ฒด๋ฅผ ๋‹ค์–‘ํ•œ ์กฐ๊ฑด์œผ๋กœ ์ธํ•ด ์ œ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ์–ด๋ ค์šธ ๊ฒฝ์šฐ ๊ฐ€์งœ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์ด๋ฅผ Mock ๊ฐ์ฒด๋ผ ํ•œ๋‹ค.

Mock ๊ฐ์ฒด๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ.

  • ํ…Œ์ŠคํŠธ ์ž‘์„ฑ์„ ์œ„ํ•œ ํ™˜๊ฒฝ ๊ตฌ์ถ•์ด ์–ด๋ ค์šด ๊ฒฝ์šฐ.

  • ํ…Œ์ŠคํŠธ๊ฐ€ ํŠน์ • ๊ฒฝ์šฐ๋‚˜ ์ˆœ๊ฐ„์— ์˜์กด์ ์ธ ๊ฒฝ์šฐ.

  • ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๋Š” ๊ฒฝ์šฐ

public static final ํ•„๋“œ ๋ฐฉ์‹

public class Elvis {
    public static final Elvis INSTANCE = new Elvis();
    private Elvis() { ... }
}

private ์ƒ์„ฑ์ž๋Š” public static final ํ•„๋“œ์ธ Elvis.INSTANCE ๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ๋”ฑ ํ•œ๋ฒˆ ํ˜ธ์ถœ๋œ๋‹ค. public ํ˜น์€ protected ์ƒ์„ฑ์ž๊ฐ€ ์—†์œผ๋ฏ€๋กœ, Elvis ํด๋ž˜์Šค๊ฐ€ ์ดˆ๊ธฐํ™”๋  ๋•Œ ๋งŒ๋“ค์–ด์ง„ ์ธ์Šคํ„ด์Šค๋Š” ํ•˜๋‚˜๋ฟ์ž„์ด ๋ณด์žฅ๋œ๋‹ค.

์žฅ์ 

  1. public ํ•„๋“œ ๋ฐฉ์‹์€ ํ•ด๋‹น ํด๋ž˜์Šค๊ฐ€ ์‹ฑ๊ธ€ํ„ด์ž„์ด API์— ๋ช…๋ฐฑํ•˜๊ฒŒ ๋“œ๋Ÿฌ๋‚œ๋‹ค.(final์ด๋ฏ€๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด ์ฐธ์กฐ ๋ถˆ๊ฐ€)

  2. ๊ฐ„๊ฒฐํ•จ

static factory ๋ฐฉ์‹

public class Elvis {
    private static final Elvis INSTANCE = new Elvis();
    private Elvis() { ... }

    public static Elvis getInstance() { return INSTANCE; }
}

Elvis.getInstance() ๋Š” ํ•ญ์ƒ ๊ฐ™์€ ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•˜๋‚˜์ž„์„ ๋ณด์žฅํ•œ๋‹ค.

์žฅ์ 

  1. ํ˜„์žฌ๋Š” singleton ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ์ •์  ๋ฉ”์„œ๋“œ์ด์ง€๋งŒ, ํ–ฅํ›„์— ํ•„์š”์— ๋”ฐ๋ผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ํ™•์žฅ์„ฑ์ด ์žˆ๋‹ค. ์œ ์ผํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํŒฉํ„ฐ๋ฆฌ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœํ•˜๋Š” ์Šค๋ ˆ๋“œ๋ณ„๋กœ ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค๋ฅผ ๋„˜๊ฒจ์ฃผ๋„๋ก ๋ฆฌํ„ดํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๊ฐ™์ด ํ™•์žฅ์„ฑ์ด ์—ด๋ ค์žˆ๋‹ค.

  2. ์ •์  ํŒฉํ„ฐ๋ฆฌ๋ฅผ ์ œ๋„ค๋ฆญ ์‹ฑํดํ„ด ํŒฉํ„ฐ๋ฆฌ-item30๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

  3. ์ •์  ํŒฉํ„ฐ๋ฆฌ์˜ ๋ฉ”์„œ๋“œ ์ฐธ์กฐ๋ฅผ ๊ณต๊ธ‰์ž(supplier)๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.(item43, item44)

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์žฅ์ ์ด ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค๋ฉด, public static final ํ•„๋“œ ๋ฐฉ์‹์ด ๋” ์ข‹๋‹ค.

Reflection ๋ฐฉ์–ด

์ด๋•Œ, public static final ๋ฐฉ์‹๊ณผ static factory ๋ฐฉ์‹์€ ๊ถŒํ•œ์ด ์žˆ๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ Reflection API - item65 ์ธ AccessibleObject.setAccessible์„ ์‚ฌ์šฉํ•ด private ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ ์ด ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ณต๊ฒฉ์„ ๋ฐฉ์–ดํ•˜๋ ค๋ฉด ๋‘๋ฒˆ ์งธ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋ คํ•  ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค.

public class Elvis {
    public static final Elvis INSTANCE = new Elvis();
    private Elvis() { 
          if( INSTANCE != null) {
            throw new RuntimeException("Can't create Constructor");
        }    
          //... 
    }
}

Singleton Class ์ง๋ ฌํ™”

Singleton class๋ฅผ ์ง๋ ฌํ™”ํ•˜๋ ค๋ฉด ๋‹จ์ˆœํžˆ Serializable์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋Š” ๋ถ€์กฑํ•˜๋‹ค. ๋ชจ๋“  ์ธ์Šคํ„ด์Šค ํ•„๋“œ๋ฅผ transient(์ผ์‹œ์ ) ์•ฝ์–ด๋ฅผ ์„ ์–ธํ•˜๊ณ  readResolve ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•ด์•ผํ•œ๋‹ค. (item 89) ์ด๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š์œผ๋ฉด, ์—ญ์ง๋ ฌํ™”(deserialize)์‹œ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.

public class Elvis implements Serializable{
    private static final Elvis INSTANCE = new Elvis();
    private Elvis() { ... }

      public static Elvis getInstance() { return INSTANCE; }

      // singleton์ž„์„ ๋ณด์žฅ
      private Object readResolve() {
          // ์—ญ์ง๋ ฌํ™”๊ฐ€ ๋˜์–ด ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜๋”๋ผ๋„ INSTANCE๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์‹ฑ๊ธ€ํ„ด ๋ณด์žฅ
           // ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋Š” GC์— ์˜ํ•ด UnReachable ํ˜•ํƒœ๋กœ ํŒ๋ณ„๋˜์–ด ์ œ๊ฑฐ
          return INSTANCE;
    }
}

Enum ๋ฐฉ์‹

public enum Elvis {
      INSTANCE;
}

์›์†Œ๊ฐ€ ํ•˜๋‚˜์ธ Enumํƒ€์ž…์„ ์„ ์–ธํ•ด singleton์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

์žฅ์ 

  1. public static ๋ฐฉ์‹๋ณด๋‹ค ๋” ๊ฐ„๊ฒฐ

  2. ์ถ”๊ฐ€ ์ฝ”๋“œ์—†์ด ์ง๋ ฌํ™” ๊ฐ€๋Šฅ

  3. Reflection ๊ณต๊ฒฉ๊ณผ ์•„์ฃผ ๋ณต์žกํ•œ ์ง๋ ฌํ™” ์ƒํ™ฉ์—๋„ ์ œ 2์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ๊ธฐ๋Š” ์ผ์„ ์™„๋ฒฝํžˆ ๋ฐฉ์–ด

๋Œ€๋ถ€๋ถ„์˜ ์ƒํ™ฉ์—์„œ๋Š” ์›์†Œ๊ฐ€ ํ•˜๋‚˜๋ฟ์ธ ์—ด๊ฑฐ ํƒ€์ž…์ด singleton์„ ๋งŒ๋“œ๋Š” ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋‹ค. ํ•˜์ง€๋งŒ, ๋งŒ๋“ค๋ ค๋Š” singleton์ด Enum ์ด์™ธ์˜ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ ์ด ๋ฐฉ๋ฒ•์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

์ฐธ๊ณ 

Last updated