What are all the design patterns in generic means ?
Answer
Overview
Design Patterns are reusable solutions to commonly occurring problems in software design. They are not code — they are templates/blueprints for solving recurring design problems.
There are 23 classic design patterns organized into 3 categories by the Gang of Four (GoF).
3 Categories of Design Patterns
| Category | Purpose | Count |
|---|---|---|
| Creational | How objects are created | 5 patterns |
| Structural | How objects are composed/structured | 7 patterns |
| Behavioral | How objects communicate/interact | 11 patterns |
1. Creational Patterns
Deal with object creation mechanisms.
| Pattern | Description | Flutter Example |
|---|---|---|
| Singleton | Only one instance of a class | text text text |
| Factory Method | Subclasses decide which object to create | text text |
| Abstract Factory | Create families of related objects | Theme factories |
| Builder | Construct complex objects step-by-step | text text |
| Prototype | Clone existing objects | text |
Singleton Example (Dart)
dartclass AppLogger { static final AppLogger _instance = AppLogger._internal(); factory AppLogger() => _instance; AppLogger._internal(); void log(String msg) => print('[LOG] $msg'); }
Factory Method Example (Dart)
dartabstract class Button { Widget build(); // Factory decides which button to create factory Button(String platform) { if (platform == 'ios') return IOSButton(); return AndroidButton(); } }
Builder Example (Dart)
dartclass QueryBuilder { String _table = ''; String? _where; int? _limit; QueryBuilder from(String table) { _table = table; return this; } QueryBuilder where(String condition) { _where = condition; return this; } QueryBuilder limit(int n) { _limit = n; return this; } String build() => 'SELECT * FROM $_table' '${_where != null ? ' WHERE $_where' : ''}' '${_limit != null ? ' LIMIT $_limit' : ''}'; } // Usage final query = QueryBuilder() .from('users') .where('age > 18') .limit(10) .build();
2. Structural Patterns
Deal with how classes and objects are composed.
| Pattern | Description | Flutter Example |
|---|---|---|
| Adapter | Convert one interface to another | Wrapping a library to match your interface |
| Bridge | Separate abstraction from implementation | Platform-specific implementations |
| Composite | Tree structure of objects | Flutter Widget tree |
| Decorator | Add behavior without modifying class | text text text |
| Facade | Simplified interface to complex system | Repository hiding API + DB complexity |
| Flyweight | Share data to support many objects efficiently | text |
| Proxy | Surrogate for another object | Caching proxy for API calls |
Decorator Example (Dart)
dartabstract class DataSource { String readData(); } class FileDataSource implements DataSource { String readData() => 'Raw File Data'; } // Decorator adds encryption without changing FileDataSource class EncryptedDataSource implements DataSource { final DataSource _wrappee; EncryptedDataSource(this._wrappee); String readData() => 'ENCRYPTED(${_wrappee.readData()})'; } // Usage DataSource source = EncryptedDataSource(FileDataSource()); print(source.readData()); // ENCRYPTED(Raw File Data)
Facade Example (Dart)
dart// Facade hides complexity of multiple systems class AuthFacade { final FirebaseAuth _auth = FirebaseAuth.instance; final FirebaseFirestore _db = FirebaseFirestore.instance; Future<void> signUp(String email, String password) async { final credential = await _auth.createUserWithEmailAndPassword( email: email, password: password); await _db.collection('users').doc(credential.user!.uid).set({ 'email': email, 'createdAt': FieldValue.serverTimestamp() }); } }
3. Behavioral Patterns
Deal with communication between objects.
| Pattern | Description | Flutter Example |
|---|---|---|
| Observer | Notify dependents on state change | text text text |
| Strategy | Swap algorithms at runtime | Sorting strategies, payment methods |
| Command | Encapsulate request as an object | Undo/Redo operations |
| State | Change behavior based on state | Navigation state, auth state |
| Iterator | Traverse collection without exposing internals | Dart's text |
| Template Method | Define skeleton; subclasses fill in steps | text |
| Chain of Responsibility | Pass request along a chain | Middleware, interceptors |
| Mediator | Centralize communication | EventBus, BLoC as mediator |
| Memento | Capture and restore state | Undo feature |
| Visitor | Add operations without changing classes | Code analyzers |
| Interpreter | Grammar interpreter | Query language parsers |
Observer Example (Dart)
dart// Observable (Subject) class Cart extends ChangeNotifier { List<String> _items = []; List<String> get items => _items; void addItem(String item) { _items.add(item); notifyListeners(); // Notifies all observers (Widgets) } } // Observer (View) Consumer<Cart>( builder: (context, cart, child) { return Text('Items: ${cart.items.length}'); }, )
Strategy Example (Dart)
dartabstract class SortStrategy { List<int> sort(List<int> data); } class BubbleSort implements SortStrategy { List<int> sort(List<int> data) { // bubble sort implementation return data..sort(); } } class QuickSort implements SortStrategy { List<int> sort(List<int> data) { // quick sort implementation return data..sort(); } } class Sorter { SortStrategy _strategy; Sorter(this._strategy); void setStrategy(SortStrategy strategy) => _strategy = strategy; List<int> sort(List<int> data) => _strategy.sort(data); } // Swap strategy at runtime final sorter = Sorter(BubbleSort()); sorter.setStrategy(QuickSort()); // Switch algorithm without changing Sorter
Design Patterns Most Common in Flutter
| Pattern | Used In |
|---|---|
| Singleton | text |
| Observer | text text text |
| Factory | text |
| Builder | Widget APIs, text text |
| Decorator | Widget wrapping ( text text |
| Facade | Repository pattern |
| Strategy | Different state management approaches |
Key Insight: Flutter's entire widget system is built on the Composite pattern — every widget is a tree of smaller widgets composited together.