Lecture Week 11 Model-View-Controller
Lecture Week 11 Model-View-Controller
Lecture Week 11 Model-View-Controller
Model-View-Controller
1
1. The MVC pattern
The Model-View-Controller (MVC) architecture is the
basis for all modern GUI systems. It is designed to
• separate the GUI from the domain model
• separate the control of the GUI from the visible display
• allow multiple views to be updated when needed
• achieve low coupling and high cohesion
• achieve clean code and high reuse
2
2. Basic communication
1. The user generates an event. The controller receives the
event and calls methods in the model object.
4. update() gets data from the model and updates the view.
command
Model
query
Controller
event
update data
View
3
Code outlines
A controller responds to view events and calls model
methods. It may also access the view to get new values and
change the view (such as clearing a text field).
class Model
{ private LList<View> views;
4
3. Building the system
The process to build an MVC system may be summarised
as MMMVCVCVC.
Model
view view
Controller
model model
5
The Observer pattern
A model updates its views by calling update().
class Viewable
{ private LList<View> views =
new LList<View>();
All the model does is call update(), so you can change the
GUI completely with no change at all to the model!
6
4. Putting it all together
A model
A model class looks like
7
A view
public class ViewA implements View
{ private A a;
private view attributes;
public ViewA(A a)
{ this.a = a;
setup();
build();
...
add(new ViewB(a.b)); }
...
8
A controller
A controller looks like
public ListenerA(A a)
{ this.a = a; }
Common errors
The symptom is that nothing changes when you click on
the button. The probable cause is
9
5. A concert booking system
This system sells seats for a concert. The GUI has a panel
for each group of seats; it looks like
10
viewer classes
public abstract interface View
{ public abstract void update();
}
class Stadium
public class Stadium
{ private LL<Group> seats = new LL<Group>();
public Stadium()
{ seats.add(new Group("Back", 2000, 35.00));
seats.add(new Group("Middle", 4000, 60.00));
seats.add(new Group("Front", 200, 300.00));}
}
11
class Group
12
The root class
The root class creates the stadium and passes it to the window.
13
class Panel
The top-level panel (the panel that covers the window)
adds a group panel for each group.
public class Panel extends JPanel
{
public Panel(Stadium stadium)
{ setup(stadium.groups());
build(stadium); }
14
class GroupPanel
The group panel stores (a pointer to) the group, attaches
itself to the group, and creates a listener for the button.
public class GroupPanel
extends JPanel implements View
{ private JTextField sold = new JTField(5);
private JTextField left = new JField(5);
private JTextField sell = new JField(5);
private Group group;
15
public void update()
{ sold.setText(group.sold() + "");
left.setText(group.left() + "");
sell.setText(""); }
16
Input validation
The controller can change the view directly. Consider the
case where the user enters an invalid value, such as too many
seats to sell. The controller checks that there are enough seats
before calling sell, and changes the view to tell the user.
Here, I just show an error message in the input text field.
17