M5 · Communication Between ObjectsSession 10-11BehavioralReshaped in KotlinOutline

Intent. Notify a set of subscribers automatically when state changes; the publisher does not know who is listening.

In Kotlin. Flow, StateFlow, SharedFlow; Delegates.observable for a single property.

Opens the communication module. The publisher not knowing its subscribers is the whole point. Read it against Flow and StateFlow and ask which of the classic Observer problems – leaked subscriptions, undefined delivery order, no backpressure – the coroutine library actually solved, and which it just moved.

Structure

classDiagram
    class Subject {
        -observers: List~Observer~
        +attach(o)
        +detach(o)
        +notify(event)
    }
    class Observer {
        <<interface>>
        +update(event)
    }
    class ConcreteObserverA {
        +update(event)
    }
    class ConcreteObserverB {
        +update(event)
    }
    Subject o--> Observer : does not know which
    Observer <|.. ConcreteObserverA
    Observer <|.. ConcreteObserverB

Telling It Apart

Patterns whose structure looks the same but whose intent does not. See the Confusing Pairs reference.

  • Mediator — Replace direct references between objects with a hub that coordinates them.