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

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

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

  • ๋™์ผํ•œ ํŒจํ„ด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ์ˆœํšŒํ•˜๊ธฐ ์œ„ํ•œ ํŒจํ„ด
  • ์ฆ‰, Container/Aggregator ๋ผ ๋ถˆ๋ฆฌ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•ด ์ˆœํšŒ
    • Array, Linked List, Tree, Graph, Table(DBMS)
  • ๊ฐ ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ๋‹ค์Œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์ด ๋‹ฌ๋ผ์ง (๊ตฌํ˜„ ๋ฐฉ์‹์ด ๋‹ค๋ฅด๋‹ˆ)
  • ์ด๋ฅผ ํ•˜๋‚˜์˜ ํŒจํ„ด์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ ๊ฒƒ

์˜ˆ์‹œ

Code

main

 
import Foundation
 
internal func main() {
    let items = [
        Item(name: "Bitcoin", cost: 23000),
        Item(name: "Tesla", cost: 170),
        Item(name: "Jordan", cost: 200),
        Item(name: "CyberTruck", cost: 50000)
    ]
 
    let array = Array(items: items)
    let iterator = array.iterator
 
    while iterator.next {
        guard let current = iterator.current as? Item else {
            break
        }
 
        print(current.description)
    }
 
}
 
main()
 

Object

import Foundation
 
internal protocol Object {
 
}
 

Aggregator

import Foundation
 
internal protocol Aggregator {
 
    var iterator: Iterator { get }
    
}

Iterator

import Foundation
 
internal protocol Iterator {
 
    var next: Bool { get }
    
    var current: Object? { get }
 
}

Loggable

import Foundation
 
internal protocol Loggable {
 
    var description: String { get }
 
}
 

Item

import Foundation
 
internal class Item: Object {
 
    internal init(name: String, cost: Int) {
        self.name = name
        self.cost = cost
    }
 
    private let name: String
    private let cost: Int
    
}

Array

import Foundation
 
internal class Array: Aggregator, Object {
 
    internal init(items: [Item]) {
        self.items = items
    }
 
    internal var iterator: Iterator {
        ArrayIterator(array: self)
    }
 
    internal var count: Int {
        self.items.count
    }
 
    internal func item(at index: Int) -> Item? {
        guard index < self.items.count else {
            return nil
        }
        return self.items[index]
    }
 
    private var items = [Item]()
 
}
 

ArrayIterator

import Foundation
 
internal class ArrayIterator: Iterator {
 
    internal var next: Bool {
        self.index += 1
 
        return self.index < self.array.count
    }
 
    internal var current: Object? {
        self.array.item(at: self.index)
    }
 
 
    internal init(array: Array) {
        self.array = array
        self.index = -1
    }
 
    private var array: Array
    private var index: Int
 
}

ํ™œ์šฉ์„ฑ

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

๊ฒฐ๊ณผ

  1. ์ง‘ํ•ฉ ๊ฐ์ฒด์˜ ๋‹ค์–‘ํ•œ ์ˆœํšŒ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค.
    • ๋‹ค์–‘ํ•œ Iterator๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.
  2. Aggregater์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋‹จ์ˆœํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ƒ๊ฐํ•ด๋ณผ ์ 

  • ๋ฐฐ์—ด, ๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ, ํŠธ๋ฆฌ๋“ค๊ณผ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ํ˜•ํƒœ์˜ ์ปจํ…Œ์ด๋„ˆ์— ๋Œ€ํ•ด
  • ํ‘œ์ค€ํ™”๋œ ๊ณตํ†ต API๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด ํ•ต์‹ฌ
  • ํ•œ๊ฐœ์˜ API๋งŒ์œผ๋กœ๋„ ๋‹ค์–‘ํ•œ ํ˜•ํƒœ์˜ ์ปจํ…Œ์ด๋„ˆ ์•ˆ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

Reference