GoF์˜ ๋””์ž์ธ ํŒจํ„ด, ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ ํŒจํ„ด์— ๋Œ€ํ•ด ์•Œ์•„๋ณธ๋‹ค.

ํ•ด๋‹น ๊ธ€์€, ๋‹ค์Œ์˜ ์ฝ”๋“œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

ํ•ต์‹ฌ ์š”์•ฝ

  • ๊ฐ์ฒด ์ƒ์„ฑ์„ ์œ„ํ•œ ํŒจํ„ด
  • ๊ฐ์ฒด ์ƒ์„ฑ์— ํ•„์š”ํ•œ ๊ณผ์ •์„ ํ…œํ”Œ๋ฆฟ ์ฒ˜๋Ÿผ ์ •ํ•ด๋†“๊ณ , ๊ฐ ๊ตฌํ˜„์„ ๋”ฐ๋กœํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋‹ค์–‘์„ฑ์„ ํ™•๋ณด
  • ๊ตฌ์ฒด์ ์œผ๋กœ ์ƒ์„ฑํ•  ํด๋ž˜์Šค๋ฅผ ์œ ์—ฐํ•˜๊ฒŒ ์ •ํ•  ์ˆ˜ ์žˆ์Œ
  • ์ƒ์„ฑ์— ์žˆ์–ด ํ•„์š”ํ•œ ์ธํ„ฐํŽ˜์ด์Šค์™€ ๊ตฌํ˜„์„ ๋ถ„๋ฆฌํ•จ
    • ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๊ตฌํ˜„์„ ์ „ํ˜€ ๋ชฐ๋ผ๋„ ๋จ
    • Package(framwork) ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„์–ด ๋ถ„๋ฆฌ ํ›„ ๊ฐœ๋ฐœ๊ฐ€๋Šฅ
    • ํ˜‘์—…์˜ ํšจ์œจ ์ฆ๋Œ€
    • ์‹œ์Šคํ…œ ๊ธฐ๋Šฅ ํ™•์žฅ์—๋„ ์šฉ์ด(interface ์ฑ„ํƒํ›„ ๊ตฌํ˜„)
  • Virtual Constructor(๊ฐ€์ƒ ์ƒ์„ฑ์ž)๋ผ๊ณ ๋„ ๋ถˆ๋ฆฐ๋‹ค.

์˜ˆ์‹œ

  • Factory: Object ์ƒ์„ฑ์— ์žˆ์–ด ํ•„์š”ํ•œ ์ ˆ์ฐจ๋ฅผ ์ •ํ•ด์คŒ
  • Object: ์ƒ์„ฑ๋  Object๋ฅผ ๋™์ผํ•œ ํƒ€์ž…์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ
  • WeaponFactory: ๊ตฌํ˜„์ฒด, ๊ฐ ๋ฌด๊ธฐ์˜ ์ƒ์„ฑ ๊ฐœ์ˆ˜์˜ ์ œ์•ฝ์„ ์ฒ˜๋ฆฌํ•จ
  • Sword, Shield, bow: ๊ตฌํ˜„์ฒด
  • ์ฝ”๋“œ

