15
MVC-1 Model View Controller Pattern – Behavioural Intent » Partition user-interactive applications into three parts > Model > View > Controller Motivation » Use divide and conquer to simplify interactions among the parts of a program

Model View Controller Pattern – BehaviouralMVC-2 Participants – 1 • Model » What your application is > Does computation, data manipulation and processing • Controller » How

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Model View Controller Pattern – BehaviouralMVC-2 Participants – 1 • Model » What your application is > Does computation, data manipulation and processing • Controller » How

MVC-1

Model View Controller Pattern – Behavioural

•  Intent» Partition user-interactive applications into three

parts> Model> View> Controller

•  Motivation» Use divide and conquer to simplify interactions

among the parts of a program

Page 2: Model View Controller Pattern – BehaviouralMVC-2 Participants – 1 • Model » What your application is > Does computation, data manipulation and processing • Controller » How

MVC-2

Participants – 1

•  Model

» What your application is>  Does computation, data manipulation and processing

•  Controller

»  How your model is presented to the user>  Connects the model with the view specifying what

operations from the model must be executed in response to what user events occur

•  View

»  The controller’s minions>  The graphical part of the application, presenting

information visually and interacting with users.–  Notions such as buttons, other controls and events belong

here

Page 3: Model View Controller Pattern – BehaviouralMVC-2 Participants – 1 • Model » What your application is > Does computation, data manipulation and processing • Controller » How

MVC-3

Participants – 2

Controller

Model View

Diagrams based on Stanford CS193p Fall 2011 slides

What your application is

How your model is presented to the user

The controller’s minions

Page 4: Model View Controller Pattern – BehaviouralMVC-2 Participants – 1 • Model » What your application is > Does computation, data manipulation and processing • Controller » How

MVC-4

Communications

It’s all about managing the communications

Controller

ViewModel

Model and view never talk directly to each other

Controller can always talk to the model and view

outlet

Page 5: Model View Controller Pattern – BehaviouralMVC-2 Participants – 1 • Model » What your application is > Does computation, data manipulation and processing • Controller » How

MVC-5

Communications – Model

Controller

ViewModel

Model has a radio station to broadcastchange notifications (subject in observer pattern)

Models can only communicate directly with other models

Controllers (and other models) tune into stations in which they are interested

Page 6: Model View Controller Pattern – BehaviouralMVC-2 Participants – 1 • Model » What your application is > Does computation, data manipulation and processing • Controller » How

MVC-6

Communications – View – 1 of 3

Target method

Action – button, gesture

Controller

ViewModel

Page 7: Model View Controller Pattern – BehaviouralMVC-2 Participants – 1 • Model » What your application is > Does computation, data manipulation and processing • Controller » How

MVC-7

Communications – View – 2 of 3

Controller sets itself as view’s delegate

View asks controller to dowork, indirectly as its delegate,and notifies the controller whenthe view changes

Controller

ViewModel

should, will, did

Page 8: Model View Controller Pattern – BehaviouralMVC-2 Participants – 1 • Model » What your application is > Does computation, data manipulation and processing • Controller » How

MVC-8

Communications – View – 3 of 3

Controller sets itself as view’s data source

View asks controller for the data it needs

Controller

ViewModel

data, at, count

Page 9: Model View Controller Pattern – BehaviouralMVC-2 Participants – 1 • Model » What your application is > Does computation, data manipulation and processing • Controller » How

MVC-9

Multiple MVC patterns

Controller Controller

Model

Controller

ViewModel

Controller

View

Controller

ViewModel

Controller

ViewModel

Page 10: Model View Controller Pattern – BehaviouralMVC-2 Participants – 1 • Model » What your application is > Does computation, data manipulation and processing • Controller » How

MVC-10

Hello World Example

Spaceling

No model

sayHelloButton

Page 11: Model View Controller Pattern – BehaviouralMVC-2 Participants – 1 • Model » What your application is > Does computation, data manipulation and processing • Controller » How

MVC-11

Calculator Example

delegate for “back” buttondata source

Calculator_view_controller Function_graph_vc

Calculator

Page 12: Model View Controller Pattern – BehaviouralMVC-2 Participants – 1 • Model » What your application is > Does computation, data manipulation and processing • Controller » How

MVC-12

Participants

•  ModelHas all the data and methods for maintaining the data

•  ViewDisplays relevant data, accepts user actions and notifies controller

•  Controller» Reacts to changes in the model to instruct how the

view is to change» Reacts to view “actions” to instruct the view how

to change and to instruct how the model is to change

Page 13: Model View Controller Pattern – BehaviouralMVC-2 Participants – 1 • Model » What your application is > Does computation, data manipulation and processing • Controller » How

MVC-13

Applicability

•  Use MVC in all interactive applications

Page 14: Model View Controller Pattern – BehaviouralMVC-2 Participants – 1 • Model » What your application is > Does computation, data manipulation and processing • Controller » How

MVC-14

Consequences

•  The accepted way to deal with interactive applications

Page 15: Model View Controller Pattern – BehaviouralMVC-2 Participants – 1 • Model » What your application is > Does computation, data manipulation and processing • Controller » How

MVC-15

Related Patterns

•  MVC is a special case of the Mediator pattern» The controller is the mediator between the model

and the view

•  MVC has similarities to the Observer pattern.