Summer SALE

Observer in Swift

Observer is a behavioral design pattern that allows some objects to notify other objects about changes in their state.

The Observer pattern provides a way to subscribe and unsubscribe to and from these events for any object that implements a subscriber interface.



Usage examples: The Observer pattern is pretty common in Swift code, especially in the GUI components. It provides a way to react to events happening in other objects without coupling to their classes.

Identification: The pattern can be recognized by subscription methods, that store objects in a list and by calls to the update method issued to objects in that list.

The following examples are available on Swift Playgrounds.
Kudos to Alejandro Mohamad for creating the Playground version.

Observer in Other Languages

Observer in C# Observer in C++ Observer in Go Observer in Java Observer in PHP Observer in Python Observer in Ruby Observer in Rust Observer in TypeScript