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

ํ™์ •์™„ยท2021๋…„ 7์›” 14์ผ
0

Design pattern

๋ชฉ๋ก ๋ณด๊ธฐ
2/7
post-thumbnail

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;
  }
}
profile
์Šต๊ด€์ด ์ „๋ถ€๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€