Card 03/ 05
All 5 cards
ExampleDifficulty: Beginner1 min
The Same Pattern, a Different Dependency
The pattern travels a long way from a database. Here it is behind an entirely different kind of dependency — a mail client sending ride confirmations.
interface MailClient {
void send(String to, String body);
}
class SmtpMailClient implements MailClient {
public void send(String to, String body) { /* talks to a real SMTP server */ }
}
class StubMailClient implements MailClient {
public void send(String to, String body) {
System.out.println("stub sent to " + to + ": " + body);
}
}
class NotificationService {
private final MailClient mailClient;
NotificationService(MailClient mailClient) {
this.mailClient = mailClient;
}
void notifyRideConfirmed(String rider) {
mailClient.send(rider, "Your ride is confirmed");
}
}stub sent to alex@example.com: Your ride is confirmedA constructor parameter instead of a new, in both cases — one swaps a database, the other swaps a mail server, and the fix reads the same way each time. That repetition is the actual lesson: this is not a database trick. It is a shape, and it is the shape every later topic in this subject assumes you can already see.