Model-View-Controller

Download Report

Transcript Model-View-Controller

Model-View-Controller

Ku-Yaw Chang [email protected]

Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

Outline

Background Model-View-Controller  Context      Problem Solution Structure CRC Cards Dynamics Variants Known Uses Conclusion 2004/11/16 Pattern-Oriented Design 2

Background

Today’s systems  A high degree of user interaction, with help of graphical user interfaces.

The challenges  Keep the functional core user interface independent of the The core is based on the functional requirements  Usually remains stable User interfaces are often subject to change and adaptation 2004/11/16 Pattern-Oriented Design 3

Background

Two architectural patterns for interactive systems (from POSA1)  Model-View-Controller pattern (MVC)  Presentation-Abstraction-Control pattern (PAC) 4 2004/11/16 Pattern-Oriented Design

Model-View-Controller

Divide an interactive application into three components    Model Core functionality Data Views Display information Controllers Handle the user interface A change-propagation mechanism  Consistency between the user interface and the model 2004/11/16 Pattern-Oriented Design 5

An Example: Political Elections

2004/11/16 Pattern-Oriented Design 6

Context

Interactive applications with a flexible human-computer interface 2004/11/16 Pattern-Oriented Design 7

Problem

User interfaces are especially prone to change requests.

Different user place conflicting requirements on the user interface.

Building a system with the required flexibility is expensive and error-prone if the user interface is tightly interwoven with the functional core.

2004/11/16 Pattern-Oriented Design 8

Problem

The following

forces

influence the solution     The same information is presented differently in different windows, for example, in a bar or pie chart.

The display and behavior of the application must reflect data manipulations immediately.

Changes to the user interface should be easy, and even possible at run-time.

Support different ‘look and feel’ standards or porting the user interface should not affect code in the core of the application.

2004/11/16 Pattern-Oriented Design 9

Solution

Model-View-Controller (MVC)  First introduced in the Smalltalk-80  Divide into the three parts Processing Output Input 2004/11/16 Pattern-Oriented Design 10

Solution

Model-View-Controller (MVC)    Model Encapsulate core data and functionality  Independent of specific output representations or input behavior View Display information to the user Obtain data from the model Multiple views of the model Controller Receive input  Events that encode mouse movement, activation of mouse buttons, or keyboard input 2004/11/16 Pattern-Oriented Design 11

Structure

Class-Responsibility-Collaborator (CRC) Cards 2004/11/16 Pattern-Oriented Design 12

Structure

2004/11/16 Pattern-Oriented Design 13

Dynamics

Scenario I

2004/11/16 Pattern-Oriented Design 14

Dynamics

Scenario II

2004/11/16 Pattern-Oriented Design 15

Variants

Document-View  Relax the separation of view and controller In several GUI platforms, window display and event handling are closely interwoven.

Combine the responsibilities of the view and the controller from MVC in a single component  Sacrificing exchangeability of controllers 2004/11/16 Pattern-Oriented Design 16

Known Uses

Smalltalk MFC  Integrated in the Visual C++ environment CDocument CView ET ++ 2004/11/16 Pattern-Oriented Design 17

MVC    Model Data Core functionality View Output Controller Input

Conclusion

Document-View   Document Data Core functionality View Input Output 2004/11/16 Pattern-Oriented Design 18