๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • Dev Blog
CS/๋””์ž์ธ ํŒจํ„ด

[GoF ๋””์ž์ธ ํŒจํ„ด] - ๋””์ž์ธ ํŒจํ„ด์ด๋ž€?

by ์œ ์ง„์˜ 2024. 5. 28.
๋ชจ๋“  ์ฝ”๋“œ๋Š” Swift๋กœ ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

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

๋””์ž์ธ ํŒจํ„ด์ด๋ž€ ์†Œํ”„ํŠธ์›จ์–ด ์„ค๊ณ„์— ์žˆ์–ด์„œ ๋ฐ˜๋ณต์ ์œผ๋กœ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ๋“ค์„ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ผ์ข…์˜ ํ…œํ”Œ๋ฆฟ ํ˜น์€ ๊ฐ€์ด๋“œ๋ผ์ธ์ด๋‹ค. ํ”ํžˆ ์•„ํ‚คํ…์ฒ˜ ํŒจํ„ด๊ณผ ๋งŽ์ด ํ˜ผ๋™ํ•ด์„œ ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๊ณค ํ•œ๋‹ค.

์•„ํ‚คํ…์ฒ˜ ํŒจํ„ด์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ „์ฒด์ ์ธ ๊ตฌ์กฐ์— ์ดˆ์ ์„ ๋งž์ถ˜ ํŒจํ„ด์ด๋ผ๋ฉด ๋””์ž์ธ ํŒจํ„ด์€ ๊ฐ์ฒด๋“ค ๊ฐ„์˜ ๊ด€๊ณ„์— ์ดˆ์ ์„ ๋งž์ถ˜ ํŒจํ„ด์ด๋ผ๋Š” ์ฐจ์ด์ ์ด ์กด์žฌํ•œ๋‹ค.

 

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

ํ”ํžˆ ์†Œํ”„ํŠธ์›จ์–ด ๊ณตํ•™์—์„œ '๋””์ž์ธ ํŒจํ„ด'์€ ๋Œ€๋ถ€๋ถ„ GoF(Gang of Four) ๋””์ž์ธ ํŒจํ„ด์„ ์˜๋ฏธํ•œ๋‹ค. ์ด๋“ค์€ ๊ฐ๊ฐ์˜ ๋ชฉ์ ์— ๋”ฐ๋ผ ์ƒ์„ฑ ํŒจํ„ด, ๊ตฌ์กฐ ํŒจํ„ด, ํ–‰์œ„ ํŒจํ„ด์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜๊ณ  ์ด 23๊ฐœ์˜ ํŒจํ„ด์œผ๋กœ ๊ตฌ์ฒดํ™”ํ•˜์˜€๋‹ค. 

 

Gang of Four

 

์ด๋ ‡๊ฒŒ๋‚˜ ๋งŽ์€ ๋””์ž์ธ ํŒจํ„ด์„ ๊ณต๋ถ€ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๊ฐ€ ๋ฌด์—‡์ผ๊นŒ?

 

์ฒซ์งธ, ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๊ฐ์ฒด ์„ค๊ณ„์— ํ•„์š”ํ•œ 5๊ฐ€์ง€ ์›์น™์ธ SOLID ์›์น™์„ ์ค€์ˆ˜ํ•˜๊ฒŒ ๋˜์–ด ๊ฐ์ฒด ๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถฐ ์ดํ•ด์™€ ์ˆ˜์ •์ด ์‰ฌ์šด ์ฝ”๋“œ๋กœ ์ž‘์„ฑ๋œ ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

๋‘˜์งธ, ์ฝ”๋“œ์˜ ๊ตฌ์กฐ์— ์ด๋ฆ„์„ ๋ถ™ํ˜€์„œ ์†Œํ†ตํ•  ์ˆ˜ ์žˆ๊ธฐ์— ๋ฌธ์ œ ํ•ด๊ฒฐ์— ๋Œ€ํ•œ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์˜ ๋„๊ตฌ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค.

 

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

 

 

UML ํด๋ž˜์Šค ๊ด€๊ณ„ ํ‘œํ˜„

๋””์ž์ธ ํŒจํ„ด์€ ์œ„์—์„œ ๋งํ–ˆ๋‹ค์‹œํ”ผ ๊ฐ์ฒด๋“ค ๊ฐ„์˜ ๊ด€๊ณ„์— ์ดˆ์ ์„ ๋งž์ถ˜ ํŒจํ„ด์ด๊ธฐ์— ๊ฐ์ฒด ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•œ UML ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ์— ๋Œ€ํ•œ ๋ฐฐ๊ฒฝ ์ง€์‹์ด ํ•„์š”ํ•˜๋‹ค.

 

UML ํด๋ž˜์Šค ๊ด€๊ณ„ ํ‘œํ˜„ ์˜ˆ์‹œ(ํ™”์‚ดํ‘œ ๋ชจ์–‘์€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Œ)

 

 

์—ฐ๊ด€ ๊ด€๊ณ„

์—ฐ๊ด€ ๊ด€๊ณ„๋ž€ ํ•œ ๊ฐ์ฒด๊ฐ€ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ๊ด€๊ณ„๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ํ™”์‚ดํ‘œ๊ฐ€ ์กด์žฌํ•˜๋ฉด ํ•œ์ชฝ๋งŒ ์ธ์ง€ํ•˜๊ณ  ์žˆ๋Š” ๊ด€๊ณ„, ํ™”์‚ดํ‘œ๊ฐ€ ์—†๋‹ค๋ฉด ์„œ๋กœ๊ฐ€ ์„œ๋กœ๋ฅผ ์ธ์ง€ํ•˜๊ณ  ์žˆ๋Š” ๊ด€๊ณ„๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

 

