GoF์˜ ๋””์ž์ธ ํŒจํ„ด, ์žฅ์‹์ž ํŒจํ„ด์— ๋Œ€ํ•ด ์•Œ์•„๋ณธ๋‹ค.

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

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

  • ๊ธฐ๋Šฅ์„ ๋งˆ์น˜ ์žฅ์‹ ์ฒ˜๋Ÿผ ๊ณ„์† ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ํŒจํ„ด
  • ๊ธฐ๋Šฅ์„ ๋Ÿฐํƒ€์ž„์— ๋™์ ์œผ๋กœ ๋ณ€๊ฒฝ ๋˜๋Š” ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋Š” ํŒจํ„ด
  • ์ฃผ์–ด์ง„ ์ƒํ™ฉ ๋ฐ ์šฉ๋„์— ๋”ฐ๋ผ ์–ด๋–ค ๊ฐ์ฒด์— ์ฑ…์ž„์„ ๋ง๋ถ™์ด๋Š” ํŒจํ„ด
  • ๊ธฐ๋Šฅ ํ™•์žฅ์ด ํ•„์š”ํ•  ๋•Œ ์„œ๋ธŒํด๋ž˜์‹ฑ ๋Œ€์‹  ์“ธ ์ˆ˜ ์žˆ๋Š” ์œ ์—ฐํ•œ ๋Œ€์•ˆ์ด ๋  ์ˆ˜ ์žˆ์Œ
  • ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ์— ์ถ”๊ฐ€์ ์ธ ์žฅ๋น„๋ฅผ ์ž…ํž๋•Œ๋ฅผ ์ƒ๊ฐํ•ด๋ณด๋ฉด ์ข‹์Œ (๋Ÿฐํƒ€์ž„์— ์บ๋ฆญํ„ฐ ์™ธ๊ด€, ๋Šฅ๋ ฅ์ด ๋ณ€๊ฒฝ๋จ == ๊ธฐ๋Šฅํ™•์žฅ)

์˜ˆ์‹œ

  • Strings: ์žฅ์‹ํ•  ๋Œ€์ƒ์ด ๋˜๋Š” class. ๋ฌธ์ž์—ด์„ ์—ฌ๋Ÿฌ๊ฐœ ๊ฐ€์ง€๊ณ  ์žˆ์Œ
  • Decorator: ์žฅ์‹์„ ํ•˜๋Š” interface
  • ์—ฌ๊ธฐ์„œ ๋ณด๋ฉด, Strings์™€ Decorator๊ฐ€ ๊ฐ™์€ Item interface๋ฅผ ์ƒ์†๋ฐ›๋„๋ก ํ•˜์—ฌ
  • ์„œ๋กœ ๋‹ค๋ฅธ ์˜๋„๋ฅผ ๊ฐ€์ง„ ๋‘ ํด๋ž˜์Šค๋ฅผ ํ•˜๋‚˜์˜ ๊ฐœ๋…์œผ๋กœ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.
  • ์ฆ‰, ์›๋ž˜์˜ ๋‚ด์šฉ๋ฌผ(String)๊ณผ ์žฅ์‹(Decorator)์— ๋Œ€ํ•œ ๊ตฌ๋ถ„์„ ์—†์•  ๋™์ผํ•˜๊ฒŒ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฒฐ๊ณผ์ ์œผ๋กœ ์žฅ์‹์— ์žฅ์‹์„ ๊ณ„์†ํ•ด์„œ ๋”ํ•˜๋Š” ํšจ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
  • ์˜ˆ์ œ ์ฝ”๋“œ
