Design Pattern - ruthetum/study GitHub Wiki

๋””์ž์ธ ํŒจํ„ด

  • ํŠน์ • ๋ฌธ๋งฅ์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์— ๋Œ€ํ•ด ์“ฐ์ด๋Š” ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ํ•ด๊ฒฐ์ฑ…
  • ๋ชฉ์ ๋ณ„๋กœ ์ผ์ •ํ•œ ํŒจํ„ด์ด ์ œ์‹œ๋˜์–ด ์žˆ์Œ
  • ์™„์ „ํ•œ ์ •๋‹ต์ด ๋˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๋‹ฌ๋ฆฌ ํ˜„์žฌ ์ƒํ™ฉ์— ๋งž์ถฐ ์ตœ์ ํ™”๋œ ํŒจํ„ด์„ ๊ฒฐ์ •ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Œ
  • ๋Œ€ํ‘œ์ ์œผ๋กœ ๊ตฌ์ฒดํ™”๋œ ๋””์ž์ธ ํŒจํ„ด์€ GoF(Gang of Four)์—์„œ ์ œ์‹œํ•œ 23๊ฐœ์˜ ํŒจํ„ด์ด ์žˆ์Œ

๋””์ž์ธ ํŒจํ„ด์˜ ์žฅ์ 

  • ๊ฐœ๋ฐœ์ž ๊ฐ„์˜ ์›ํ™œํ•œ ํ˜‘์—…์ด ๊ฐ€๋Šฅ
  • ์†Œํ”„ํŠธ์›จ์–ด์˜ ๊ตฌ์กฐ๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ ์šฉ์ดํ•จ
  • ์žฌ์‚ฌ์šฉ์„ ํ†ตํ•ด ๊ฐœ๋ฐœ ์‹œ๊ฐ„ ๋‹จ์ถ•
  • ์„ค๊ณ„ ๋ณ€๊ฒฝ์ด ์žˆ์„ ๊ฒฝ์šฐ ๋น„๊ต์  ์›ํ™œํ•˜๊ฒŒ ์กฐ์น˜๊ฐ€ ๊ฐ€๋Šฅ

๋””์ž์ธ ํŒจํ„ด์˜ ๋‹จ์ 

  • ๊ฐ์ฒด์ง€ํ–ฅ์  ์„ค๊ณ„๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ์ง„ํ–‰ํ•ด์•ผ ํ•จ
  • ์ดˆ๊ธฐ ํˆฌ์ž ๋น„์šฉ์ด ๋งŽ์ด ๋“ค์–ด๊ฐ

GoF ๋””์ž์ธ ํŒจํ„ด

๋ชฉ์ ์— ๋”ฐ๋ฅธ ๋ถ„๋ฅ˜

  • ์ƒ์„ฑ ํŒจํ„ด, ๊ตฌ์กฐ ํŒจํ„ด, ํ–‰๋™ ํŒจํ„ด ์ด 3๊ฐ€์ง€๋กœ ๊ตฌ๋ถ„๋จ
  • ๊ฐ ํŒจํ„ด์ด ์–ด๋–ค ์ž‘์—…์„ ์œ„ํ•ด ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์ธ์ง€์— ๋”ฐ๋ฅธ ๊ตฌ๋ถ„
์ƒ์„ฑ ํŒจํ„ด ๊ตฌ์กฐ ํŒจํ„ด ํ–‰๋™ ํŒจํ„ด
Abstract Factory
Builder
Factory Method
Prototype
Singleton
Adapter
Bridge
Composite
Decorator
Facade
Flyweight
Proxy
Chain of Responsibility
Command
Interpreter
Iterator
Mediator
Memento
Observer
State
Stratergy
Template Method
Visitor

์ƒ์„ฑ ํŒจํ„ด

  • ์ƒ์„ฑ ํŒจํ„ด์€ ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ๊ด€๋ จ๋œ ํŒจํ„ด
  • ํŠน์ • ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝ๋˜์–ด๋„ ํ”„๋กœ๊ทธ๋žจ ๊ตฌ์กฐ์— ์˜ํ–ฅ์„ ์ตœ์†Œํ™”ํ•  ์ˆ˜ ์žˆ๋„๋ก ์œ ์—ฐ์„ฑ ์ œ๊ณต
