Skip to content

Latest commit

ย 

History

History
147 lines (112 loc) ยท 4.34 KB

ObserverPattern.md

File metadata and controls

147 lines (112 loc) ยท 4.34 KB

Observer Pattern์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜์‹œ์˜ค

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์€ ๊ธ€

Answer

Observer: ๊ด€์ฐฐ์ž.

Observer Pattern์€ ํ•ด๋‹น ํ”„๋กœํผํ‹ฐ๊ฐ€ ๋ณ€๊ฒฝ ๋˜๋Š”๊ฑธ ๊ด€์ฐฐํ•˜๊ณ  ์žˆ๋‹ค๊ฐ€ ๋ณ€๊ฒฝ ๋˜๋Š” ์‹œ์ ์—์„œ update๊ฐ€ ์ˆ˜ํ–‰ ๋˜๊ฒŒ ๋˜๋Š” ํ˜•ํƒœ๋ฅผ ๋งํ•œ๋‹ค. Delegate Pattern์˜ 1:1 ๊ด€๊ณ„์™€ ๋‹ค๋ฅด๊ฒŒ, ๊ฐ์ฒด๊ฐ„์˜ 1:N ์˜์กด ๊ด€๊ณ„๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ํŒจํ„ด์ด๋‹ค.

ํŠน์ • ๊ฐ’์ด ๋ณ€๊ฒฝ์ด ๋ฐœ์ƒ ํ•  ๋•Œ ๋‹ค๋ฅธ ์—ฐ์‡„์ ์œผ๋กœ ๊ทธ ๊ฐ’์„ ์ฐธ์กฐ ํ•˜๊ณ  ํ•˜๊ณ  ์žˆ๋Š” ๊ฐ’๋“ค์ด ์ž๋™ ์ ์œผ๋กœ ๋ณ€๊ฒฝ์ด ์ด๋ฃจ์–ด ์ ธ์•ผ ๋  ๋•Œ ์‚ฌ์šฉ ํ•˜๋ฉด ์œ ์šฉํ•˜๋‹ค.

๋”ฐ๋ผ์„œ, NotificationCenter์™€ ์กด์žฌ ์ด์œ ๊ฐ€ ๋น„์Šทํ•˜๋‹ค(์ž์„ธํ•œ ๊ฑด ํ•˜๋‹จ์—์„œ).

