OOP & SOLIDMedium30 XP5 min read
What are the common design patterns used in Flutter?
TL;DR: Flutter uses Singleton (services, GetIt), Factory (Widget.of()), Observer (Listenable, Stream, ChangeNotifier), Builder (widget builders, AlertDialog.builder), and Decorator (wrapping widgets like Padding, DecoratedBox).
Full Answer
Key Patterns in Flutter
- ▸Singleton: Navigator, GetIt, Dio instances — one shared instance
- ▸Factory: Theme.of(context), Navigator.of(context) — creates/finds the right instance
- ▸Observer: ChangeNotifier/Listenable — widgets subscribe and rebuild on change
- ▸Builder: ListView.builder, Dialog.builder — deferred widget creation
- ▸Decorator: Padding(child: Container(child: Text)) — wraps widgets to add behavior
- ▸Command: BLoC events — encapsulate an action as an object
- ▸State Machine: BLoC/Cubit state enum — transitions between defined states
Code Examples
dartObserver pattern with ChangeNotifier
Output
// Observer: ListenableBuilder rebuilds when cart changes // Singleton: AppRouter.instance is always the same object
Common Mistakes
- ✗Using Singleton for mutable, testable objects — makes testing impossible; use DI instead
- ✗Calling notifyListeners() in build() or constructors — should only be called after state changes
Interview Tip
💡
Connect patterns to Flutter APIs. Observer = Listenable/ChangeNotifier. Builder = widget builders. Decorator = the entire widget tree. Showing these connections proves you understand the framework deeply.
#design-patterns#singleton#factory#observer#decorator