์ƒ์„ฑ ํŒจํ„ด ์˜๋„
์ถ”์ƒ ํŒฉํ† ๋ฆฌ(Abstract Factory) ๊ตฌ์ฒด์ ์ธ ํด๋ž˜์Šค๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ์—ฐ๊ด€๋˜๋Š” ๊ฐ์ฒด๋“ค์„ ๋ณด์—ฌ์คŒ
๋นŒ๋”(Builder) ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ํ‘œํ˜„์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ(Factory Method) ๊ฐ์ฒด ์ƒ์„ฑ์„ ์„œ๋ธŒํด๋ž˜์Šค๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์œ„์ž„(์บก์Šํ™”)
ํ”„๋กœํ† ํƒ€์ž…(Prototype) ์›๋ณธ ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ(ํด๋ก )
์‹ฑ๊ธ€ํ†ค(Singleton) ํ•œ ํด๋ž˜์Šค๋งˆ๋‹ค ์ธ์Šคํ„ด์Šค๋ฅผ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑํ•˜์—ฌ ์–ด๋””์„œ๋“  ์ฐธ์กฐ

๊ตฌ์กฐ ํŒจํ„ด

  • ๊ตฌ์กฐ ํŒจํ„ด์€ ํ”„๋กœ๊ทธ๋žจ ๋‚ด ์ž๋ฃŒ ๊ตฌ์กฐ๋‚˜ ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์กฐ ๋“ฑ ํ”„๋กœ๊ทธ๋žจ ๊ตฌ์กฐ๋ฅผ ์„ค๊ณ„ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ํŒจํ„ด
  • ํด๋ž˜์Šค๋‚˜ ๊ฐ์ฒด๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ๋” ํฐ ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์คŒ
๊ตฌ์กฐ ํŒจํ„ด ์˜๋„
์–ด๋Œ‘ํ„ฐ(Adapter) ํด๋ž˜์Šค์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์–ด๋–ค ํด๋ž˜์Šค์—์„œ๋“  ์ด์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ณ€ํ™˜
๋ธŒ๋ฆฌ์ง€(Bridge) ๊ตฌํ˜„๋ถ€์—์„œ ์ถ”์ƒ์ธต์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ฐ์ž ๋…๋ฆฝ์ ์œผ๋กœ ๋ณ€ํ˜•ํ•˜๊ณ  ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ
์ปดํฌ์ง€ํŠธ(Composite) ๊ฐ์ฒด๋“ค์˜ ๊ด€๊ณ„๋ฅผ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ณตํ•ฉ ๊ฐ์ฒด์™€ ๋‹จ์ผ ๊ฐ์ฒด๋ฅผ ๊ตฌ๋ถ„์—†์ด ๋‹ค๋ฃธ
๋ฐ์ฝ”๋ ˆ์ดํ„ฐ(Decorator) ์ฃผ์–ด์ง„ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๊ฐ์ฒด์— ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๋ง๋ถ™์ž„
ํŒŒ์‚ฌ๋“œ(Facade) ์„œ๋ธŒ ์‹œ์Šคํ…œ์— ์žˆ๋Š” ์ธํ„ฐํŽ˜์ด์Šค ์ง‘ํ•ฉ์— ๋Œ€ํ•ด ํ†ตํ•ฉ๋œ ์ธํ„ฐํŽ˜์ด์Šค ์ œ๊ณต
ํ”Œ๋ผ์ด์›จ์ดํŠธ(Flyweight) ํฌ๊ธฐ๊ฐ€ ์ž‘์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ์ฒด๋ฅผ ๋งค๋ฒˆ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์ตœ๋Œ€ํ•œ ๊ณต์œ ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋„๋ก ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ
ํ”„๋ก์‹œ(Proxy) ์‹ค์ œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฐ์ฒด ๋Œ€์‹  ๊ฐ€์ƒ์˜ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด ๋กœ์ง์˜ ํ๋ฆ„์„ ์ œ์–ด