Observer Pattern with protocols

  • Observerable ํ•œ ํŒจํ„ด์„ ์œ„ํ•ด ํ”„๋กœํ† ์ฝœ์„ ํ†ตํ•œ ์ ‘๊ทผ ๋ฐฉ์‹ ์ •์˜

    // MARK: -Observerable Protocol
    protocol Observer {
        var id: Int { get }
        func update()
    }
  • Subject ํด๋ž˜์Šค: ๊ฐ์ฒด์—๊ฒŒ ์—…๋ฐ์ดํŠธ๋ฅผ ์•Œ๋ฆฌ๋Š” ์—ญํ• 

    ์˜ต์ €๋ฒ„๋ฅผ attachํ•˜๋ฉด, ์ด๋ฅผ ObserverArray์— ๋„ฃ๋Š”๋‹ค.

    ๋‹ค๋ฅธ ๊ฐ์ฒด์—๊ฒŒ notifyํ•˜๊ฒŒ ๋˜๋ฉด ์ด ObserverArray๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ Observer Protocol ๋‚ด update() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

    class Subject {
        private var observerArray = [Observer]()
        private var _number = Int()
        
        // number๋ณ€์ˆ˜๊ฐ€ ์ดˆ๊ธฐํ™”๋˜๋ฉด notify
        var number: Int {
            set {
                _number = newValue
                notify()
            }
            get {
                return _number
            }
        }
        
        // ์˜ต์ €๋ฒ„ ๋“ฑ๋ก
        func attachObserver(observer: Observer) {
            observerArray.append(observer)
        }
        
        // ๋“ฑ๋ก๋œ ์˜ต์ €๋ฒ„๋“ค์—๊ฒŒ notify
        private func notify() {
            for observer in observerArray {
                observer.update()
            }
        }
    }
    • ํ”„๋กœ์ฝ”ํ†จ ์ฑ„ํƒ
    // 2์ง„์ˆ˜ ๋ณ€ํ™˜
    class BinaryObserver: Observer {
        private var subject = Subject()
        var id = Int()
        
        init(subject: Subject, id: Int) {
            self.subject = subject
            self.subject.attachObserver(observer: self) // ์˜ต์ €๋ฒ„ ๋“ฑ๋ก
            self.id = id
        }
        
        func update() {
            print("\(subject.number)์˜ 2์ง„์ˆ˜ ๋ณ€ํ™˜: \(String(subject.number, radix:2))")
        }
    }
    
    // 16์ง„์ˆ˜ ๋ณ€ํ™˜
    class HexObserver: Observer {
        private var subject = Subject()
        var id = Int()
        
        init(subject: Subject, id: Int) {
            self.subject = subject
            self.subject.attachObserver(observer: self) // ์˜ต์ €๋ฒ„ ๋“ฑ๋ก
            self.id = id
        }
        
        func update() {
            print("\(subject.number)์˜ 16์ง„์ˆ˜ ๋ณ€ํ™˜: \(String(subject.number, radix:16))")
        }
    }
    
    
    // 8์ง„์ˆ˜ ๋ณ€ํ™˜
    class OctaObserver: Observer {
        private var subject = Subject()
        var id = Int()
        
        init(subject: Subject, id: Int) {
            self.subject = subject
            self.subject.attachObserver(observer: self) // ์˜ต์ €๋ฒ„ ๋“ฑ๋ก
            self.id = id
        }
        
        func update() {
            print("\(subject.number)์˜ 8์ง„์ˆ˜ ๋ณ€ํ™˜: \(String(subject.number, radix:8))")
        }
    }
    • notify
    let subject = Subject()
    
    let binary = BinaryObserver(subject: subject, id: 1)
    let octa = OctaObserver(subject: subject, id: 2)
    let hex = HexObserver(subject: subject, id: 3)
    
    subject.number = 15 // notify
    subject.number = 2 // notify
    • output
    15์˜ 2์ง„์ˆ˜ ๋ณ€ํ™˜: 1111
    15์˜ 8์ง„์ˆ˜ ๋ณ€ํ™˜: 17
    15์˜ 16์ง„์ˆ˜ ๋ณ€ํ™˜: f
    
    2์˜ 2์ง„์ˆ˜ ๋ณ€ํ™˜: 10
    2์˜ 8์ง„์ˆ˜ ๋ณ€ํ™˜: 2
    2์˜ 16์ง„์ˆ˜ ๋ณ€ํ™˜: 2

More

Observer Pattern์€ observer๋ฅผ ๋“ฑ๋กํ•˜๊ณ  notification์„ postํ•œ๋‹ค๋Š” ๋ฐ์— ์žˆ์–ด์„œ NotificationCenter์™€ ๊ทธ ๋™์ž‘ ๋ฐฉ์‹์ด ๋น„์Šทํ•˜๋‹ค.

๊ทธ๋Ÿผ ๋ญ๊ฐ€ ๋‹ค๋ฅผ๊นŒ? NotificationCenter๋Š” Observer Pattern์˜ ๊ฐœ๋…์„ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ด๋‹ค.

๋”ฐ๋ผ์„œ NotificationCenter๋Š” ๊ธฐ์กด Observer Pattern์—์„œ observer๋ฅผ ๋“ฑ๋กํ•˜๊ณ  notification์„ ์ฃผ๋Š” ์—ญํ• ๋งŒ ๋นผ์„œ ์ถ”์ƒํ™” ๋ ˆ๋ฒจ์„ ์˜ฌ๋ฆฐ ๊ตฌํ˜„์ฒด๋ผ๋Š” ์ฐจ์ด์ ์ด ์žˆ๋‹ค.