Skip to content
Katabench
Try free

Notify Every Observer

Hard About 35 min Free, no account needed

BuildCoordinator reports completion to two constructor-injected observers, but its fields and constructor still name both concrete classes. Introduce one observer contract and make the coordinator broadcast through a collection without knowing which observers are present. A two-fake seam catches both concrete coupling and the tempting partial fix that notifies only the first subscriber.

The brief

BuildCoordinator finishes a build and reports the result to two stateful collaborators. The metrics observer records timing data, while the audit observer records the completed build. The current code produces the right output and even receives both collaborators through its constructor.

The design is still closed to a third observer. BuildCoordinator has one field and one constructor parameter for each concrete observer, so adding a deployment notifier or a cache warmer means editing the coordinator again. Constructor injection moved construction out of the class, but it did not remove the coordinator's knowledge of every concrete subscriber.

The trap

Simply extracting IBuildCompletedObserver is not enough. If both observers implement the interface while BuildCoordinator keeps concrete MetricsObserver and AuditObserver fields, the new abstraction is decorative. The coordinator is still coupled to the exact pair it knows today. The interface and implementation rules may turn green, but the abstraction reference and concrete ban rules stay red.

There is a second, quieter partial fix: accept a collection of observers, then invoke only the first element. That has the right type shape but breaks the broadcast contract. The grading seam replaces the real observers with unknown, stateful fakes and requires an observation from every item in the collection. It also creates two coordinators with disjoint observer pairs before running either one, so moving the collection into static shared state fails even if a constructor resets it.

Your job

Introduce the nongeneric IBuildCompletedObserver callback contract and make both concrete observers implement it. Refactor BuildCoordinator to retain a readonly collection of that abstraction and notify every item when a build completes. The coordinator must remain sealed and must not reference either concrete observer. Keep the real metrics and audit observers in Solution, and preserve the output exactly.

How it's graded

Your submission must pass all five behavior cases with the same metrics and audit strings. Static rules verify that the observer contract exists, both observers implement it, the coordinator refers to the abstraction instead of either concrete, its fields are readonly, and all three concrete classes are sealed. A substitution seam then injects two stateful observers that are unknown to the production code into each of two coordinator instances. It checks that every callback receives the right input values and that neither instance steals or replaces the other's observer collection.

An observer abstraction earns its keep only when the publisher can broadcast to an open set of subscribers without naming any of them.

How it is graded

Executable structural checks grade the shape of your types as well as their behavior.

SOLID principles exercises →

What it practices

Observer pattern
Notify any number of subscribers without the subject knowing them concretely.

Related katas