Skip to content
Katabench
Try free

Ask for the Interface

Easy About 15 min Free, no account needed

An order notifier news up its own SmtpEmailer and types the field as the concrete class, even though an IMessenger abstraction sits right next to it. Make the notifier ask for the interface in its constructor. Typing the field as IMessenger while still newing the concrete inside is not the fix.

The brief

Notifications.OrderNotifier confirms orders by email. It builds its own collaborator, new SmtpEmailer(), and types the field as the concrete class. An IMessenger abstraction exists in the same folder, and SmtpEmailer even implements it, but the notifier never asks for it. The result: swapping the emailer for an SMS sender, or for a fake in a test, means editing the notifier. The class that decides when to notify has welded itself to how the message travels.

The trap

The reflex fix is to retype the field:

C#
private readonly IMessenger _messenger = new SmtpEmailer();

The field now looks abstract, but the notifier still constructs the concrete, so it still references SmtpEmailer and the "no concrete" rule stays red. Programming to an interface is not about the declared type of a variable. It is about who chooses the implementation. As long as the new lives inside the notifier, the notifier is doing the choosing.

Your job

Give the choice to the caller. Have OrderNotifier take an IMessenger through its constructor, hold it in a readonly field, and use it. The composition root (the Solution class here) is the one place allowed to know concrete types, so that is where new SmtpEmailer() belongs. The confirmation text must stay identical.

How it's graded

Your submission must (1) still pass the behavior tests (same confirmation strings) and (2) satisfy every rule shown in the results: OrderNotifier references the IMessenger abstraction, references no SmtpEmailer at all, keeps its fields readonly, and stays sealed. The retyped-field trap clears the abstraction rule and leaves the concrete rule failing; only constructor injection turns both green.

Depend on abstractions, and let someone else pick the concrete. A class that constructs its own dependencies has quietly taken on a second job: deciding the shape of the system around it.

How it is graded

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

SOLID principles exercises →

Related guides

What it practices

Dependency injection
Ask for collaborators through constructors instead of newing them up or locating them.

Related katas