What is the MVC Pattern?
Learn the MVC pattern -- Model, View, Controller roles and the request flow between them -- with a Java example and interview questions.
Expected Interview Answer
MVC (Model-View-Controller) is an architectural pattern that separates an application into three interconnected parts: the Model holds data and business logic, the View renders the UI from that data, and the Controller receives user input and coordinates updates between the Model and View.
The Controller sits at the entry point of a user action -- a button click or HTTP request -- and decides what should happen: it invokes methods on the Model to change state and then selects which View to render. The Model has no direct knowledge of the View; it simply exposes data and business operations, often notifying observers when it changes so Views can refresh. The View is a mostly passive rendering layer that reads from the Model and forwards user actions to the Controller. This separation lets the same Model be presented through multiple different Views, and lets UI technology change without rewriting business logic.
- Decouples business logic from presentation, easing testing
- Multiple Views can render the same Model differently
- Controllers centralize input-handling and coordination logic
- Encourages a maintainable, layered structure for growing applications
AI Mentor Explanation
The Model is the official match data -- scores, overs, wickets -- maintained independently of how it is displayed. The View is the stadium’s giant screen or the TV broadcast graphic that renders that data for spectators. The Controller is the third umpire or scorer who processes an event (a boundary, a review request) and updates the match data, which then flows back out to however many screens are displaying it. The same underlying score can appear on the stadium screen, a phone app, and a radio commentary summary simultaneously.
Step-by-Step Explanation
Step 1
User action reaches the Controller
A click, form submission, or HTTP request is routed to a Controller method.
Step 2
Controller updates the Model
Business logic executes and the Model's state changes accordingly.
Step 3
Model notifies or is queried
The Model exposes its new state, often via an observer mechanism or direct query.
Step 4
View renders from the Model
The Controller selects a View, which reads the updated Model and renders the UI.
What Interviewer Expects
- Correct role definition for each of Model, View, Controller
- Understanding that the Model has no direct dependency on the View
- Awareness that one Model can support multiple Views
- A concrete example of the request-to-render flow
Common Mistakes
- Putting business logic inside the Controller instead of the Model
- Believing the View directly modifies the Model without going through the Controller
- Confusing MVC with just “the folder structure” rather than the responsibility split
- Assuming MVC forbids the View from reading the Model directly
Best Answer (HR Friendly)
“MVC splits an application into three roles: the Model holds the data and business rules, the View displays that data to the user, and the Controller handles user input and decides how the Model and View should update in response. This separation makes it easier to change the UI without touching business logic, and to test the business logic independently of any interface.”
Code Example
class UserModel {
private String name;
void setName(String name) { this.name = name; }
String getName() { return name; }
}
class UserView {
void render(UserModel model) {
System.out.println("User: " + model.getName());
}
}
class UserController {
private final UserModel model;
private final UserView view;
UserController(UserModel model, UserView view) {
this.model = model;
this.view = view;
}
void updateName(String newName) {
model.setName(newName); // update Model
view.render(model); // refresh View
}
}
UserController controller = new UserController(new UserModel(), new UserView());
controller.updateName("Priya"); // simulates a user actionFollow-up Questions
- How does MVC differ from MVP?
- Can a single Model be rendered by multiple different Views simultaneously?
- Where should validation logic live in MVC -- Model or Controller?
- How does the observer pattern relate to keeping the View in sync with the Model?
MCQ Practice
1. In MVC, which component holds business logic and data?
The Model is responsible for data and business rules, independent of presentation.
2. What is the Controller's primary responsibility in MVC?
The Controller receives input, updates the Model, and selects which View to render.
3. Which statement about MVC is true?
Because the Model is presentation-agnostic, multiple Views can render it differently.
Flash Cards
MVC in one line? — Model holds data/logic, View renders it, Controller handles input and coordinates the two.
Does the Model know about the View? — No -- the Model is presentation-agnostic; the Controller mediates.
Key benefit of MVC? — Business logic can be tested and reused independently of the UI.
Can one Model have multiple Views? — Yes -- that is one of MVC's core strengths.