Responsibility Division in Object-Oriented Design
MVC divides a graphical component into Model, View, and Controller so that each object has one clearly defined role.
A Slider That Does Too Much
Imagine building a volume slider for a music app. The slider must remember how loud the volume currently is, draw itself on the screen at the correct position, and react when someone drags it. You could write all of that inside one large object. For a toy example that works, but it becomes hard to change the way the slider looks without accidentally breaking the way it stores its value, or the reverse. The Model-View-Controller pattern, usually called MVC, is a way of avoiding that tangle.
What do you think happens?
When a user drags the slider knob, does the drawing part of the component change the stored value directly, or does some other part handle that job first?
Reveal answer
Another part handles the drag first. The controller responds to the input and modifies the model, and the drawing part updates only after the model has changed.
This is the central habit of the pattern: user input does not paint pixels directly. It changes data, and the change is then shown. The rest of this article follows the slider through the three realms to show why.
Naming the Three Realms
The MVC pattern starts from a general principle of object-oriented design: an object should have one clearly defined role with a limited realm of responsibility. Applied to graphical user interfaces, that principle produces three realms. MVC is short for Model-View-Controller, and each name labels one realm. The Model is the realm of state, the place where the component's data lives. The View is the realm of presentation, the part that draws that state on the screen. The Controller is the realm of event handling, the part that responds to user and other events.
MVC divides a graphical component into Model, View, and Controller so that each object has one clearly defined role. The Model stores state, the View presents that state, and the Controller responds to events.
Why split the component at all? Because the three concerns change for different reasons. The way the slider looks may change when a designer updates its appearance. The rule for what volume values are allowed may change for a different reason. The way the slider reacts to a drag may change again. When these concerns share one object, a change to one can disturb the others. Separating them keeps each change contained inside the realm it belongs to.
The Model Holds the State
For the slider, the Model is the place where the current volume value lives. The Model does not draw anything, and it does not decide what to do when the mouse moves. It only holds the data and keeps it accurate. Because the state sits in its own object, any other part of the component that needs the volume asks the Model for it. Nothing keeps a separate copy that could fall out of step.
Asking the Model for the volume
The slider's Model currently stores a volume of 40. The View needs to know where to draw the knob. What should it do?
- 1Locate the state
The volume value lives in the Model, not in the View, so the View has no value of its own to consult.
- 2Ask the Model
The View requests the current volume from the Model and receives 40.
- 3Use the answer
The View positions the knob to correspond to 40. If the Model later holds 75, the next request returns 75 and the knob moves accordingly.
The View never keeps a private copy of the volume. It reads the Model whenever it needs the value, so the drawn knob and the stored value cannot disagree.
The volume numbers in the worked example are illustrative. The point is structural: the Model is the single source of the value, and every other part of the component reads from it.
The View Draws From the Model
The source notes that the View is often the component object itself, which makes it the easiest of the three parts to recognize. To draw correctly, the View has to consult the Model, because the Model holds the state and that state determines what appears on screen. The drawn knob position depends on the volume value stored in the Model, so the View keeps a reference to the Model object and reads the data when it paints.
Notice the direction of the relationship. The View reads from the Model when it draws. The View does not own the value, and it does not decide the value on its own. That division allows the same data to be presented in more than one way if the design calls for it.
The Controller Responds to Events
The Controller is the least tidy of the three realms when MVC is used for Swing components. The source explains that its responsibilities are often split among several objects rather than gathered into one. These can include mouse and keyboard listeners that respond to input on the View, action objects that respond to menu commands or buttons, and listeners for higher-level events such as those produced by a slider. Each piece responds to something happening, and each one ultimately changes the state of the component.
The typical pattern is that the Controller responds to an event by modifying the Model. The View is then updated only indirectly, because the Model has changed and the View reacts to that change. The Controller does not paint the knob. It changes the data, and the change is shown through a separate route.
How does the View learn that a redraw is needed? The source says that in Java this is typically handled with events and listeners. The Model is set up to generate events when its data changes, and the View registers itself as a listener for those events. When the Model changes, an event is generated, the View is notified, and the View updates its appearance. The Model does not need to know how the View draws things. It only announces that its data has changed, and the View decides how to respond.
Who Owns Which Part
| Realm | Owns | Does not do | Slider example |
|---|---|---|---|
| Model | The current state of the component | Draw anything or respond to mouse movement | Stores the volume value |
| View | Presentation: drawing the state on screen | Own the value or decide a new one on its own | Positions the knob from the stored volume |
| Controller | Responding to user and other events | Paint pixels directly | Reacts to a drag by changing the volume in the Model |
The three realms of an MVC component, based on the state, presentation, and event-handling distinctions
Tracing a Drag from Input to Screen
Now follow one complete update. The user drags the slider knob. The sequence below shows the order of messages. Read it from top to bottom: each arrow is a message sent after the one above it.
Tracing the volume through one drag
The Model holds volume 40. The user drags the knob up to 60. Trace which part does what, in order.
- 1Input arrives
The drag is caught by a listener in the Controller. The Controller does not draw the knob and does not store the number itself.
- 2Model changes
The Controller tells the Model to take the new value, 60. The Model's state is now 60.
- 3Event announces the change
Because its data changed, the Model generates an event. The View, registered as a listener, is notified.
- 4View redraws
The View reads 60 from the Model and moves the knob to match.
The value and the screen both end at 60. The user's input changed the Model first, and the View followed through the event. At no point did the drawing code set the volume.
Mistakes When Dividing Responsibilities
Common mistake Letting the View change the stored value during a drag
What it looks likeThe paint code sets the volume to the knob's pixel position.
Why it failsThe View then owns state, and a second copy of the value can drift away from the Model.
Do this insteadThe Controller responds to the drag and modifies the Model. The View reads the result.
Common mistake Keeping a private copy of the volume in the View
What it looks likeThe View stores its own volume field and updates it only sometimes.
Why it failsAny part of the component that needs the volume should ask the Model, because a separate copy could fall out of step.
Do this insteadThe View keeps a reference to the Model and reads the value when it paints.
Common mistake Making the Model aware of how things are drawn
What it looks likeThe Model computes pixel coordinates for the knob.
Why it failsThe Model should only hold data and announce changes. Drawing details belong to the View.
Do this insteadThe Model announces that its data changed, and the View decides how to respond.
Common mistake Assuming the Controller is always a single object
Sorting Responsibilities in a Simple Counter
A simple GUI component has a button labelled Add and a label that shows a count. Each click should increase the count by one. Decide which realm owns each responsibility: (a) storing the count, (b) drawing the number on screen, (c) interpreting the click as a request to add one. Explain each answer using the state, presentation, and event-handling distinctions.
Hints
- Ask first which part holds the number that persists between clicks.
- Ask which part is responsible for the words and digits that appear on screen.
- Ask which part responds to the mouse event, and which part should change the Model in response.
Key Takeaways
- MVC stands for Model-View-Controller, and each letter names one realm of responsibility in a graphical component.
- The Model owns state: it holds the current data and does not draw or handle input.
- The View owns presentation: it draws the state by consulting the Model and redraws when notified of changes.
- The Controller owns event handling: it responds to user input by modifying the Model, so input changes data first and the screen follows through events.