// Strings
HI H.I.~!
I'm Wansook kun~!
I'm iOS Developer!
Design Pattern is not so good as much as before!
// Side Decorator
"HI H.I.~!"
"I'm Wansook kun~!"
"I'm iOS Developer!"
"Design Pattern is not so good as much as before!"
// Line Number Decorator
00: HI H.I.~!
01: I'm Wansook kun~!
02: I'm iOS Developer!
03: Design Pattern is not so good as much as before!
// Box Number Decorator
+------------------------------------------------+
|HI H.I.~!                                       |
|I'm Wansook kun~!                               |
|I'm iOS Developer!                              |
|Design Pattern is not so good as much as before!|
+------------------------------------------------+
internal func main() {
    let strings = Strings()
 
    strings.add(item: "HI H.I.~!")
    strings.add(item: "I'm Wansook kun~!")
    strings.add(item: "I'm iOS Developer!")
    strings.add(item: "Design Pattern is not so good as much as before!")
 
    var decorator: Item = SideDecorator(item: strings, side: "\"")
    decorator = LineNumberDecorator(item: decorator)
    decorator = BoxDecorator(item: decorator)
    decorator.printContent()
}
 
// Mix
+------------------------------------------------------+
|00: "HI H.I.~!"                                       |
|01: "I'm Wansook kun~!"                               |
|02: "I'm iOS Developer!"                              |
|03: "Design Pattern is not so good as much as before!"|
+------------------------------------------------------+

๋™๊ธฐ

  • ๋‚ด์šฉ์€ ๋™์ผํ•œ ์ƒํƒœ์—์„œ ์žฅ์‹์ด ๊ณ„์†ํ•ด์„œ ๋‹ฌ๋ผ๋ถ™์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ
    • ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ์— ์žฅ๋น„ ๋‹ฌ๊ธฐ
  • ๋™์ ์œผ๋กœ ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ
  • ์ƒ์†์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ธฐ๋Šฅํ™•์žฅ์„ ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ

ํ™œ์šฉ์„ฑ

  • ๋™์ ์œผ๋กœ ์ฑ…์ž„ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
  • ๋‹ค๋ฅธ ๊ฐ์ฒด์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๊ณ  ํ™•์žฅ ๊ฐ€๋Šฅ
  • โ€œ์žฅ์‹โ€์ด๋ผ๋Š” ๋ง์— ๋งž๊ฒŒ, ์ œ๊ฑฐ๋˜์–ด๋„ ๋ฌด๋ฐฉํ•œ ์ฑ…์ž„์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉ
  • ์ƒ์†์œผ๋กœ ์„œ๋ธŒํด๋ž˜์‹ฑํ•˜๋Š” ๊ฒƒ์ด ์‹ค์งˆ์ ์ด์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ ์‚ฌ์šฉ

๊ตฌ์กฐ

์ฐธ์—ฌ์ž

  • Component(Item): ๋™์ ์œผ๋กœ ์ถ”๊ฐ€ํ•  ์„œ๋น„์Šค๋ฅผ ๊ฐ€์งˆ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ๊ฐ์ฒด๋“ค์— ๋Œ€ํ•œ ์ธํ„ฐํŽ˜์ด์Šค
  • ConcreteComponent(Strings): ์‹ค์ œ๋กœ ์ •์˜๋˜์–ด์•ผํ•  ํ•„์š”๊ฐ€ ์žˆ๋Š” ํด๋ž˜์Šค
  • Decorator(Decorator): ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ฐธ์กฐ์ž๋ฅผ ๊ด€๋ฆฌํ•˜๋ฉด์„œ Component์— ์ •์˜๋œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ์กฑํ•˜๋„๋ก ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜(?)
  • ConcreteDecorator(BorderDecorator, ScrollDecorator): Component์— ์ ์šฉํ•  ์žฅ์‹์„ ์‹ค์ œ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค

ํ˜‘๋ ฅ ๋ฐฉ๋ฒ•

  • Decorator ๋‚ด๋ถ€์—์„œ ์ž์‹ ์ด ๊ฐ€์ง„ Component(Item)์œผ๋กœ ๋จผ์ € ์š”์ฒญ์„ ์ „๋‹ฌํ•œ๋‹ค.
    • ์ฆ‰, ๋จผ์ € ๋‚ด๋ถ€ ์ปจํ…์ธ ์— ๋Œ€ํ•œ ์ž‘์—…์„ ๋๋‚ด๊ณ  ๋ฐ˜ํ™˜๋ฐ›๋Š” ๊ฒƒ
  • ์ดํ›„ Decorator์—์„œ ์ถ”๊ฐ€ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

