Swift World: Design Patterns — Observer

Peng
SwiftWorld
Published in
2 min readMar 22, 2017

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.

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"

Thanks for your time. Please clap to get this article seen by more people. Please follow me by clicking Follow. As a passionate iOS developer, blogger and open source contributor, I’m also active on Twitter and GitHub.

--

--

Peng
SwiftWorld

Engineers are the artists of our generation.