Junior — Middle
What architectural model is recommended for developing interfaces with Jetpack Compose?
sobes.tech AI
Answer from AI
When developing interfaces with Jetpack Compose, it is recommended to use the MVVM (Model-View-ViewModel) architectural pattern.
- Model — data and business logic.
- ViewModel — manages UI state and provides data for display.
- View (Compose UI) — a declarative interface that subscribes to state changes from the ViewModel.
MVVM works well with Compose because Compose automatically reacts to state changes provided by the ViewModel, simplifying UI management and increasing testability.
Example:
class MyViewModel : ViewModel() {
private val _state = mutableStateOf("Hello")
val state: State<String> = _state
fun updateText(newText: String) {
_state.value = newText
}
}
@Composable
fun MyScreen(viewModel: MyViewModel = viewModel()) {
val text by viewModel.state
Text(text = text)
}