l10: model-view-controller general application structure. user interface: role, requirements,...

24
L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable. Implementation problems and strategies in Java and C++.

Upload: rodney-green

Post on 18-Jan-2018

222 views

Category:

Documents


0 download

DESCRIPTION

3 But What Does It Look Like? No Separation Partitioned Separated

TRANSCRIPT

Page 1: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

L10: Model-View-Controller• General application structure.• User Interface: Role, Requirements, Problems• Design patterns:

Model – View – Controller, Observer/Observable.

• Implementation problems and strategies in Java and C++.

Page 2: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

2

Some domain-specific

code

General Structure

An application generally consists of:

Some user-interface code

Some platform-specific code

Page 3: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

3

But What Does It Look Like?

No Separation Partitioned Separated

Page 4: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

4

Role of the User Interface

1.To represent the domain model to the user2.To allow the user to control the model.

NOT to be part of the model.

Page 5: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

5

User Interface Requirements

• Represent objects of interest.• Provide alternative representations.• Allow control via different mechanisms.

• Implies that the user interface must be loosely-coupled to the application model.

Page 6: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

6

Potential Problems

1. How does the controller part of the interface ‘know’ when an interaction is requested? (Event-driven programming)

2. What happens if the model is (sometimes) CPU intensive? (Threads)

Page 7: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

7

Decoupling

• A flexible, loosely-coupled arrangement is appropriate for this separation of application model and user interface.

• The Model-View-Controller design pattern derived from Smalltalk is a useful construct.

Page 8: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

8

Design Patterns (1)

• “Generally, descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context.” -- Design Patterns, p3.

Page 9: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

9

Design Patterns (2)

• The intention is not necessarily to reuse the code that implements a design pattern wholesale, but rather to understand the solution mechanism so well that writing new implementations of the pattern can be done efficiently.

Page 10: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

10

Model-View-Controller

• A design pattern consisting of a triad of classes:–Model - the application object,– View - the screen presentation of the object,– Controller - defines the way the user interface

reacts to input.• Classes are decoupled by defining a subscribe/

notify protocol.• Multiple views and controllers can be defined.

Page 11: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

11

Model-View-Controller

Relies on an Observer mechanism (another design pattern) to connect the classes.

A View must ensure that it accurately and completely reflects the current state of the Model.

The Model notifies all Views that depend on it.

The Controller modifies the state of the object through its programming interface in response to user actions.

Page 12: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

12

Advantages

• Multiple views can be added to an object.• Multiple control mechanisms can be

implemented.• Presentation and control of the model can

be changed without rewriting the model itself.

Page 13: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

13

Why Is This Useful?

• It allows application-specific code to be localized.

• It allows the view to be different from the model.

• It allows multiple differing views of the same model.

• It allows the mechanism for control to be separate from both the view and the model.

Page 14: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

14

MVC in Java

Java.util.Observable - a base class that incorporates the observer mechanism.

Java.util.Observer - an interface that can be implemented which notices when objects that it is observing change state.

The Swing user interface classes use the Model-View-Controller pattern as a basis.

Page 15: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

15

Implementation in Java

• Model classes extend Observable.• View classes extend Applet and implement

Observer.• Each View must :

– implement the update() method and– be added to the Model instance via addObserver().

• When the Model calls notifyObservers(), the update() method of all observing Views will be called.

Page 16: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

16

Implementation in C++

• No explicit code for Observer/Observable mechanisms.

• View and Controller interact with the Model via normal ‘Get’ and ‘Set’ functions.

• Some libraries allow ‘callback’ functions, e.g. OpenGL™ and qt.

• For an alternative view, see outerface (http://www.outerface.com/).

Page 17: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

17

Modelcreates

createsView

createsController

Who creates Model?Version 1: App

App

Page 18: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

18

Version 1: App

• Model really is model and nothing else.• App is responsible for registering any number

of observers/controllers.• M + V + C components can all be controlled

directly from App (good???).• Have to create M, then V, then register, then

update M.• Make model constructor responsible for

adding views?

Page 19: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

19

createsView

createsController

createsModel

Who creates Model?Version 2: Controller

App

Page 20: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

20

Version 2: Controller

• Controller automatically gets a reference to the model.

• Becomes responsible for initiating all state changes to model (including creation).

• Has to surrender a reference to the model to let the view be added as an observer.

• Ends up being responsible for adding observers to model (not ideal).

• Implies only one controller.

Page 21: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

21

createsView

createsController

createsModel

Who creates Model?Version 3: View

App

Page 22: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

22

Version 3: View

• View responsible for adding itself as observer to model.

• Implies only one view.• Multiple views might share reference to

model rather than each create one.

Page 23: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

23

When View is Controller

• For example, sliders and text fields.• Situation: View has to become

Observable as well as Observer.• Problem: Multiple inheritance.• Possible Solution: Don’t treat view-

controllers as views — tie the “view” part of the controller to another part of the controller rather than to the model.

Page 24: L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable

24

Summary

• Flexible decoupling of user interface and application is essential.

• Model-View-Controller is a useful design pattern.

• Java is amenable to MVC implementation through the Observer/Observable classes.

• Complete decoupling is not always possible, or may lead to inelegant code.