Comparing MVC, MVP, and MVVM Design Patterns for Android Mobile Development
This article compares the MVC, MVP, and MVVM architectural patterns for Android mobile development, explains their differences, and provides step‑by‑step code examples for implementing MVP and MVVM, including model, view, presenter layers and data‑binding configuration.
Improving productivity is a key reason developers invest in architectural design; modular, decoupled code and high cohesion allow developers to focus on specific features without needing to understand the whole system. This article examines three common mobile design patterns—MVC, MVP, and MVVM—by comparing their concepts and providing concrete examples.
The three patterns share a UI layer (View) and a data layer (Model). The diagram below illustrates their common structure:
In MVC the Controller passively receives UI events and updates the Model indirectly via the View; Android Activities often become large and hard to maintain. MVP upgrades MVC by introducing a Presenter that fully isolates View and Model, handling UI updates directly. MVVM, originally from Microsoft’s WPF, relies on data binding to achieve complete decoupling of the UI from business logic, a capability added to Android Studio in 2015.
MVP Implementation
Below is a simple MVP example for a user‑data entry app. The project structure is shown in the image, and the data bean is defined as:
public class UserBean {
private String mFirstName;
private String mLastName;
public UserBean(String firstName, String lastName) {
this.mFirstName = firstName;
this.mLastName = lastName;
}
public String getFirstName() { return mFirstName; }
public String getLastName() { return mLastName; }
}The Model interface encapsulates CRUD operations:
public interface IUserModel {
Boolean insert(int id, String firstName, String lastName);
Boolean update(int id, String firstName, String lastName);
void delete(int id);
UserBean query(int id);
void reset();
List<UserBean> queryAll();
}The Presenter coordinates View and Model:
public class UserPresenter {
private IUserView mUserView;
private IUserModel mUserModel;
public UserPresenter(IUserView view) {
mUserView = view;
mUserModel = new UserModel();
}
public void saveUser(int userId, String firstName, String lastName) {
mUserModel.insert(userId, firstName, lastName);
}
public void loadUser(int userId) {
UserBean user = mUserModel.query(userId);
mUserView.setFirstName(user.getFirstName());
mUserView.setLastName(user.getLastName());
}
}The View interface defines UI callbacks:
public interface IUserView {
String getFristName();
String getLastName();
void setFirstName(String firstName);
void setLastName(String lastName);
}With these components the simple app demonstrates MVP without full Activity code, illustrating how the pattern reduces coupling.
MVVM Implementation
MVVM was first used in Microsoft’s WPF and later introduced to Android via the Data Binding library (Google I/O 2015). To use MVVM you need Android Studio 1.3+ and the data‑binding plugin.
Step 1: Enable Canary channel updates in Android Studio preferences.
Step 2: Add Data Binding dependencies in build.gradle:
dependencies {
classpath "com.android.tools.build:gradle:1.2.3"
classpath "com.android.databinding:dataBinder:1.0-rc0"
}Step 3: Apply the plugins in each module that uses data binding:
apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'Step 4: Define a layout with a <data> section that binds UI elements to a ViewModel:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="user" type="com.baidu.msg.base.User"/>
<variable name="vModel" type="com.baidu.msg.handlers.viewModel"/>
</data>
<LinearLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<EditText android:id="@+id/id" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入 id" android:text="@{user.getId()}" android:inputType="number"/>
<EditText android:id="@+id/first" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="inter first name" android:text="@{user.getFirstName()}" android:inputType="text"/>
<EditText android:id="@+id/last" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="inter last name" android:text="@{user.getLastName()}" android:inputType="text"/>
<Button android:id="@+id/save" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="save" android:onClick="@{vModel.onClickButton}"/>
<Button android:id="@+id/load" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="load"/>
</LinearLayout>
</layout>The <data> node defines variables that bind directly to the ViewModel, allowing UI updates without explicit code in the Activity. This demonstrates how MVVM achieves a higher degree of decoupling compared with MVP.
Conclusion
The article highlighted the characteristics and trade‑offs of MVC, MVP, and MVVM for Android development, emphasizing that architecture should match product size and team maturity. Early‑stage projects may benefit from simpler structures, while larger codebases gain maintainability from well‑chosen patterns that promote low coupling and high cohesion.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
