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

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

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

  • ์ข…์†์ž, ๊ฒŒ์‹œ-๊ตฌ๋…(Publish-Subscribe)๋ผ ๋ถˆ๋ฆฐ๋‹ค.
  • swift index์ด ์ด ํ˜•ํƒœ๋ผ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.
  • 03. Notification To Combine ์—์„œ ์‚ฌ์šฉํ•˜๋Š” Notification๋„ ๊ฐ™์€ ํ˜•ํƒœ๋‹ค.

์˜ˆ์‹œ

Code

//
//  main.swift
//  Observer
//
//  Created by Choiwansik on 2023/09/26.
//
 
import Foundation
 
internal func main() {
    let diceGame: DiceGame = FairDiceGame()
 
    let player1: Player = EvenBettingPlayer(name: "์™„์‹")
    let player2: Player = OddBettingPlayer(name: "์ตœ์™„์‹")
    let player3: Player = OddBettingPlayer(name: "์™„์ˆ™์ด")
 
    diceGame.add(player: player1)
    diceGame.add(player: player2)
    diceGame.add(player: player3)
 
    (0..<5).forEach { _ in
        diceGame.play()
        print()
    }
}
 
main()
 
//
//  Player.swift
//  Observer
//
//  Created by Choiwansik on 2023/09/26.
//
 
import Foundation
 
public class Player {
 
    public init(name: String) {
        self.name = name
    }
 
    public func update(diceNumber: Int) {
 
    }
 
    public let name: String
 
}
 
//
//  OddBettingPlayer.swift
//  Observer
//
//  Created by Choiwansik on 2023/09/26.
//
 
import Foundation
 
public class OddBettingPlayer: Player {
 
    override public init(name: String) {
        super.init(name: name)
    }
 
    override public func update(diceNumber: Int) {
        if diceNumber % 2 == 1 {
            print("\(self.name)๊ฐ€ ์ด๊น€!")
        }
    }
 
}
 
//
//  EvenBettingPlayer.swift
//  Observer
//
//  Created by Choiwansik on 2023/09/26.
//
 
import Foundation
 
public class EvenBettingPlayer: Player {
 
    override public init(name: String) {
        super.init(name: name)
    }
 
    override public func update(diceNumber: Int) {
        if diceNumber % 2 == 0 {
            print("\(self.name)๊ฐ€ ์ด๊น€!")
        }
    }
 
}
 
//
//  DiceGame.swift
//  Observer
//
//  Created by Choiwansik on 2023/09/26.
//
 
import Foundation
 
public class DiceGame {
 
    public func add(player: Player) {
        self.players.append(player)
    }
 
    public func play() {
        
    }
 
    public var players = [Player]()
 
}
 
//
//  FairDiceGame.swift
//  Observer
//
//  Created by Choiwansik on 2023/09/26.
//
 
import Foundation
 
public class FairDiceGame: DiceGame {
 
    override public func play() {
        guard let diceNumber = (1...6).randomElement() else {
            return
        }
 
        print("์ฃผ์‚ฌ์œ„ ๋˜์ง \(diceNumber)")
 
        var iter = self.players.makeIterator()
        while let player = iter.next() {
            player.update(diceNumber: diceNumber)
        }
 
    }
    
}
 
์ฃผ์‚ฌ์œ„ ๋˜์ง 4
์™„์‹๊ฐ€ ์ด๊น€!

์ฃผ์‚ฌ์œ„ ๋˜์ง 6
์™„์‹๊ฐ€ ์ด๊น€!

์ฃผ์‚ฌ์œ„ ๋˜์ง 5
์ตœ์™„์‹๊ฐ€ ์ด๊น€!
์™„์ˆ™์ด๊ฐ€ ์ด๊น€!

์ฃผ์‚ฌ์œ„ ๋˜์ง 2
์™„์‹๊ฐ€ ์ด๊น€!

์ฃผ์‚ฌ์œ„ ๋˜์ง 5
์ตœ์™„์‹๊ฐ€ ์ด๊น€!
์™„์ˆ™์ด๊ฐ€ ์ด๊น€!

ํ™œ์šฉ์„ฑ

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

์ฐธ์—ฌ์ž

  • Subject(DiceGame)
    • ๊ฐ์‹œ์ž๋“ค์„ ์•Œ๊ณ  ์žˆ๋Š” ์ฃผ์ฒด.
    • ๊ฐ์‹œ์ž๋“ค์„ ๋ถ™์ผ ์ˆ˜ ์žˆ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ •์˜๋˜์–ด์•ผ ํ•จ
  • Observer(Player)
    • ์ฃผ์ฒด์— ๋ณ€ํ™”๋ฅผ ๊ด€์‹ฌ์žˆ์–ด ํ•˜๋Š” ์นœ๊ตฌ๋“ค์—๊ฒŒ ํ•„์š”ํ•œ ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
  • ConcreteSubject(FairDiceGame)
  • ConcreteObserver(OddBettingPlayer, EvenBettingPlayer)

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

  1. Subject์™€ Observer ํด๋ž˜์Šค ๊ฐ„์—๋Š” ์ถ”์ƒ์ ์ธ ๊ฒฐํ•ฉ๋งŒ์ด ์กด์žฌํ•œ๋‹ค.
    • ์ธํ„ฐํŽ˜์ด์Šค๋กœ๋งŒ ์–ฝํ˜€์žˆ์„ ๋ฟ์ด๋‹ค.
  2. Broadcast ๋ฐฉ์‹์˜ ๊ต๋ฅ˜๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ๋‹ค.
    • ์ด ํŒจํ„ด์—์„œ ์ฃผ์ฒด์ž๋Š” ๊ตฌ์ฒด์ ์ธ ์ˆ˜์‹ ์ž๋ฅผ ์•Œ ํ•„์š”๊ฐ€ ์—†๋‹ค.
    • ๊ทธ๋ƒฅ ๋“ฑ๋ก๋œ์• ํ•œํ…Œ ๋‹ค ๋ฟŒ๋ฆฌ๊ณ  ๋์ด๋‹ค.
    • ๋ฐ›๋Š”์ชฝ์—์„œ ๋ฌด์‹œํ• ์ง€ ๋ฐ›์„์ง€๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.
  3. ์˜ˆ์ธกํ•˜์ง€ ๋ชปํ•œ ์ •๋ณด๋ฅผ ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ๊ฐ์‹œ์ž๋“ค๋ผ๋ฆฌ ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๊ณ  ํ•˜์ž.
    • ์ •๋ณด ๊ฐฑ์‹ ์ž์ฒด์˜ ๋น„์šฉ์ด ํฐ ์ƒํ™ฉ์ผ ๋•Œ, ํ•œ ์ •๋ณด๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ์ฃผ์ฒด๋“ค์€ ํ•˜์œ„ ๊ฐ์‹œ์ž๋“ค์—๊ฒŒ ๋˜ ์ˆ˜์ •์„ ๊ฐ€ํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ์ด ์ˆ˜์ •์€ ๋ถˆํ•„์š”ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ์ •๋ณด๊ฐ€ ์•„๋‹ ์ˆ˜๋„ ์žˆ๋‹ค.
    • ์ฆ‰, ๋ณ€๊ฒฝ์˜ ์ด์œ ์— ๋Œ€ํ•œ ๊ฒƒ์„ ์œ ์ถ”ํ•˜๋Š” ๊ฒƒ์ด ์–ด๋ ต๋‹ค.

Reference