Tell me about the MVP approach.
MVP (Model-View-Presenter) is an architectural pattern that emerged as a development of MVC to better separate responsibility between the interface and the logic, especially in interactive UI applications (mobile, desktop, web).
The main idea is to fully isolate logic from the View, so that the interface becomes "dumb" and can be tested without a UI.
1. Main components
Model
- Responsible for data and business logic.
- Stores information, performs queries, processes rules.
- Does not know who or how will show this data.
Example: a UserModel class with getUser(), saveUser() methods.
View
- Responsible only for displaying the interface.
- Contains no business logic and does not control the Model directly.
- Has an interface (contract) through which the Presenter works with it.
Example: a login screen with showLoading(), showError(), showUserInfo() methods.
Presenter
- The central element, an analog of the controller in MVC, but smarter.
- Manages the interaction between the View and the Model.
- Receives events from the View (for example, "the user clicked a button"), calls the needed Model methods, processes the response, and passes the result back to the View.
- Does not know exactly how the View is built; it works only through the interface.
Example:
LoginPresenter listens to the "Log in" button, calls AuthModel.login(), and depending on the result calls view.showSuccess() or view.showError().
2. How it works
User
↓
View ───▶ Presenter ───▶ Model
◀──────────────
(data update)- The user performs an action (for example, clicking a button).
- The View passes the event to the Presenter.
- The Presenter calls the Model and gets a result.
- The Presenter tells the View what and how to display.
3. Main differences from MVC
| Parameter | MVC | MVP |
|---|---|---|
| Controller | Directs the data flow | The Presenter manages logic and the View |
| View | Can turn to the Model directly | Works only through the Presenter |
| Interaction | View ↔ Controller ↔ Model | View → Presenter → Model → Presenter → View |
| Testability | Medium | High (the View can be replaced with mocks) |
4. Variants of MVP
- Passive View - the View is as "dumb" as possible, only showing data (ideal for tests).
- Supervising Presenter - part of the logic (for example, data bindings) stays in the View, and the Presenter controls the overall processes.
5. Advantages of MVP
Good testability - the Presenter can be tested without a UI. Separation of concerns - the interface contains no logic. Flexibility - the View is easy to change (for example, replacing a web interface with a mobile one). Simplified maintenance - the layers are independent and readable.
6. Drawbacks
More code (interfaces, contracts, connections). With a large UI, there is a risk of the Presenter "growing out of control". Requires discipline in separating roles.
7. Where MVP is used
- Android applications (before the move to MVVM, Google recommended MVP)
- Desktop applications (WinForms, Swing, Qt)
- Web applications with JavaScript frameworks before reactive models appeared (Knockout, early versions of AngularJS).
Summary:
MVP is a logically clean descendant of MVC: the View knows nothing about the data, the Model knows nothing about the interface, and the Presenter coordinates everything between them.
The result is code that is easy to test, maintain, and evolve.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.