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.
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.
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.
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.
What it practices
- Observer pattern
- Notify any number of subscribers without the subject knowing them concretely.
Related katas
- A Rule Tree Can Contain Any Rule Hard Free Replace a central tagged-tree evaluator with recursive rule objects that preserve child order, empty identities, and short-circuit behavior.
- Compose the Fees Hard Free Account fee variation is modeled by subclassing: PremiumAccount and StudentAccount each override MonthlyFee, so the base can never be sealed and every new flavor is a new class. Flatten the hierarchy into one sealed Account composed with an injected IFeePolicy. Extracting the policy but hard-coding a default inside the account trips the composition-root rule.
- The Leaky Payment Port Hard Free A checkout service is welded to a Stripe client and passes vendor DTOs around. Someone half-extracted an IPaymentGateway port, but it leaks Stripe types through its own signature. Make the port vendor-neutral with a real anti-corruption layer, not an interface that smuggles the SDK through.
- Make It a Value Object Hard Free A Reservation is a mutable two-phase DTO that also hands out its backing guest list. Make it a real value object, frozen at construction, and don't let the obvious
private setfix fool you into leaving the collection leak wide open.