23
1 Model-view-controller An architecture for UI

model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

1

Model-view-controller

An architecture for UI

Page 2: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 2

The flow of information (idealized)

OS

Application

Controller

Model

View

0 Event 1 Changes

2 State

3 Pixels

Flow of information

Page 3: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 3

Responsibilities

n Model: Holds state information.q It models the user’s conception of what it is that

they are manipulating or viewing.q Should align with the user’s mental model.

n View: Presents a visualization of the models state.q Views are usually stateless, but might include

“view state” such as zoom level, scroll position, selection or highlighting, caret position.

Page 4: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 4

Responsibilities

n Controller: Interprets UI events (mouse events, keyboard events, screen touches, etc.)q Turns UI events into changes to the model (and

sometimes view state).

Page 5: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 5

MVC Encourages

n Separation of presentation from representation.

n Separation of view from control.q Allows these components to be independently

extended and, perhaps, reused.

Page 6: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 6

The flow of information (idealized)

OS

Application

Controller

Model

View

0 Event 1 Changes

2 State

3 Pixels

Flow of information

Page 7: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 7

Flow of information

n Often the flow is more complicated because1. The underlying GUI system associates events

with view objects.n E.g. in AWT/Swing. Events are routed through the GUI

component the user directs them at.2. The controller may need to know the model’s

state3. Some events affect only the view and so should

not go through the model.n E.g. Scrolling, cursor position, selection.

Page 8: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 8

The flow of information (more realistic)

Controller

Model

View

0 Event

1 Changes to model state

2 State3 Pixels User

EventChanges to view state

State

(push)(push)

(pull)

(push)

(pull)

Page 9: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

n The View uses the Controller as a strategy to help it deal with input events

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 9

Strategy

Controller

View

0.0 Event(push)

0.0.0 Changes to view state (push)

0 Event

n For reusability, they usually know each other only via interfaces.

Messages

Page 10: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

n The Controller observes the Model so that it is aware of relevant changes to state.

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 10

Observer

Controller

Model

0.0.1 Changes to model state

0.0.1.0.0 get state

0.0.1.0 update

Page 11: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

n The View also observes the Model so that it is aware of relevant changes to state.

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 11

Observer

Controller

Model

View

0.0.1 Changes to model state

0.0.1.1 update

0.0.1.1.0 get state

Page 12: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

n The composite pattern is often used in any of the three parts. The model may use Façade.

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 12

Composite and Façade

Page 13: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

n Controllers and models are often state machines and may use the state pattern.

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 13

State pattern

State pattern:

Object-orientedImplementation of state machine.Each state is represented by a different class (all with the same interface---strategy pattern).

Page 14: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 14

Advantages

n Clean separation of presentation (view) from domain modelling (model).

n Clean synchronization. The observer pattern helps keep all views and controllers in sync with the data.

Page 15: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 15

Advantages

n Separation of view from control.q The view is typically platform dependent. And

events that come to it are typically defined by the platform (e.g. Swing).

q By separating the controller you can reuse the controller independently of the view.

Page 16: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

Case study: The Rat Race game.

n Model keeps a map of a maze, with a cheese and a rat.q The model’s interface is in terms of “world

coordinates”n View draws the maze, cheese, and rat.

q The world—view transformation is a secret of the view.

q The view must then translate mouse events to world coordinates.

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 16

Page 17: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

Case study: The Rat Race game.

n Controllerq Forwards mouse events from the View to the

Model.q Sends periodic “pulse” events to the model so

that it is animated.

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 17

Page 18: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

Model and view: Observer

n The model and view relate by the observer pattern

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 18

Page 19: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

Controller and View: Strategy/Listenern The controller listens to the view for events

and propagates them to the model.n It also produces pulse events, on its own.

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 19

Page 20: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

What’s missing

n In this case, there was no need to have the controller observe the model

n And there is no need for the controller to send messages to the view (after registering as a listener).

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 20

Page 21: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

Is the controller needed?n In simple cases the controller is just

forwarding information from the view to the model. So is the controller needed?q If the view just sent change messages directly to

the model, it would have two responsibilities (display and control), which makes it more complicated.

q Also the view would be more tightly bound to the model, which makes it less reusable.

q We might not want one controller per view.q Independent testability.

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 21

Page 22: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

Sources and further reading

n Martin Fowler has an interesting article on styles of UI architectureq http://martinfowler.com/eaaDev/uiArchs.html

n Head-First Design Patterns by Freeman, Robson, Bates, and Sierra has a good chapter on MVC

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 22

Page 23: model-view-controller · 2018-03-05 · with view objects. n E.g. in AWT/Swing. Events are routed through the GUI component the user directs them at. 2. The controller may need to

Variations and alternativesn Trygve Reenskaug and James O. Coplien

q have an interesting article on what they call DCI (Domain, Context, Interaction). This is not so much on UI design as a challenge to a lot of (bad) OO design.

q http://www.artima.com/articles/dci_vision.html

n Mike Potel describes the Model-View-Presenter q http://www.wildcrest.com/Potel/Portfolio/mvp.pdf

n Martin Fowler on Presentation Modelsq http://martinfowler.com/eaaDev/PresentationModel.html

n MF on Passive View and Supervising Controllerq http://martinfowler.com/eaaDev/PassiveScreen.htmlq http://martinfowler.com/eaaDev/SupervisingPresenter.html

© 2003 T. S. Norvell Engineering 5895 Memorial University Graphics & Animation. Slide 23