ํ–‰๋™(ํ–‰์œ„ ํŒจํ„ด)

  • ํ–‰๋™ ํŒจํ„ด์€ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ์ฒด๋“ค์˜ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์„ ํŒจํ„ดํ™”
  • ๊ฐ์ฒด ์‚ฌ์ด์— ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋˜๋Š” ์ฑ…์ž„์„ ๋ถ„๋ฐฐํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ •์˜๋จ
  • ๊ฒฐํ•ฉ๋„๋ฅผ ์ตœ์†Œํ™”ํ•˜๋Š” ๊ฒƒ์ด ์ฃผ ๋ชฉ์ 
ํ–‰๋™ ํŒจํ„ด ์˜๋„
์ฑ…์ž„ ์—ฐ์‡„(Chain of Responsibility) ์š”์ฒญ์„ ๋ฐ›๋Š” ๊ฐ์ฒด๋ฅผ ์—ฐ์‡„์ ์œผ๋กœ ๋ฌถ์–ด ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€ ๊ฐ์ฒด Chain์„ ๋”ฐ๋ผ ์š”์ฒญ์„ ์ „๋‹ฌ
์ปค๋งจ๋“œ(Command) ์š”์ฒญ์„ ๊ฐ์ฒด์˜ ํ˜•ํƒœ๋กœ ์บก์Šํ™”ํ•˜์—ฌ ์žฌ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์ทจ์†Œ
์ธํ„ฐํ”„๋ฆฌํ„ฐ(Interpreter) ํŠน์ • ์–ธ์–ด์˜ ๋ฌธ๋ฒ• ํ‘œํ˜„์„ ์ •์˜
๋ฐ˜๋ณต์ž(Iterator) ์ปฌ๋ ‰์…˜ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•์„ ๋…ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ๋ชจ๋“  ํ•ญ๋ชฉ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณต
์ค‘์žฌ์ž(Mediator) ํ•œ ์ง‘ํ•ฉ์— ์†ํ•ด์žˆ๋Š” ๊ฐ์ฒด๋“ค์˜ ์ƒํ˜ธ์ž‘์šฉ์„ ์บก์Šํ™”ํ•˜์—ฌ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋กœ ์ •์˜
๋ฉ”๋ฉ˜ํ† (Memento) ๊ฐ์ฒด๊ฐ€ ํŠน์ • ์ƒํƒœ๋กœ ๋‹ค์‹œ ๋˜๋Œ์•„์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ๋‚ด๋ถ€ ์ƒํƒœ๋ฅผ ์‹ค์ œํ™”
์˜ต์ €๋ฒ„(Observer) ๊ฐ์ฒด ์ƒํƒœ๊ฐ€ ๋ณ€ํ•  ๋•Œ ๊ด€๋ จ ๊ฐ์ฒด๋“ค์ด ๊ทธ ๋ณ€ํ™”๋ฅผ ์ „๋‹ฌ๋ฐ›์•„ ์ž๋™์œผ๋กœ ์บ์‹ฑ
์ƒํƒœ(State) ๊ฐ์ฒด์˜ ์ƒํƒœ์— ๋”ฐ๋ผ ๋™์ผํ•œ ๋™์ž‘์„ ๋‹ค๋ฅด๊ฒŒ ์ฒ˜๋ฆฌ
์ „๋žต(Stratergy) ๋™์ผ ๊ณ„์—ด์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ตฐ์„ ์ •์˜ํ•˜๊ณ  ์บก์Šํ™”ํ•˜์—ฌ ์ƒํ˜ธ ๊ตํ™˜์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•จ
ํ…œํ”Œ๋ฆฟ ๋ฉ”์„œ๋“œ(Template Method) ์ƒ์œ„ ํด๋ž˜์Šค๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ณจ๊ฒฉ๋งŒ์„ ์ž‘์„ฑํ•˜๊ณ  ๊ตฌ์ฒด์ ์ธ ์ฒ˜๋ฆฌ๋ฅผ ์„œ๋ธŒ ํด๋ž˜์Šค๋กœ ์œ„์ž„
๋ฐฉ๋ฌธ์ž(Visitor) ๊ฐ์ฒด์˜ ์›์†Œ์— ๋Œ€ํ•ด ์ˆ˜ํ–‰ํ•  ์—ฐ์‚ฐ์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๋ณ„๋„์˜ ํด๋ž˜์Šค๋กœ ๊ตฌ์„ฑ
โš ๏ธ **GitHub.com Fallback** โš ๏ธ