ํ™œ์šฉ์„ฑ

  • ์–ด๋–ค ํด๋ž˜์Šค๊ฐ€ ์ž์‹ ์ด ์ƒ์„ฑํ•ด์•ผ ํ•˜๋Š” ๊ฐ์ฒด์˜ ํด๋ž˜์Šค๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์—†์„ ๋•Œ
    • Weapon๋Š” Weapon์ธ๋ฐ ์–ด๋–ค Weapon๋ฅผ ์ƒ์„ฑํ•˜๋Š”์ง€์— ๋Œ€ํ•ด์„œ๋Š” ๋ชจ๋ฅด๊ฑฐ๋‚˜ ๋ชฐ๋ผ๋„ ๋˜๋Š” ๊ฒฝ์šฐ
  • ์ƒ์„ฑํ•  ๊ฐ์ฒด๋ฅผ ๊ธฐ์ˆ ํ•˜๋Š” ์ฑ…์ž„์„ ์„œ๋ธŒํด๋ž˜์Šค๊ฐ€ ์ง€์ •ํ–ˆ์œผ๋ฉด ํ•  ๋•Œ
    • ์‹ค์ œ๋กœ Weapon์— ๋Œ€ํ•œ ์ฑ…์ž„์€ ๊ตฌํ˜„์ฒด๊ฐ€ ๋‹ด๋‹นํ•จ
  • ๊ฐ์ฒด ์ƒ์„ฑ์˜ ์ฑ…์ž„์„ ๋ช‡ ๊ฐœ์˜ ๋ณด์กฐ ์„œ๋ธŒํด๋ž˜์Šค ๊ฐ€์šด๋ฐ ํ•˜๋‚˜์—๊ฒŒ ์œ„์ž„ํ•˜๊ณ ,
    • ์ด์–˜๊ธฐ๋Š” ์ƒ์„ฑ์˜ ์ฑ…์ž„์„ ์„œ๋ธŒํด๋ž˜์Šค์— ์ฃผ๊ฒ ๋‹ค๋Š” ๊ฒƒ
  • ์–ด๋–ค ์„œ๋ธŒํด๋ž˜์Šค๊ฐ€ ์œ„์ž„์ž์ธ์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ตญ์†Œํ™”์‹œํ‚ค๊ณ  ์‹ถ์„ ๋•Œ
    • ์ƒ์„ฑ์„ ๋‹ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ด€๋ จ ์ฝ”๋“œ๋ฅผ ์‘์ง‘๋„ ๋†’๊ฒŒ ๊ด€๋ฆฌํ•˜๊ณ  ์‹ถ๋‹ค๋Š” ๊ฒƒ
    • ๋งŒ์•ฝ ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์ด ์—†๋‹ค๋ฉด ์ƒ์„ฑ์— ๊ด€ํ•œ ์ฝ”๋“œ๋Š” ์—ฌ๊ธฐ์ €๊ธฐ ํฉ์–ด์ ธ ์žˆ์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ

์ฐธ์—ฌ์ž

  • Product(Object)
    • ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๊ฐ€ ์ƒ์„ฑํ•˜๋Š” ๊ฐ์ฒด์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜
  • ConcreteProduct(Sword, Shield, Bow)
    • Product ์ถ”์ƒ ํด๋ž˜์Šค์— ์ •์˜๋œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„
  • Creator(Factory)
    • Product ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธ
    • ์™ธ๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ณตํ†ต ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ๋Š” ๊ธฐ๋ณธ ๊ตฌํ˜„์ด ๋“ค์–ด๊ฐ(create(with name: String))
  • ConcreteCreator(WeaponFactory): ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„

ํ˜‘๋ ฅ ๋ฐฉ๋ฒ•

  • Creator๋Š” ์ž์‹ ์˜ subclass๋ฅผ ํ†ตํ•ด ์‹ค์ œ ํ•„์š”ํ•œ ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ , ์ ์ ˆํ•œ ConcreteProduct ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•จ

๊ฒฐ๊ณผ

