Docsity

Model View Controller and Interactive Applications Programming | COMPSCI 160, Study notes of Computer Science

Partial preview of the text

Download Model View Controller and Interactive Applications Programming | COMPSCI 160 and more Study notes Computer Science in PDF only on Docsity!

10/13/

CS 160: Lecture 13

Professor John Canny

Fall 2004

10/13/

Outline

Model-view controller* Why do we need it?* Changing the display* Event flow* Dragging at interactive speeds

Interactive application programming* Callbacks* Multi-threaded programming

  • 10/13/
  • Example Application Blue circles: 4Cardinal squares:

10/13/

Model



Information the app is trying to manipulate 

Representation of real world objects

  • circuit for a CAD program
  • logic gates and wires connecting them
  • shapes in a drawing program
  • geometry and color

Model

View Controller

10/13/

Multiple Views

Blue circles: 4Cardinal squares: 2

10/13/

View



Implements a visual display of the model 

May have multiple views

  • e.g., shape view and numerical view 

Any time the model is changed, each viewmust be notified so that it can change

later

  • e.g., adding a new shape

Model

View Controller

10/13/

Controller

Blue circles: 4Cardinal squares: 2

10/13/

Controller

Blue circles: 4Cardinal squares: 2

10/13/

Combining View & Controller



View and controller are tightly intertwined

  • lots of communication between the two 

Almost always occur in pairs

  • i.e., for each view, need a separate controller 

Many architectures combine into a singleclass

Model

View Controller

10/13/

Why MVC?



Combining MVC into one class or using globalvariables will not scale

  • model may have more than one view
  • each is different and needs update when model

changes



Separation eases maintenance

  • easy to add a new view later* new model info may be needed, but old views still

work

  • can change a view later, e.g., draw shapes in 3-d

(recall, view handles selection)

10/13/

Adding Views Later

Blue circles: 4Cardinal squares: 2

10/13/

Changing the Display



How do we redraw when shape moves?

10/13/

Erase w/ Background Color and

Redraw

Blue circles: 4Cardinal squares: 2

10/13/

Changing the Display



Erase and redraw

  • using background color to erase fails* drawing shape in new position loses ordering 

Move in model and then redraw view

  • change position of shapes in model* model keeps shapes in a desired order* tell

all

views to redraw themselves in order

  • slow for large / complex drawings
  • flashing!