Junior — Middle
Can you give an example of applying specific design patterns in software development?
sobes.tech AI
Answer from AI
The following design patterns are often used in Flutter development:
-
MVVM (Model-View-ViewModel): separates presentation logic and business logic, making testing and maintenance easier. For example, the ViewModel contains state and logic, while the View only displays.
-
Singleton: used to create a single instance of a class, for example, to manage application state or access a database.
-
Builder: helps create complex widgets step-by-step, improving code readability.
Example of using Singleton to manage state:
class AppState {
static final AppState _instance = AppState._internal();
factory AppState() => _instance;
AppState._internal();
int counter = 0;
}
void main() {
var state1 = AppState();
var state2 = AppState();
state1.counter++;
print(state2.counter); // Will output 1, since state1 and state2 are the same instance
}