Tldr
Model–View–Controller (MVC) separates an application’s data and rules, what the user sees, and the handling of user actions. The split helps when screen code and business rules are getting tangled.
Imagine a support tool where someone can close a ticket. The screen shows the ticket and a Close button. Before changing the status, the application must check that no tasks remain open. If the screen code also performs that check and saves the ticket, even a small interface change can become risky.
MVC organises code around three roles. Each role may involve several classes or files:
- Model: Holds the ticket’s state and the rule that it cannot close while tasks remain open. It may use a database, but it is more than a database record.
- View: Shows the ticket’s title, status, and available actions. It decides how information appears, not whether the ticket may close.
- Controller: Receives the Close action, asks the model to do the work, and prepares the response the user will see.
The useful boundary is between the rule and the screen. A second screen can present the same ticket differently without rewriting the closing rule. The rule can also be tested without opening a browser.
Follow one action
Here is one common web-style flow. The arrows mean “what happens next”; they do not mean that each box must be a separate service.
flowchart TB ACTION["User chooses Close"] INPUT["Controller handles the request"] MODEL["Model checks the rule and updates the ticket"] RESPONSE["Controller prepares the response"] VIEW["View shows the ticket's current state"] ACTION --> INPUT INPUT --> MODEL MODEL --> RESPONSE RESPONSE --> VIEW
If tasks are still open, the model refuses the change and the view shows an explanation. If none remain, the model changes the status and the view shows the updated ticket. The controller coordinates both outcomes; it should not quietly become the place where every business rule lives. Microsoft’s ASP.NET Core MVC overview describes the same separation of model, view, and controller responsibilities.
A small Python version
The roles can fit in one file while you learn them. This example keeps tickets in memory and assumes the open-task count is a non-negative integer. The view returns text, so you can try it in a terminal.
class Ticket:
def __init__(self, title, open_tasks):
self.title = title
self.open_tasks = open_tasks
self.status = "Open"
def close(self):
if self.open_tasks > 0:
raise ValueError("Finish the open tasks first.")
self.status = "Closed"
class TicketView:
def render(self, ticket):
return f"{ticket.title}: {ticket.status}"
def error(self, message):
return f"Ticket unchanged: {message}"
class TicketController:
def __init__(self, ticket, view):
self.ticket = ticket
self.view = view
def close(self):
try:
self.ticket.close()
except ValueError as error:
return self.view.error(str(error))
return self.view.render(self.ticket)
ticket = Ticket("Replace office router", open_tasks=0)
controller = TicketController(ticket, TicketView())
print(controller.close()) # Replace office router: Closed
blocked = Ticket("Upgrade firewall", open_tasks=2)
print(TicketController(blocked, TicketView()).close())
# Ticket unchanged: Finish the open tasks first.Ticket.close() owns the rule. TicketView formats both the result and the error. TicketController.close() connects the action to the model and view. In a real application, the framework may receive the request and choose a view, while other components handle saving to a database. The important part is that moving from a command-line display to a web page should not require moving the closing rule into the page.
Where Observer fits
Suppose both a ticket list and a detail screen are open at the same time. When a ticket changes, both may need to refresh. The Observer pattern offers one way to do that: interested parts subscribe to change notifications and respond when the model’s state changes.
Classic desktop MVC uses Observer to keep views in step with the model. Many server-rendered web versions instead fetch the latest data and render a fresh response for each request. The Python example above follows that direct flow and has no subscriptions. An interface that stays open and updates live may use notifications, polling, or another update mechanism. Martin Fowler’s GUI Architectures explains the Observer relationship in classic MVC.
Notifications also add work. If every small change refreshes every view, screens may do unnecessary updates and the sequence of events can become hard to follow. Keep subscriptions focused, and remove them when a view no longer needs updates.
Other patterns can appear inside an MVC application too. A view might be built from smaller views, or a controller might use interchangeable ways to handle a request. MVC does not require a fixed checklist of patterns. Martin Fowler notes how often MVC is interpreted differently; what matters here is being clear about each component’s responsibility.
When the split helps, and when it does not
MVC is useful when a user interface is growing, several screens use the same rules, or you want to test behaviour without rendering the interface. A framework can provide routing, templates, and conventions that make the split easier to follow. It cannot stop a developer from putting database queries in a view or business rules in a controller.
For a tiny script with one prompt and one result, formal MVC classes may add more moving parts than value. For a larger application, watch for the opposite problem: controllers that do all the real work while models are empty data holders. Start with clear responsibilities, and add structure when it makes a change or test easier.