← → or space · progress saves for Continue on the roadmap
Goal
Centralize construction of NotificationSender implementations behind one API (SenderFactory.create).
Step 1 - Types
enum SenderKind { email, sms, push }
abstract class NotificationSender {
void send(String to, String body);
}
class EmailSender implements NotificationSender {
@override
void send(String to, String body) {
print('email -> $to: $body');
}
}
class SmsSender implements NotificationSender {
@override
void send(String to, String body) {
print('sms -> $to: $body');
}
}
class PushSender implements NotificationSender {
@override
void send(String to, String body) {
print('push -> $to: $body');
}
}Step 2 - Factory
class SenderFactory {
static NotificationSender create(SenderKind kind) {
return switch (kind) {
SenderKind.email => EmailSender(),
SenderKind.sms => SmsSender(),
SenderKind.push => PushSender(),
};
}
}
void main() {
final sender = SenderFactory.create(SenderKind.sms);
sender.send('+1000', 'hello');
}When this fits
- Construction rules grow (config flags, A/B tests, environment).
- Call sites should not list every
newvariant.
Practice tasks
- Add
SenderKind.consolethat prints a tagged line for local dev. - Throw
ArgumentErrorfor unsupported combinations when you add a second enum (for exampleRegion). - Replace
static createwith a top-level functionNotificationSender senderFor(SenderKind k)and note readability tradeoffs.