UML ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ

 

์ฝ”๋“œ ์˜ˆ์‹œ

class House {}

class Person {
	private var house: House
}

 

 

์˜์กด ๊ด€๊ณ„

์˜์กด ๊ด€๊ณ„๋ž€ ํ•œ ๊ฐ์ฒด๊ฐ€ ๋‹ค๋ฅธ ๊ฐ์ฒด์™€ ์•„์ฃผ ์งง์€ ์‹œ๊ฐ„ ๋™์•ˆ๋งŒ ์—ฐ๊ด€์„ ์œ ์ง€ํ•˜๋Š” ๊ด€๊ณ„๋‹ค. ์—ฐ๊ด€ ๊ด€๊ณ„์ฒ˜๋Ÿผ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํ”„๋กœํผํ‹ฐ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋ฉ”์„œ๋“œ์˜ ๋ฐ˜ํ™˜๊ฐ’์ด๋‚˜ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์–ด '์•„์ฃผ ์งง์€ ์‹œ๊ฐ„ ๋™์•ˆ' ์ด๋ผ๋Š” ์˜๋ฏธ๋Š” ๊ฐ์ฒด์˜ ์ƒ๋ช… ์ฃผ๊ธฐ์— ๋น„ํ•ด ์•„์ฃผ ์งง์€ ๋ฉ”์„œ๋“œ์˜ ์ƒ๋ช… ์ฃผ๊ธฐ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

 

UML ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ

  

์ฝ”๋“œ ์˜ˆ์‹œ

class House {}

class Person {
	func createHouse() -> House {
		return House()
	}

	func useHouse(house: House) {
		let myHouse = house
	}
}

 

 

์ƒ์† ๊ด€๊ณ„์™€ ์‹ค์ฒดํ™” ๊ด€๊ณ„

์ƒ์† ๊ด€๊ณ„์™€ ์‹ค์ฒดํ™” ๊ด€๊ณ„๋Š” ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๋งํ•˜๋Š” ์ƒ์†๊ณผ ์ธํ„ฐํŽ˜์ด์Šค ์ฑ„ํƒ์„ ์˜๋ฏธํ•œ๋‹ค.

 

์ƒ์†
์‹ค์ฒดํ™”

 

์ง‘ํ•ฉ ๊ด€๊ณ„

์ง‘ํ•ฉ ๊ด€๊ณ„๋ž€ ํ•œ ๊ฐ์ฒด๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐ์ฒด๋“ค์„ ์—ฐ๊ด€ํ•˜๊ณ  ์žˆ๋Š” ๊ด€๊ณ„์ด๋‹ค.

 

UML ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ

 

์ฝ”๋“œ ์˜ˆ์‹œ

class Computer {
	private var monitor: Monitor
	private var keyboard: Keyboard
	private var mouse: Mouse

    init(monitor: Monitor, keyboard: Keyboard, mouse: Mouse) {
        self.monitor = monitor
        self.keyboard = keyBoard
        self.mouse = mouse
    }
}

 

 

ํฌํ•จ ๊ด€๊ณ„

ํฌํ•จ ๊ด€๊ณ„๋Š” ์ง‘ํ•ฉ ๊ด€๊ณ„์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•œ ๊ฐ์ฒด๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐ์ฒด๋“ค์„ ์—ฐ๊ด€ํ•˜๊ณ  ์žˆ๋Š” ๊ด€๊ณ„์ด๋‹ค. ์ง‘ํ•ฉ ๊ด€๊ณ„์™€์˜ ์ฐจ์ด์ ์€ ์ง‘ํ•ฉ ๊ด€๊ณ„๋Š” ๋ชธ์ด ์ฃฝ์œผ๋ฉด ์•ก์„ธ์„ธ๋ฆฌ๋Š” ๋‚จ์•„์žˆ๋Š” ๊ด€๊ณ„๋กœ ๋น„์œ ๋˜๊ณ  ํฌํ•จ ๊ด€๊ณ„๋Š” ๋ชธ์ด ์ฃฝ์œผ๋ฉด ์žฅ๊ธฐ๋„ ๊ฐ™์ด ์ฃฝ๋Š” ๊ด€๊ณ„๋กœ ๋น„์œ ๋œ๋‹ค. ์ฆ‰, ์ง‘ํ•ฉ ๊ด€๊ณ„๋Š” ์ดˆ๊ธฐํ™” ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์™ธ๋ถ€์—์„œ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ๋ฐ›์•„์˜ค๋Š” ๊ด€๊ณ„์ด๊ณ , ํฌํ•จ ๊ด€๊ณ„๋Š” ๊ฐ์ฒด๋“ค์„ ๋‚ด๋ถ€์—์„œ ์ƒ์„ฑํ•˜๋Š” ๊ด€๊ณ„์ด๋‹ค.

 

UML ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ

 

์ฝ”๋“œ ์˜ˆ์‹œ

class Computer {
	private var monitor: Monitor
	private var keyboard: Keyboard
	private var mouse: Mouse

    init(monitor: Monitor, keyboard: Keyboard, mouse: Mouse) {
        self.monitor = Monitor()
        self.keyboard = KeyBoard()
        self.mouse = Mouse()
    }
}

๋Œ“๊ธ€