design-patterns

Quick-reference guide to software design patterns with concise Dart examples.

View project on GitHub

Defines an interface for creating an object, but lets subclasses decide which class to instantiate.

Unlike Abstract Factory, it focuses on a single product rather than families of products.

abstract class Notification {
  void send(String message);
}

class EmailNotification implements Notification {
  @override
  void send(String message) => print('Email: $message');
}

class SmsNotification implements Notification {
  @override
  void send(String message) => print('SMS: $message');
}

class NotificationFactory {
  static Notification create(String type) => switch (type) {
    'email' => EmailNotification(),
    'sms' => SmsNotification(),
    _ => throw ArgumentError('Unknown type: $type'),
  };
}

// Usage
NotificationFactory.create('email').send('Hello!');