MVC Toni
MVC Toni
a Composed Pattern.
Toni Sellarès
Universitat de Girona
Model:
- Contains data.
Views and Controllers conform
- Encapsulates application state.
the User Interface.
- Responds to state queries/updates.
- Exposes application functionality.
Observe that:
• Both the view and the controller depend on the model.
• The separation between view and controller is secondary in many rich-client applications,
and, in fact, many user interface frameworks implement the roles as one object.
MVC: Benefits
• Supports multiple views. Because the view is separated from the model and there
is no direct dependency from the model to the view, the user interface can display
multiple views of the same data at the same time.
• Accommodates change. User interface requirements tend to change more rapidly
than business rules. Because the model does not depend on the views, adding new
types of views to the system generally does not affect the model. As a result, the
scope of change is confined to the view.
• Complexity. The MVC pattern introduces new levels of indirection and therefore
increases the complexity of the solution slightly. It also increases the event-driven
nature of the user-interface code, which can become more difficult to debug.
• Cost of frequent updates. Decoupling the model from the view does not mean
that developers of the model can ignore the nature of the views. If the model
undergoes frequent changes, it could flood the views with update requests. As a
result, the view may fall behind update requests. Therefore, it is important to keep
the view in mind when coding the model.
Looking at MVC through other patterns
MVC is set of patterns together in the same design:
– You can use its interface to add new songs, manage play lists
and rename tracks.
• Database of all your songs along with associated names and data.
• Plays the song.
• Updates the interface constantly with current song, running time and so
on
Controller
View
Controller asks
Model tells the view
Player model to
the state has
begin playing the
changed
class Player { song Controller
play ( ) { }
manipulates the
rip ( ) { }
model
burn ( ) { }
The model notifies }
the view of a change
in its state.
The model contains all the state,
Model data, and application logic needed
to maintain and play mp3s.
A Closer Look….
Lets see the nitty gritty details of how this MVC pattern works.
MODEL:
The model holds all the data,
CONTROLLER: state and application logic. The
VIEW: Takes user input and figures model is oblivious to the view
Gives you a presentation of the out what it means to the model. Here’s the and controller, although it
creamy
model. The view usually gets provides an interface to
controller;
the state and data it needs to manipulate and retrieve its
it lives in
display directly from the state and it can send
the middle.
model. notifications of state changes
to the Observer.
Observer
– The view is your window to the model. When you do something to the view (like click the Play button), then the view
tells the controller what you did. It’s the controller’s job to handle that.
– The controller takes your actions and interprets them. If you click on a button, it’s the controller’s job to figure out what
that means and how the model should be manipulated based on that action.
– When the controller receives an action from the view, it may need to tell the view to change as a result. For example, the
controller could enable or disable certain buttons or menu items in the interface.
(4) The model notifies the view when its state has changed:
– When something changes in the model, based on either some action you took (like clicking a button) or some other
internal change (like the next song in the playlist has started), the model notifies the view that its state has changed.
– The view gets the state it displays directly from the model. For instance, when the model notifies the view that a new
song has started playing, the view requests the song name from the model and displays it. The view might also ask the
model for state as the result of the controller requesting some change in the view.
class Foo {
void bar ( ) {
doBar();
}
} View
Model
Strategy
Controller
View
Controller
We can swap
another behavior for
the view by changing
the controller.
The view only worries about presentation, the
controller worries about translating user input to
actions on the model.
Composite
Composite
paint ()
Window
Set BPM
Set
<< >>
View
• The model makes use of the Observer pattern so that it can keep observers
updated, yet stay decoupled from them.
• The controller is the strategy for the view. The view can use different
implementations of the controller to get different behavior.
• The view uses Composite Pattern to implement the user interface, which
usually consists of nested components like panels, frames, and buttons.
• These patterns work together to decouple the three players in the MVC
model, which keeps designs clear and flexible.
• The Adapter pattern can be used to adapt a new model to an existing view
and controller.