๊ฒฐ๊ณผ

์žฅ์ 

  1. ์ƒ์†๋ณด๋‹ค ์„ค๊ณ„์˜ ์œตํ†ต์„ฑ์„ ๊ฐ€์ง„๋‹ค.
  2. ํ•„์š”ํ•  ๋•Œ๋งŒ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ฆ‰, ์ƒ์œ„ ํด๋ž˜์Šค์— ๊ธฐ๋Šฅ์ด ๋ˆ„์ ๋˜๋Š” ๊ฒƒ์„ ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค.
  3. ์žฅ์‹์ž์™€ ๊ทธ ์žฅ์‹์ž์˜ ๊ตฌ์„ฑ์š”์†Œ๊ฐ€ ๋™์ผํ•  ํ•„์š”๋Š” ์—†๋‹ค.
    • ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด,
    • ๊ธฐ๋ณธ ๋‚ด์šฉ๋ฌผ(Strings)์— decorator๊ฐ€ 3๋ฒˆ ์ค‘์ฒฉ ์ ์šฉ๋˜์—ˆ๋‹ค.
    • ์ด ๊ฒฝ์šฐ ๊ฐ๊ฐ์˜ decorator์—๋Š” Item์ด๋ผ๋Š” ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ,
    • ์ด ๊ตฌ์„ฑ์š”์†Œ๋Š” ๋ชจ๋‘ ๋‹ค๋ฅด๋‹ค.
    • ์ƒ์œ„์˜ decorator๋ฅผ Item์œผ๋กœ ๋ฐ›๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
    • ์ฆ‰, ๋‚ด์šฉ๋ฌผ(Strings)์™€ ์‹ค์ œ decorator์˜ item ๋ณ€์ˆ˜์— ๋‹ด๊ธด ๊ฒƒ๊ณผ ๋™์ผํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

๋‹จ์ 

  • ์ž‘์€ ๊ทœ๋ชจ์˜ ๊ฐ์ฒด๋“ค์ด ๋งŽ์ด ์ƒ๊ธด๋‹ค. ์ž์นซํ•˜๋ฉด ์˜คํžˆ๋ ค ์ดํ•ด๊ฐ€ ์–ด๋ ค์›Œ์งˆ ์ˆ˜ ์žˆ๋‹ค.

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

  • ์ ์‘์ž(09. Adapter)
    • ์ธํ„ฐํŽ˜์ด์Šค ๋ณ€๊ฒฝ์— ์‚ฌ์šฉํ–ˆ์—ˆ์Œ
    • ์ด๋…€์„์€ ์ฑ…์ž„๊ณผ ํ–‰๋™์„ ๋ณ€ํ™”์‹œํ‚ด
  • ๋ณตํ•ฉ์ฒด(11. Composite)
    • ํ•˜๋‚˜์˜ ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ๊ฐ€์ง€๋Š” ๋ณตํ•ฉ์ฒด๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
    • ๋‹ค๋งŒ, ๊ฐ์ฒด ํ•ฉ์„ฑ์ด ์•„๋‹ˆ๊ณ  ํ–‰๋™์„ ์ถ”๊ฐ€ํ•˜๋Š”๋ฐ ๋ณด๋‹ค ๋ชฉ์ ์ด ์žˆ๋‹ค.
  • ์ „๋žต(24. Strategy)
    • ์žฅ์‹์ž: ๊ฒ‰๋ชจ์–‘ ๋ณ€๊ฒฝ
    • ์ „๋žต: ๊ฐ์ฒด์˜ ๋‚ด๋ถ€ ๋ณ€ํ™”

Reference