MVVM
MVVM (Model-View-ViewModel) is a design pattern used in Android development to create maintainable and testable code.
-
MVVM (Model-View-ViewModel) is a design pattern used in Android development to create maintainable and testable code. It separates the development of the graphical user interface from the business logic and the back-end logic (data).
- Components of MVVM
- Model
- Purpose: Represents the data layer of the application. This includes data sources like databases, web services, and repositories.
- Role: Responsible for handling the data and business logic.
- Implementation: Can be implemented using plain Java/Kotlin classes, Room database, Retrofit for network operations, etc.
- View
- Purpose: Represents the UI layer. This includes activities, fragments, and any custom views.
- Role: Displays the data and forwards user interactions to the ViewModel.
- Implementation:XML layout files and UI components in activities/fragments.
- ViewModel
- Purpose: Acts as a bridge between the View and the Model.
- Role: Fetches data from the Model and prepares it for the View. It also handles user interactions that require data updates.
- Implementation:Uses LiveData or other observable types to expose data to the View and update it reactively.
- Flow of Data in MVVM
- User Interaction: User interacts with the View (e.g., clicks a button).
- ViewModel Update:The View forwards the interaction to the ViewModel.
- Model Interaction: The ViewModel updates or requests data from the Model.
- Data Update: The Model sends updated data back to the ViewModel.
- View Update: The ViewModel updates the View using LiveData or other observables, ensuring the UI reflects the latest data.
- Example of MVVM in Android Studio
- Below is a simple example of an MVVM implementation in Android Studio using Java:
- Model (User.java)
- Repository (UserRepository.java)
- ViewModel (UserViewModel.java)
- View (MainActivity.java)
- Layout (activity_main.xml)
package com.example.mvvmexample.model;
public class User {
private String name;
private String email;
public User(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
}
package com.example.mvvmexample.repository;
import com.example.mvvmexample.model.User;
public class UserRepository {
public User getUser() {
// Simulating a network or database operation
return new User("John Doe", "john.doe@example.com");
}
}
package com.example.mvvmexample.viewmodel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.example.mvvmexample.model.User;
import com.example.mvvmexample.repository.UserRepository;
public class UserViewModel extends ViewModel {
private MutableLiveData userLiveData;
private UserRepository userRepository;
public UserViewModel() {
userRepository = new UserRepository();
userLiveData = new MutableLiveData<>();
}
public LiveData' getUser() {
return userLiveData;
}
public void loadUser() {
User user = userRepository.getUser();
userLiveData.setValue(user);
}
}
package com.example.mvvmexample.view;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import android.widget.TextView;
import com.example.mvvmexample.R;
import com.example.mvvmexample.model.User;
import com.example.mvvmexample.viewmodel.UserViewModel;
public class MainActivity extends AppCompatActivity {
private TextView tvName;
private TextView tvEmail;
private UserViewModel userViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvName = findViewById(R.id.tvName);
tvEmail = findViewById(R.id.tvEmail);
userViewModel = new ViewModelProvider(this).get(UserViewModel.class);
userViewModel.getUser().observe(this, new Observer() {
@Override
public void onChanged(User user) {
tvName.setText(user.getName());
tvEmail.setText(user.getEmail());
}
});
userViewModel.loadUser();
}
}
<!-- res/layout/activity_main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="18sp"
android:padding="8dp"/>
<TextView
android:id="@+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:text/>
<LinearLayout />
- Summary
- In this example, the MVVM pattern ensures a clear separation of concerns:
- The Model handles data.
- The ViewModel serves as an intermediary, processing data for the View.
- The View observes changes from the ViewModel and updates the UI accordingly.
- This structure makes the codebase more modular, testable, and maintainable, allowing for easier updates and enhancements.
Comments
Post a Comment