[Design pattern] Singleton ๐Ÿ™†โ€โ™‚๏ธ, (Creational patterns)

3071 ๋‹จ์–ด Design PatternDesign Pattern

Singleton pattern

๋‹จ ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋””์ž์ธ ํŒจํ„ด



์žฅ์ 

  • ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค ๐Ÿ‘‰ ์‚ฌ์šฉ ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„๋ฅผ ๋ฐฉ์ง€

  • ์ „์—ญ ์ธ์Šคํ„ด์Šค ๐Ÿ‘‰ ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋“ค์ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ 


๋‹จ์ 

  • ์‹ฑ๊ธ€ํ†ค ์ธ์Šคํ„ด์Šค๊ฐ€ ๋งŽ์€ ์—ญํ• (๋ฐ์ดํ„ฐ or ๊ธฐ๋Šฅ)์„(๋ฅผ) ๊ณต์œ ํ•  ๊ฒฝ์šฐ,
    SOLID ์›์น™ ์ค‘ SRP, OCP ์›์น™ ์œ„๋ฐฐ

  • ๐Ÿ‘† ์ˆ˜์ •๊ณผ ํ…Œ์ŠคํŠธ๊ฐ€ ์–ด๋ ค์›€

  • ๊ผญ ํ•„์š”ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์ง€์–‘



๊ตฌ์กฐ




Code


public class SingletonPatternEx {
  public static void main(String[] args) {
  
    SingleObject object = SignleObject.getInstace();
  }
}

public class SingleObject {
  private static SingleObject instance = new SingleObject();
  
  private SingleObject(){}
  
  public static SingleObject getInstance() {
    return instance;
  }
}

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