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

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

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

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

์˜ˆ์‹œ

  • Class Diagram์ด ์–ด๋ ต๋‹ค๋ฉด ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜์ž.
  • ํ•ด๋‹น ์ฝ”๋“œ๋Š” DB์—์„œ ํŠน์ • ์ด๋ฆ„์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž๋ฃŒ๋ฅผ ์กฐ์‚ฌํ•˜๋Š” ๋™์ž‘์„ ๋ชจ์‚ฌํ•œ ๊ฒƒ์ด๋‹ค.
  • ์ด ๋•Œ, ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์†๋„๋ฅผ ๋†’ํžˆ๋ ค ํ•œ๋‹ค.
  • ์ด ๊ณผ์ •์—์„œ ์บ์‹œ ์กฐํšŒ, DB ์กฐํšŒ, ์บ์‹œ ์‚ฝ์ž…๋“ฑ ์–ด๋– ํ•œ ๋กœ์ง์ด ํ•„์š”ํ•˜๋‹ค.
  • ๋งŒ์•ฝ ์ด ๊ณผ์ •์„ ์‚ฌ์šฉํ•˜๋Š” ์ธก์—์„œ ์กฐํ•ฉํ•˜๋„๋ก ํ•œ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋”๋Ÿฌ์šด ์ฝ”๋“œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
// Not using Facade
 
internal func main() {
    let dbms = DBMS()
    let cache = Cache()
 
    let name = "wansik"
 
 
    if let row = cache.get(with: name) {
 
        let message = Message(row: row)
        print(message.makeName())
        print(message.makeBirthday())
        print(message.makeEmail())
 
    } else if let row = dbms.query(name: name) {
        cache.put(row: row)
 
        let message = Message(row: row)
        print(message.makeName())
        print(message.makeBirthday())
        print(message.makeEmail())
 
    } else {
        print("์—†๋Š” ์ด๋ฆ„์ž…๋‹ˆ๋‹ค..")
        return
    }
}
 
  • ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•ด ์ถ”๊ฐ€๋กœ ์กฐ์‚ฌํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์„ฑ ์ธก๋ฉด์—์„œ ์ข‹์ง€ ๋ชปํ•˜๋‹ค.
  • ๊ฐœ๋ฐœ์ž๊ฐ€ ํ•˜๋‚˜์˜ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ , ๊ทธ ์•ˆ์—์„œ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ• ์ง€์— ์ •์˜ํ•ด๋‘๋ฉด ํŽธํ•˜์ง€ ์•Š์„๊นŒ?

// Using Facade
internal struct Facade {
 
    internal func run(name: String) {
        if let row = cache.get(with: name) {
 
            let message = Message(row: row)
            print(message.makeName())
            print(message.makeBirthday())
            print(message.makeEmail())
 
        } else if let row = dbms.query(name: name) {
            cache.put(row: row)
 
            let message = Message(row: row)
            print(message.makeName())
            print(message.makeBirthday())
            print(message.makeEmail())
 
        } else {
            print("์—†๋Š” ์ด๋ฆ„์ž…๋‹ˆ๋‹ค..")
            return
        }
    }
 
    private var dbms = DBMS()
    private var cache = Cache()
}
 
internal func main() {
    let facade = Facade()
 
    facade.run(name: "wansik")
}
  • Facade ๊ฐ์ฒด ์•ˆ์— ํ•ด๋‹น ํ•จ์ˆ˜์˜ ๋‚ด์šฉ์„ ์ด๋™ํ–ˆ๋‹ค.
  • ์™ธ๋ถ€์—์„œ๋Š” run(name:)๋งŒ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค.

๋™๊ธฐ

  • ์ปดํŒŒ์ผ๋Ÿฌ ์‹œ์Šคํ…œ์„ ์ƒ๊ฐํ•ด๋ณด์ž.
  • Parser, Scanner ๋“ฑ์˜ ์—ฌ๋Ÿฌ ์ž‘์—…์„ ๊ฑฐ์ณ์•ผ ๋น„๋กœ์†Œ ๋ฐ”์ด๋„ˆ๋ฆฌ๊ฐ€ ๋งŒ๋“ค์–ด์ง„๋‹ค.
  • ํ•˜์ง€๋งŒ ์‚ฌ์šฉํ•˜๋Š” ์ชฝ์—์„œ๋Š” compile() ๋ฉ”์„œ๋“œ ํ•˜๋‚˜๋งŒ ์•Œ๋ฉด๋œ๋‹ค.

ํ™œ์šฉ์„ฑ

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

๊ตฌ์กฐ

