The Observer Pattern Alternatives
Compare with delegates and Combine publishers.
The Observer Pattern Alternatives is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Observer Pattern Alternatives
NotificationCenter is one way to react to events. Swift offers others: delegation, Combine, and async streams. Each fits a different shape of communication.
The Observer Pattern
The observer pattern lets objects subscribe to events from a subject. NotificationCenter is a global, loosely coupled implementation of it.
import Foundation
// Subject posts, observers react, neither holds the other.
NotificationCenter.default.post(name: Notification.Name("tick"), object: nil)
print("Broadcast an event")Delegation
A delegate is a one-to-one link: an object forwards specific events to a single delegate via a protocol. It is explicit and type-safe.
protocol DownloadDelegate: AnyObject {
func didFinish(_ bytes: Int)
}
class Downloader {
weak var delegate: DownloadDelegate?
func finish() { delegate?.didFinish(1024) }
}
print("Delegate is one-to-one and typed")Delegate vs Notification
Use a delegate when exactly one object cares and the relationship is clear. Use NotificationCenter when many, unknown observers may react.
import Foundation
// One listener -> delegate. Many/unknown listeners -> NotificationCenter.
print("Cardinality guides the choice")Combine Publishers
Combine models events as publishers you subscribe to with sink, supporting transformation operators like map and filter.
import Combine
let subject = PassthroughSubject<Int, Never>()
let c = subject.sink { print("Got \($0)") }
subject.send(5)
_ = cNotificationCenter Publisher
Combine bridges NotificationCenter: publisher(for:) turns notifications into a stream you can transform.
import Foundation
import Combine
let name = Notification.Name("ping")
let c = NotificationCenter.default.publisher(for: name)
.sink { _ in print("Combine got notification") }
NotificationCenter.default.post(name: name, object: nil)
_ = cCombine Operators
Combine's strength is composing event pipelines: filter, debounce, combine, and map values declaratively.
import Combine
let subject = PassthroughSubject<Int, Never>()
let c = subject
.filter { $0 > 0 }
.map { $0 * 2 }
.sink { print($0) }
subject.send(-1)
subject.send(3)
_ = cAsync Sequences
Modern Swift offers AsyncStream and notification async sequences, letting you consume events with for await in structured concurrency.
import Foundation
func listen() async {
let name = Notification.Name("ping")
for await _ in NotificationCenter.default.notifications(named: name) {
print("Async observed")
break
}
}
print("Can await notifications")Closures and Callbacks
For a single, local callback a stored closure is simplest, with no observer machinery at all.
struct Button {
var onTap: () -> Void
func tap() { onTap() }
}
Button(onTap: { print("tapped") }).tap()Choosing an Approach
Delegate: one-to-one, explicit. NotificationCenter: many-to-many, decoupled. Combine/async: streams needing transformation. Closure: simple local callbacks.
import Foundation
// Match the tool to the communication shape.
print("Delegate, Notification, Combine, async, closure")Tradeoffs
NotificationCenter is flexible but untyped and easy to leak. Delegation and Combine are type-safe; Combine and async add power at the cost of more concepts.
import Foundation
// Prefer typed approaches when the relationship is known.
print("Type safety vs flexibility tradeoff")Quick Check
Which mechanism is best for a clear one-to-one, type-safe relationship?
Recap
The observer pattern has several Swift forms: NotificationCenter for decoupled many-to-many events, delegation for typed one-to-one links, Combine and async sequences for composable streams, and closures for simple callbacks. Choose by communication shape and type-safety needs. That completes the course.
Frequently asked questions
Is the “The Observer Pattern Alternatives” lesson free?
Yes — the full text of “The Observer Pattern Alternatives” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “The Observer Pattern Alternatives”?
Compare with delegates and Combine publishers. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The Observer Pattern Alternatives” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Posting and Observing Notifications
- Notification userInfo Payloads
- Removing Observers Safely
- The Observer Pattern Alternatives