organizing presentation logicctchen/pdf/opl.pdf · model view presenter (mvp) separates the...

63
1 Chien-Tsun Chen Department of Computer Science and Information Engineering National Taipei University of Technology, Taipei 106, Taiwan [email protected] Otc. 25 2004 Organizing Presentation Logic

Upload: others

Post on 15-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

1

Chien-Tsun ChenDepartment of Computer Science and Information EngineeringNational Taipei University of Technology, Taipei 106, Taiwan

[email protected]. 25 2004

Organizing Presentation Logic

Page 2: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

2

This talk introduces some common issues of developing GUI applications and provides an example to give the MVP pattern a try

Why presentation design is harder than design of other layers?

A SWT/JFace application is developed to show the power of the MVP pattern

Page 3: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

3

ReferencesMartin Fowler, Organizing Presentation Logic, 2004, http://www.martinfowler.com/eaaDev/OrganizingPresentations.html.Todd Sundsted, MVC meets Swing, Javaworld, 1998.Mike Potel, MVP: Model-View-Presenter. The TaligentProgramming Model for C++ and Java, 1996.Michael Feathers, The Humble Dialog, 2002.Jason Cai et. al., HMVC: The Layered Pattern for Developing strong client tiers, Javaworld, 2000.W. Greg Phillips and T.C. Nicholas Graham, Software Architectures for Multiuser Interactive Systems

Page 4: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

4

Common issues of developing GUI applications

Page 5: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

5

We use Album to discuss the issues of organizing basic behaviors within a single window

The initial state of Album application

Check Classic button enable the Composer

Page 6: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

6

Other Other PackagesPackages

The global picture: An application can be divided into three (coarse-grained) layers

Presentation Presentation Layer Layer

Domain Domain Layer Layer

Data Source Data Source LayerLayer Transaction Transaction

Management Management

MessagingMessagingSystemSystem

DatabaseDatabase

Application External Systems

Users

session state record statescreen state

Page 7: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

7

There are several ways to split up the logic of the presentation, such as MVC & PAC

MVC & HMVC

PAC

PAC & PAC Hierarchy

Page 8: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

8

However, the well-known MVC approach doesn’t work well with most GUI frameworks

separating the model from the view/controller

separating view from controller was not popular in rich client GUI framework

Most GUI framework are designed so that the UI controls both display and receive the user input events

Page 9: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

9

Let’s look at a Swing JButton example: each widget is a “UI component”

Swing UI Component

AbstractButton

setUI()

JComponent

setModel()

AbstractButton

Page 10: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

10

AbstractButton declares most of the Button’s behaviors and variables related to UI painting

Not all UI states are defined in the model

Page 11: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

11

ButtonModel and ComponentUI

AbstractButton

JComponent

Page 12: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

12

Data is copied between layers and applications have to coordinate and synchronize the data between layers

Domain Layer(session state)

Presentation Layer(screen state)

External Systems(record state)DatabaseDatabase

Page 13: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

13

Frequency and granularity are major design issues when synchronizing between layers

Key synchronization

Field synchronization

Screen synchronization

Coarse-grained synchronization: changes the entire UI whenever anything is changed

Fine-grained synchronization: changes the fields that really need updating

Page 14: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

14

The concept of active screen is used to denote that the screen state is not synchronized to the session state

Sequential active screen

Single active screen

Multiple active screens

1

2

3

Page 15: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

15

In “direct synchronization”, parent and child communicate directly to synchronize their changes

direct synchronization

child session state parentchange

save

save

*changed

load

Page 16: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

16

In “mediated synchronization”, child communicates with mediator (a shared state) and separately the parent synchronizes itself with the mediator

child session state parent

change

save

*changed

load

mediated synchronization

Page 17: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

17

Organizing presentation by MVP

Page 18: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

18

Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

viewpresenter

titleFieldChanged

titleBar.text = “Album:” + titleField.text

change title filed

view

1

23

sequence diagram

presenter

Page 19: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

19

The first (simple) implementation of MVP: presenter and view are strongly coupled

view

selectedChangedEvent

m_view.m_txtTitle.setText()

select Album list

presenter

m_view.m_lstAlbum.getSelectedIndex()m_albums.getTitle()

Page 20: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

20

The second implementation of MVP: presenter and view are loosely coupled

view

selectedChangedEvent

IAlbumView.setTitle()

select Album list

presenter

IAlbumView.getSelectedIndex()m_albums.getTitle()

Page 21: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

21

Before implementing, we review the “interface”of the view