์ฐธ์—ฌ์ž

  • Facade(Facade)
    • ๋‹จ์ˆœํ•˜๊ณ  ์ผ๊ด€๋œ ํ†ตํ•ฉ ์ธํ„ฐํŽ˜์ด์Šค ์ œ๊ณต
    • ์„œ๋ธŒ ์‹œ์Šคํ…œ์„ ๊ตฌ์„ฑํ•˜๋Š” ํด๋ž˜์Šค๊ฐ€ ์–ด๋–ค ํด๋ž˜์Šค๋ฅผ ์š”์ฒญํ•ด์•ผ ํ•˜๋Š”์ง€ ์•Œ๊ณ  ์žˆ์Œ
      • ์œ„์˜ ์˜ˆ์‹œ๋กœ๋ถ€ํ„ฐ ์ƒ๊ฐํ•ด๋ณด๋ฉด ์›๋ž˜๋Š” main์—์„œ ๊ฐœ๋ฐœ์ž๊ฐ€ ํ•˜๋‹ค๊ฐ€ ์˜ฎ๊ธด ๊ฒƒ ๋ฟ์ž„
      • ์ฆ‰, ๋งŒ๋“  ๊ฐœ๋ฐœ์ž๊ฐ€ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€ ๋‹ค ์•Œ๊ณ  ์ฝ”๋“œ๋ฅผ ์˜ฎ๊ฒจ๋‘” ๊ฒƒ
    • ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์„ ์„œ๋ธŒ์‹œ์Šคํ…œ ๊ฐ์ฒด๋“ค์—๊ฒŒ ์ „๋‹ฌํ•จ
  • Subsystem(DBMS, Cache)
    • ์„œ๋ธŒ์‹œ์Šคํ…œ์˜ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•จ
    • ์ž‘์—…์„ ์‹ค์ œ๋กœ ์ฒ˜๋ฆฌํ•˜์ง€๋งŒ Facade์— ๋Œ€ํ•œ ์•„๋ฌด๋Ÿฐ ์ •๋ณด๊ฐ€ ์—†์Œ

ํ˜‘๋ ฅ ๋ฐฉ๋ฒ•

  • ์‚ฌ์šฉ์ž๋Š” Facade์— ์ •์˜๋œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒํ˜ธ์ž‘์šฉํ•จ
  • Facade๋Š” ์•Œ๋งž์€ ๊ฐ์ฒด์—๊ฒŒ ํ•ด๋‹น ์ž‘์—…์„ ์ „๋‹ฌํ•จ
  • ์‚ฌ์šฉ์ž๋Š” ์„œ๋ธŒ์‹œ์Šคํ…œ์„ ๊ตฌ์„ฑํ•˜๋Š” ๊ฐ์ฒด๋กœ ์ง์ ‘ ์ ‘๊ทผํ•  ํ•„์š”๊ฐ€ ์—†์Œ

๊ฒฐ๊ณผ

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

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

  • ์ถ”์ƒ ํŒฉํ† ๋ฆฌ(03. Abstract Factory)
    • ์„œ๋ธŒ ์‹œ์Šคํ…œ์— ๋…๋ฆฝ์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐ์ฒด ์ƒ์„ฑ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ํ•จ๊ป˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ์ค‘์žฌ์ž(20. Mediator) - ๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” ํด๋ž˜์Šค์˜ ๊ธฐ๋Šฅ์„ฑ์„ ์ถ”์ƒํ™”ํ•˜๋Š” ๋ฉด์—์„œ ์œ ์‚ฌ, ํ•˜์ง€๋งŒ
    • ์ค‘์žฌ์ž: ๊ฐ์ฒด๋“ค๊ฐ„์˜ ํ˜‘๋ ฅ ๊ด€๊ณ„๋ฅผ ์ถ”์ƒํ™”ํ•˜์—ฌ ๊ธฐ๋Šฅ์˜ ์ง‘์ค‘์„ ๋ง‰์ž.
    • 13. Facade: ์„œ๋ธŒ์‹œ์Šคํ…œ ์ธํ„ฐํŽ˜์ด์Šค ์ž์ฒด๋ฅผ ์ถ”์ƒํ™”ํ•˜์—ฌ ์‚ฌ์šฉ์„ ์šฉ์ดํ•˜๊ฒŒ ํ•˜์ž.
  • ๋‹จ์ผ์ฒด(08. Singleton) - ํ•˜๋‚˜๋งŒ ์žˆ์–ด๋„ ๋˜๋ฉด ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ๊ตฌํ˜„

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

  • UIKit๊ณผ ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ด๋Ÿฐ์‹์œผ๋กœ ๋งŽ์ด ๋˜์–ด์žˆ๋‹ค.
  • ์‚ฌ์šฉํ•˜๊ธฐ ํŽธํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด UILabel ๊ฐ™์€ ๊ฒƒ์„ ์ œ๊ณตํ•˜์ง€๋งŒ,
  • ์‹ค์งˆ์ ์œผ๋กœ ์•„๋žซ๋‹จ์€ CoreText์™€ ๊ฐ™์€ ๊ฒƒ์œผ๋กœ ๋˜์–ด์žˆ๋‹ค.
  • ๋งž๋Š” ์˜ˆ๊ฐ€ ์•„๋‹ ์ˆ˜ ์žˆ์œผ๋‚˜, ๊ฒฐ๊ตญ ์‚ฌ์šฉ์„ฑ์„ ์œ„ํ•ด Interface์˜ ์ œ์•ฝ์„ ๊ฑธ์—ˆ๋‹ค๋Š” ์ ์—์„œ ์œ ์‚ฌํ•˜๋‹ค ํ•  ์ˆ˜ ์žˆ๋‹ค.

Reference