How cirocosta/observer-c works
Purpose and Entry Points
observer-c is a small pure-C implementation of the Observer pattern. It provides reusable subject/observer interfaces, demonstrated by an animal-event program. Primary consumer entry points are observer/observer.h and observer/subject.h; the runnable example begins at example/main.c.
Observer Contract
Observers implement a notification callback defined by the observer abstraction. Subject implementations hold observer registrations and invoke that callback when their domain event occurs.
• observer/observer.h
• observer/observer.c
• observer/common.h
This is a callback/function-pointer design: observers depend only on the observer contract, rather than concrete subject types.
Subject Lifecycle and Registration
The subject layer defines the observable-side interface and owns registration/removal behavior. A concrete subject must implement its subject interface plus registerObserver and unregisterObserver, establishing the lifecycle boundary between a publisher and its listeners.
• observer/subject.h
• observer/subject.c
• observer/common.h
Notification Flow
Control flows from application code into a concrete subject event, then through the subject’s registered observer set, and finally into each observer’s notify implementation. Notification is synchronous and in-process; there is no broker, persistence, network transport, or asynchronous runtime indicated by the repository.
• observer/subject.c
• observer/observer.c
• example/models/cat.c
Concrete Domain Example
The example models a cat as the subject whose meow triggers reactions from animal observers. Cat-specific code adapts the generic subject mechanism to the event, while dog code implements a distinct observer reaction.
• example/models/cat.h
• example/models/cat.c
• example/models/dog.c
Application Composition
The example executable constructs the participating animal objects, connects observers to the cat, and triggers the observed behavior. It is the integration point that demonstrates object initialization, registration, event emission, and callback dispatch.
• example/main.c
• example/function-pointer.c
Runtime and Verification
The project targets a native C toolchain, with build/test orchestration centered on Makefile. There are no external services or infrastructure dependencies. Behavioral validation focuses on observable registration and notification semantics.
• Makefile
• tests/test-observable.c
Main entry points: observer.h Observer contract · main.c Example executable · subject.c Subject registration · cat.c Cat meow event
Components 12
Observer Library
Reusable callback-based observer and subject contracts.
- Observer contract · Defines the notification callback abstraction.
observer/observer.h - Observer implementation
observer/observer.c - Subject contract · Defines observable-side registration and removal.
observer/subject.h - Subject registration · Owns registered observers and synchronous dispatch.
observer/subject.c - Shared types
observer/common.h
Animal Example
Concrete cat event and dog observer composition.
- Cat subject API
example/models/cat.h - Cat meow event · Adapts a meow into generic subject notification.
example/models/cat.c - Dog reaction · Implements a distinct notify callback reaction.
example/models/dog.c - Example executable · Constructs animals, registers observers, and triggers the cat event.
example/main.c - Function pointer demo
example/function-pointer.c
Build & Runtime
- Make orchestration · Builds and runs native C targets.
Makefile - Native C runtime · Synchronous callback execution with no external transport.
Connections 16
- Observer contract → Observer implementation: implemented by
- Subject contract → Subject registration: implemented by
- Shared types → Observer contract: shared definitions
- Shared types → Subject contract: shared definitions
- Observer contract → Subject registration: callback contract
- Cat subject API → Cat meow event: declares
- Subject contract → Cat meow event: implements
- Observer contract → Dog reaction: implements notify
- Example executable → Cat meow event: creates and triggers
- Example executable → Dog reaction: creates and registers
- Cat meow event → Subject registration: uses registration and dispatch
- Subject registration → Dog reaction: synchronous notify · Dispatches to each registered observer.
- Make orchestration → Example executable: builds
- Make orchestration → Function pointer demo: builds
- Example executable → Native C runtime: runs in
- Function pointer demo → Native C runtime: runs in