getAlbums

setAlbums

getArtist setArtist

getTitle

setTitle

getClassical

setClassical

getComposer setComposer

setEnableApplyAndCancel

setSelectedAlbumIndex

getSelectedAlbumIndex

getWindowTitlesetWindowTitle

Page 22: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

22

We apply test first programming in the second implementation to find the interface of view

Test the initial state of view

Page 23: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

23

To run the test, we have to create AlbumPresenter and MockAlbumView

Page 24: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

24

Now we can run the test but it failed. We should write some code in the MockAlbumView

Page 25: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

25

The getAlbums() and setAlbums() is implemented to pass the test

before

after

Page 26: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

26

The test passed. We can continue to write more tests

Page 27: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

27

When Eclipse finds a undefined method, it can create the method for you

Page 28: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

28

We run the test and it falls again, of course. A lot of works need to do…

Page 29: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

29

Finally, I finished the tests, MockAlbumView, and Presenter

MockAlbumViewMockAlbumView

PresenterPresenter

TestsTests

Page 30: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

30

Next step, creating interface IAlbumView

Page 31: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

31

I extract an interface from MockAlbumView by using “Extract Interface”

Page 32: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

32

Implement AlbumViewImp by copying code from AlbumView of the first example

copy & modify

Page 33: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

33

I write a test for AlbumViewImp by coping the test for MockAlbumView with little modification

copy & modify

Page 34: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

34

The VM arguments must be set to run the test of AlbumViewImp, which is a SWT/JFace program

Page 35: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

35

As you expected, the test fail

Page 36: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

36

Why the test fail?

When initial,composer should be disabled

Find a bug of Presenter

Page 37: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

37

The test still fail !

Page 38: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

38

Why the test still fail?

Apply & Cancel should be disabled

The Presenter does not set up the initial value of the view.Why the tests for MockAlbumView do not find this bug?

Page 39: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

39

I fixed the bug, but another bug occurs in the test method: testListSelectedShouldCauseUpdate()

Page 40: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

40

The bug is caused by AlbumViewImp

The problem is that the org.eclipse.swt.widgets.List does not fire event when we call the setSelection(int) method

Add this statement

Page 41: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

41

All done. The test pass

Page 42: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

42

Conclusion

Question?

Organizing your application so that it is easy to implement as well as testing

MVP not only separates the behavior of a presentation from the view but also supports presentation behavior testing without the view

Page 43: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

43

MoclAlbumView (1/2)

Page 44: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

44

MoclAlbumView (2/2)

Page 45: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

45

Presenter

Page 46: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

46

AlbumPresenterTest is used to test Presenter with MockAlbumView

Page 47: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

47

Test Initial state of view (screen state)

Page 48: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

48

List selected should cause update

Page 49: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

49

Check the classical box should enable composer field

Page 50: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

50

The most complex test case. Step 1

Domain layer Domain layer ((session statesession state))

==

F F

FF

0Bug

When initial, the screen state == session state

State 0

Page 51: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

51

Test code of Step 1: screen state == session state

Page 52: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

52

Step 2: screen state != session state

F F

FF

0

T T

TT

0

State 0

State 1Domain layer Domain layer

((session statesession state))!=

Domain layer Domain layer ((session statesession state))

==

Page 53: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

53

Test code of Step 2: screen state != session state

Page 54: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

54

Step 3: At state 3, screen state should equal to session state but I forget to check at state 2

T T

TT

0

F F

TT

3

State 1

State 3

State 2

Domain layer Domain layer ((session statesession state))

??

Page 55: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

55

Test code of Step 3: screen state is checked but it is not checked with session state

Page 56: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

56

Step 4: Go be to item 0 and make sure the view state == session state

F F

TT

3

State 3

Domain layer Domain layer ((session statesession state))

==

State 4F F

TT

0

F F

FF

0

State 0

Page 57: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

57

Test code of Step 4: screen state == session state

Page 58: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

58

Extract Interface from MockAlbumView (1/6)

Page 59: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

59

Extract Interface from MockAlbumView (2/6)

Page 60: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

60

Extract Interface from MockAlbumView (3/6)

Page 61: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

61

Extract Interface from MockAlbumView (4/6)

Page 62: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

62

Extract Interface from MockAlbumView (5/6)

Page 63: Organizing Presentation Logicctchen/pdf/OPL.pdf · Model View Presenter (MVP) separates the behavior of a presentation from the view while allowing the view to receive user events

63

Extract Interface from MockAlbumView (6/6)