Swift World: Design Patterns - Observer

215 阅读1分钟
原文链接: pengguo.xyz

While developing iOS apps, maybe you will use Key-Value Observing (KVO) which helps one object observe another’s state changes. It’s an example of observer pattern. If you want to learn more about it, the official programming guide and this article from objc.io are good references.

As the following figure tells us, there are subject and observer in this pattern. The subject is the target to be observed. It holds the observer instances and will notify them about the changes. The observers get notified and handle the change. The logic is clear.

ObserverPattern

Let’s writing the codes. As an observer, it will only print some information when it is notified.

protocol Observer {
    func update()
}

class ConcreteObserver: Observer {
    var id: String

    init(id: String) {
        self.id = id
    }

    func update() {
        print(id + " observered the subject's state was changed.")
    }
}

As we mentioned, the subject has an observer list and notify every observer when its state is changed.

class Subject {
    var observers: [Observer] = []
    var state: String {
        didSet {
            notify()
        }
    }

    init(state: String) {
        self.state = state
    }

    func attach(observer: Observer) {
        observers.append(observer)
    }

    func notify() {
        for observer in observers {
            observer.update()
        }
    }
}

Let’s see how to use it.

//usage
let subject = Subject(state: "initial state")
let observerA = ConcreteObserver(id: "A")
let observerB = ConcreteObserver(id: "B")
subject.attach(observer: observerA)
subject.attach(observer: observerB)
subject.state = "new state"

That’s all for today. Thanks for your time.

Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>