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

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

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

  • ํ”Œ๋ผ์ด๊ธ‰ - ๊ฐ€๋ณ๊ณ  ๋ฏผ์ฒฉํ•œ ๋ณต์‹ฑ ์„ ์ˆ˜์˜ ์ฒด๊ธ‰์„ ์˜๋ฏธ
  • ์–ด๋–ค ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋งค๋ฒˆ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ํ•œ๋ฒˆ๋งŒ ์ƒ์„ฑํ•จ
  • ๋‹ค์‹œ ํ•„์š”ํ•ด์ง€๋Š” ๊ฒฝ์šฐ ์ด์ „์— ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ์žฌ์‚ฌ์šฉ
  • ๊ฐ์ฒด ์ƒ์„ฑ์‹œ ๋งŽ์€ ์ž์›์ด ์†Œ๋ชจ๋˜๋Š” ๊ฒฝ์šฐ ์ ์€ ์ž์›์œผ๋กœ ํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

์˜ˆ์‹œ

  • Digit
    • 0~9 ์˜ ์ˆซ์ž๋ฅผ ํ™”๋ฉด์— ํ‘œ์‹œ
    • ํ™”๋ฉด์— ํ‘œ์‹œ == ํ”ฝ์…€๋กœ ํ™”๋ฉด์— ๊ทธ๋ ค์•ผ ํ•จ
    • ๋งŽ์€ ์–‘์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •
    • ์ด ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ๋Š” 0.txt์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์ €์žฅ๋˜์–ด ์žˆ๋‹ค๊ณ  ํ•˜์ž.
    • ์ƒ์„ฑ์‹œ ํŒŒ์ผ์„ ์ฝ๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด ์ถœ๋ ฅํ•œ๋‹ค.
    • ์ด๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ๋‚ด์šฉ์„ ๋ฉ”๋ชจ๋ฆฌ์— ๋ชจ๋‘ ์ ์žฌํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์—์„œ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ๋งŽ๋‹ค.
  • DigitFactory
    • ๋ฐ”๊นฅ ์ชฝ์—์„œ ์›ํ•˜๋Š” ์ˆซ์ž๋ฅผ ๋„˜๊ธฐ๋ฉด ๋ฐ˜ํ™˜
    • Digit ๊ฐ์ฒด๋ฅผ ๋ฏธ๋ฆฌ ์ƒ์„ฑํ•ด๋‘์ง€ ์•Š๊ณ , ์ฒ˜์Œ ์š”์ฒญ ๋ฐ›์„์‹œ ํ•œ๋ฒˆ๋งŒ ์ƒ์„ฑ
    • ๋™์ผํ•œ ์š”์ฒญ์„ ๋ฐ›์œผ๋ฉด ๊ทธ๋Œ€๋กœ ๋ฆฌํ„ด
  • Number
    • ๊ธด ์ˆซ์ž๊ฐ’์„ ํ™”๋ฉด์— ์ถœ๋ ฅ

์ถ”๊ฐ€ ์˜ˆ์‹œ

  • Number class๋ฅผ ์ƒ์„ฑ์‹œ 434331์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์ƒ์„ฑํ•˜๋ผ๊ณ  ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž.
  • ๊ทธ๋ ‡๋‹ค๋ฉด ๊ฐ ์ˆซ์ž์— ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž๋ฅผ ํ™”๋ฉด์— ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด Digit ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
  • ๊ทธ๋ ‡๋‹ค๋ฉด 4, 3๊ณผ ๊ฐ™์ด ๋ฐ˜๋ณต๋˜๋Š” ์ˆซ์ž์— ๋Œ€ํ•ด์„œ ๊ฐ๊ฐ์— ํ•ด๋‹นํ•˜๋Š” Digit ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ ธ์•ผํ• ๊นŒ?
  • ์ด๋Ÿด ๊ฒฝ์šฐ Flyweight ํŒจํ„ด์„ ์ด์šฉํ•˜์—ฌ ์ด๋ฏธ ์ƒ์„ฑ๋œ ๊ฐ์ฒด์ค‘ 1, 3, 4 ๊ฐ์ฒด 3๊ฐœ๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

์ฝ”๋“œ

Main

//
//  main.swift
//  Flyweight
//
//  Created by Choiwansik on 2023/01/02.
//
 
import Foundation
 
internal func main() {
    let number = Number(integer: 434331)
    number.print()
}
 
main()
 

Number

//
//  Number.swift
//  Flyweight
//
//  Created by Choiwansik on 2023/01/02.
//
 
import Foundation
 
internal class Number {
 
    internal init(integer: Int) {
        let string = String(integer)
 
        let factory = DigitFactory()
 
        (0..<string.count)
            .map { (offset: Int) -> String in
                let index = string.index(string.startIndex, offsetBy: offset)
                return String(string[index])
            }
            .compactMap { Int($0) }
            .forEach {
                self.digitList.append(factory.digit(of: $0))
            }
    }
 
    internal func print() {
        self.digitList.forEach {
            $0.print()
        }
    }
 
    private var digitList = [Digit]()
}
 

DigitFactory

//
//  DigitFactory.swift
//  Flyweight
//
//  Created by Choiwansik on 2023/01/02.
//
 
import Foundation
 
internal class DigitFactory {
 
