Junior
What architectural patterns used in Android development do you know?
sobes.tech AI
Answer from AI
- MVC (Model-View-Controller): Divides the application into three interconnected parts. Model (data), View (user interface), and Controller (handles user actions). In Android, View is often an Activity/Fragment, Controller is also an Activity/Fragment, and Model is business logic and data.
- MVP (Model-View-Presenter): An improvement over MVC. View does not know about Model, and Presenter acts as an intermediary between View and Model. The Presenter contains logic for displaying data from the Model on the View and handling user actions.
- MVVM (Model-View-ViewModel): The View is connected to the ViewModel through Data Binding. The ViewModel contains presentation logic and does not hold references to the View. The View reacts to changes in the ViewModel.
Other patterns and concepts:
- Repository Pattern: Abstracts the data source (local database, network, cache) from business logic.
- Dependency Injection (DI): Injects dependencies (objects that the class depends on) from outside rather than creating them inside the class. Popular libraries: Dagger, Hilt, Koin.
- Clean Architecture: Separation into layers (Domain, Data, Presentation) with strict dependencies (inner layers do not know about outer layers).
- MVI (Model-View-Intent): Based on a unidirectional data flow. The View sends an Intent, the ViewModel processes it, updates the State, which is then displayed on the View.
- State Management: Managing the state of the user interface (e.g., using LiveData, StateFlow).
- Observer Pattern: An object (Subject) notifies other objects (Observers) about its changes, which have subscribed to notifications. Used in LiveData, Flow.
// Example of using LiveData in ViewModel (MVVM)
class MyViewModel : ViewModel() {
private val _data = MutableLiveData<String>()
val data: LiveData<String> = _data
fun fetchData() {
// Logic to load data
_data.value = "Loaded data"
}
}