์žฅ์ 

  1. ์ธํ„ฐํŽ˜์ด์Šค์™€ ๊ตฌํ˜„์˜ ๋ถ„๋ฆฌ๋กœ ์œ ์—ฐ์„ฑ ํ™•๋ณด
    • ์ •์˜ํ•œ ์–ด๋– ํ•œ ConcreteProduct ํด๋ž˜์Šค์™€๋„ ๋™์ž‘ ๊ฐ€๋Šฅ
  2. ์„œ๋ธŒ ํด๋ž˜์Šค(๊ตฌํ˜„์ฒด)์— ๋Œ€ํ•œ hook ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณต
    • ์ฆ‰, ์žฌ์ •์˜๋ฅผ ํ†ตํ•ด ์‘์šฉ์„ฑ์ด ๋†’์•„์ง์„ ์˜๋ฏธ
    • Hook: ์ถ”์ƒ ํด๋ž˜์Šค์— ๋“ค์–ด์žˆ๋Š” ๊ธฐ๋ณธ ํ–‰๋™์„ ์ •์˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ, ์žฌ์ •์˜ ๊ฐ€๋Šฅ
  3. ๋ณ‘๋ ฌ์ ์ธ ํด๋ž˜์Šค ๊ณ„ํ†ต์„ ์—ฐ๊ฒฐํ•˜๋Š” ์—ญํ• ์„ ๋‹ด๋‹น
    • ๋ณ‘๋ ฌ์  ํด๋ž˜์Šค ๊ณ„ํ†ต: ํด๋ž˜์Šค๊ฐ€ ์ž์‹ ์˜ ์ฑ…์ž„์„ ๋ถ„๋ฆฌ๋œ ๋‹ค๋ฅธ ํด๋ž˜์Šค์— ์œ„์ž„ํ•  ๋•Œ ๋ฐœ์ƒ
    • ์ฆ‰, ์ƒ์„ฑ์˜ ์ฑ…์ž„์„ ๋ถ„๋ฆฌํ•˜๋Š” ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋Š” ๋ณ‘๋ ฌ์  ํด๋ž˜์Šค ๊ณ„ํ†ต์ด ๋ฐœ์ƒํ•œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Œ. ๊ทธ๋ฆฌ๊ณ  ์ด ๋•Œ ์œ ์šฉํ•จ
    • ๋‹จ์ ์ธ ์˜ˆ๋กœ ํ‚ค๋…ธํŠธ์™€ ๊ฐ™์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์„ , ๊ธ€์”จ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ์ƒํ™ฉ์„ ์ƒ๊ฐํ•ด๋ณด๊ฒ ์Œ
    • ๊ฐ๊ฐ์˜ Figure(์„ , ๊ธ€์”จ)์— ๋Œ€ํ•œ ๋งˆ์šฐ์Šค ์กฐ์ž‘(๋”๋ธ” ํด๋ฆญ, ๋“œ๋ž˜๊ทธ, ์šฐํด๋ฆญ ๋“ฑ)์€ ์ •ํ•ด์ ธ์žˆ์œผ๋‚˜, ๊ทธ์— ๋Œ€ํ•œ ๋™์ž‘์€ ๋‹ค๋ฅผ ๊ฒƒ์ž„
    • ์ด๋Ÿฐ ๊ฒฝ์šฐ, ๊ฐ Figure์— ๋Œ€ํ•œ Manipulator๋ฅผ ์Œ์œผ๋กœ ๋‘๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์ง ํ•จ
    • ์ด ๋•Œ, ์ด Manipulator๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ฑ…์ž„์„ ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.
    • ๊ทธ๋ ‡๋‹ค๋ฉด Figure์˜ ํด๋ž˜์Šค ๊ณ„ํ†ต์ด ์ปค์ง€๋ฉด Manipulator์˜ ํด๋ž˜์Šค ๊ณ„ํ†ต ์—ญ์‹œ ์ปค์ง€๋Š”, ๋ณ‘๋ ฌ์  ํด๋ž˜์Šค ๊ณ„ํ†ต์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋จ
    • ์ด ๋•Œ, Manipulator์— ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์ ์šฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ์‚ฌ์ด๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ์—ญํ• ์„ ๋‹ด๋‹นํ•˜๊ฒŒ ๋จ

๋‹จ์ 

  1. ConcreteProduct๊ฐ€ ํ•œ๊ฐœ ๋ฐ–์— ์—†๋Š” ํŒฉํ† ๋ฆฌ์ธ๋ฐ Creator๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํŒฉํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š”๊ฐ€?

๊ด€๋ จ ํŒจํ„ด๊ณผ ์ฐจ์ด์ 

  • 03. Abstract Factory ํŒจํ„ด์€ ์ด ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๊ตฌํ˜„ํ•  ๋•Œ๊ฐ€ ๋งŽ์Œ
  • ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋Š” 25. Template Method ํŒจํ„ด์—์„œ๋„ ์‚ฌ์šฉ๋  ๋•Œ๊ฐ€ ๋งŽ์Œ

Reference