isp666 mvc & design patterns. outline review event programming model model-view-controller...

23
ISP666 MVC & Design Patterns

Upload: bernice-harrell

Post on 31-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

ISP666

MVC & Design Patterns

Outline

• Review Event Programming Model

• Model-View-Controller

• Revisit Simple Calculator

• Break

• Design Patterns

• Exercise

Event-driven programming

• It gives user full control• For application programmer, it is about

two things:– Registering event listeners– Writing event-handling code for the

listeners• Caution

– Keep your event-handling code short and run fast

• Why?

How to connect application logic to GUI?

• Model: information the application is dealing with– e.g. Chips, circuits in case of a ECAD tool– e.g. Student, class data in case of a class

registration system

• View: visual display of the model– Diagrams, images– Tables– Buttons, menus

• Controller: connecting the two

Model-View-Controller (MVC)

•Model and View must be separated!•Same model may have multiple views

•E.g. A demographic data may be displayed as a table or a graph•Same view may be used to deal with multiple Models

•Code reuse

•View and controller might be combined

MVC

0%

10%

20%

30%

40%

50%

60%

a b c

a 50%b 30%c 20%

50%

30%

20%

a

b

c

a = 50%b = 30%c = 20%

model

Model Class

• Keep references to all its views– May not be necessary if all the possible

changes of model are from view

• Notify all its views if its value changed– Could be handled in views and controller if

all the change come from view

Model Class

• Keep a reference to its model

• Notify the model when it is changed by the user action

Controller

• Translate user action event to method invocation on model

• Its functionality may spread in multiple classes– In Event-handlers

Simple Calculator

• Model: a math expression and a result• View: strings in text box, buttons• Controller: event handling code for key

buttons and text boxes

12*(7.5-1)+5

7 9

4

0

21

5

3

6

8

/

*

+

-

( )

. =

Clear

MVC Discussion

• A well architectured GUI application is said to always separate View and Model

• Discuss – Will it make the code shorter?– What are the benefits?

break

Interaction among Objects

• Can be very complicated– Composite, aggregate, inheritance

• We also strive to achieve– Reusability of code– Extensibility (flexibility) of our system

• Ad hoc solutions may not work well

Design Patterns

• Famous book of the Gang of Four (aka GoF book)– Gamma, Helm, Johnson & Vlissides– “Design Patterns – Elements of Reusable

Object-Oriented Software”, 1995

• Elegant solutions to recurring problems in Object-Oriented software design

Type of Patterns

• Creational Pattern– Object creation

• Structural Pattern– Composition of classes and objects

• Behavioral Pattern– Characteristic of object interaction

UML Class Diagram Notation

Abstract Factory Pattern

• Application does not need to know which kind of button should be drawn (Motif Button, or Windows Button)

• It just draw a button, system will draw the current button according to current look-and-feel setting.

• Creational

Change look-and-feel with one line of code, how?

Abstract Factory

Adaptor Pattern

• Aka. Wrapper Pattern• Let different classes conform to the

same interface• Structural

There's an existing TextView class that you want to add it to your graphic editor program as a Shape object (like LineShape, PolygonShape, etc.), but TextView is not created with Shape interface in mind. What to do?

Class Adaptor

Object Adaptor

Strategy Pattern

• Aka. Policy• Define a family of algorithms, and make

them interchangable for clients that use it

Several different line-breaking algorithms are available for a editor you are developing, e.g. HtmlLineBreaker, TexLineBreaker, ArrayLineBreaker, etc. Now you don't want rendering code to explicitly ask for an algorithm (for the sake of MVC) but still get an appropriate algorithm given the type of the content. How to achieve that?

Strategy

Exercise

• Let's design an arcade game, say, a “bombing the enemy submarines” game.– You control a surface ship to bomb enemy

submarines below– Enemy submarines will fire back, there are

different types of subs, firing different missiles at you

• It should be played on the Web, on cell phone, or download to the computer for offline play.

• More content can be added by user through scripting.