    internal func digit(of integer: Int) -> Digit {
        // 0-9 ์ด์™ธ์˜ ๊ฐ’์ด ๋“ค์–ด์˜ค๋Š” ๊ฒฝ์šฐ๋Š” ์‹œ๊ฐ„์ƒ ์ œ์™ธํ•จ
 
        if let digit = self.pool[integer],
           digit != nil {
            return digit!
        } else {
            let digit = Digit(filename: "\(integer).txt")
            self.pool[integer] = digit
            return digit
        }
    }
 
    private var pool = Dictionary(uniqueKeysWithValues: zip(0...9, [Digit?].init(repeating: nil, count: 10)))
}
 

Digit

//
//  Digit.swift
//  Flyweight
//
//  Created by Choiwansik on 2023/01/02.
//
 
import Foundation
 
internal class Digit {
 
    internal init(filename: String) {
        self.data = TextReader.read(filename: filename)
    }
 
    internal func print() {
        Swift.print(self.data)
    }
 
    private var data = ""
 
}
 

๊ฒฐ๊ณผ

   #
  ##
 # #
#####
   #
 ###
    #
 ###
    #
 ###
   #
  ##
 # #
#####
   #
 ###
    #
 ###
    #
 ###
 ###
    #
 ###
    #
 ###
  #
  #
  #
  #
  #

๋™๊ธฐ

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

ํ™œ์šฉ์„ฑ

  • ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์ด ๋Œ€๋Ÿ‰์˜ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๋•Œ
  • ๊ฐ์ฒด์˜ ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์ ธ ์ €์žฅ ๋น„์šฉ์ด ๋†’์•„์งˆ ๋•Œ
  • ๋Œ€๋ถ€๋ถ„์˜ ๊ฐ์ฒด ์ƒํƒœ๋ฅผ ๋ถ€๊ฐ€์ ์ธ ๊ฒƒ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์„ ๋•Œ
  • ๊ฐ์ฒด์˜ ๋งŽ์€ ๋ถ€๋ถ„์„ ์ ์€ ์ˆ˜์˜ ๊ณต์œ  ๊ฐ์ฒด๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์„ ๋•Œ

๊ตฌ์กฐ

์ฐธ์—ฌ์ž

  • FlyWeight(Digit)
  • FlyWeightFactory(DigitFactory)
  • Number(client)

ํ˜‘๋ ฅ ๋ฐฉ๋ฒ•

  • client๋Š” Flyweight ์ธ์Šคํ„ด์Šค๋ฅผ FlyWeightFactory์— ์š”์ฒญํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

๊ฒฐ๊ณผ

  • ์žฅ์ 
    • ๊ณต์œ  ๊ฐ์ฒด ์‚ฌ์šฉ์„ ํ†ตํ•ด ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ
    • ์ธ์Šคํ„ด์Šค ๊ฐœ์ˆ˜ ์ค„์ผ ์ˆ˜ ์žˆ์Œ
    • ๋ณธ์งˆ์  ์ƒํƒœ(์ˆซ์ž๋Š” 10๊ฐœ - 10๊ฐœ๋งŒ ์žˆ์œผ๋ฉด ๋จ)๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Œ
  • ๋‹จ์ 
    • ๋Ÿฐํƒ€์ž„ ๋น„์šฉ ์ฆ๊ฐ€
    • ๋ถ€๊ฐ€์  ์ƒํƒœ(์ˆซ์ž๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ ์ด์™ธ์˜ ์ƒํƒœ - Number์™€ ๊ฐ™์€ ๊ฒƒ, ์ƒ‰์ƒ ๋“ฑ๋“ฑ)๋ฅผ ์—ฐ์‚ฐํ•ด์•ผ ํ•จ

๊ตฌํ˜„

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

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

  • ๋Šฆ์€ ์ƒ์„ฑ (lazy Initialzation)์€ ์–ธ์–ด ์ฐจ์›์—์„œ ์ง€์›ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ๋ณด๋‹ค ์ข‹๋‹ค๊ณ  ๋Š๊ผˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ์ปดํŒŒ์ผ๋Ÿฌ ๋‹จ์—์„œ ์ตœ์ ํ™”๊ฐ€ ์ผ์–ด๋‚  ์ˆ˜๋„ ์žˆ๋‹ค.
  • ๊ฐ’์ด ์ดˆ๊ธฐ์— ์ƒ์„ฑ๋˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ๋„ฃ๋Š” ๊ฐœ๋… ์ž์ฒด๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๊ตณ์ด ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • dictionary๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ช…ํ™•ํ•˜๊ฒŒ ๋“ค์–ด์˜จ ๊ฒฝ์šฐ์—๋งŒ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.
  • ํ•ด๋‹น ํŒจํ„ด์€ ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ๋Š” ์ดํ•ดํ•˜๋˜, ์–ด๋– ํ•œ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•์— ๋งค๋ชฐ๋˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
  • ์„ฑ๋Šฅ์ด ๋†’์•„์ง„ ๊ฒƒ๋„ ํ•ด๋‹น ํŒจํ„ด์˜ ์ค‘์š”์„ฑ์ด ์ค„์–ด๋“œ๋Š”๋ฐ ์—ญํ• ์„ ํ–ˆ๋‹ค.
  • ๋‹ค๋งŒ, ๊ณต์œ ๋œ ์ƒํƒœ๊ฐ€ ํ•„์š”ํ•  ๊ฒฝ์šฐ ๋ณธ์งˆ์ , ๋ถ€๊ฐ€์ ์ธ ์ƒํƒœ๋ฅผ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์€ ์˜๋ฏธ๊ฐ€ ์žˆ์–ด ๋ณด์ธ๋‹ค.

Reference