leverageing the model-view-presenter pattern in rich ... · this.view = view; public void...
TRANSCRIPT
© 2008 Syntax Consulting, Inc; All rights reserved.
Leveraging the Model-View-Presenter Pattern in Rich ClientApplications
Patrick PaulinEclipse RCP Trainer and ConsultantRCP Quickstart
www.rcpquickstart.com/training/presentations/mvp-and-rcp-ew2008
© 2008 Syntax Consulting, Inc; All rights reserved.
Agenda
A brief history lessonPresenter pattern in detailMVP and RCPRAP / RCP dual use demo
© 2008 Syntax Consulting, Inc; All rights reserved.
UI architecture
Forms and controlsModel-View-Controller (MVC)Model-View-Presenter (MVP)
© 2008 Syntax Consulting, Inc; All rights reserved.
Model / View separation
© 2008 Syntax Consulting, Inc; All rights reserved.
Forms and controls
Text name = new Text(parent, SWT.BORDER);name.addModifyListener(new ModifyListener()
public void modifyText(ModifyEvent e) {model.setName(nameText.getText());
}});
© 2008 Syntax Consulting, Inc; All rights reserved.
Model-View-Controller (MVC)
© 2008 Syntax Consulting, Inc; All rights reserved.
What is a controller, anyway?
© 2008 Syntax Consulting, Inc; All rights reserved.
Model-View-Presenter (MVP)
© 2008 Syntax Consulting, Inc; All rights reserved.
Agenda
A brief history lessonPresenter pattern in detailMVP and RCPRAP / RCP dual use demo
© 2008 Syntax Consulting, Inc; All rights reserved.
What is MVP?
© 2008 Syntax Consulting, Inc; All rights reserved.
Why MVP?
Manage complexityFlexibilityTestability
© 2008 Syntax Consulting, Inc; All rights reserved.
Managing complexity
© 2008 Syntax Consulting, Inc; All rights reserved.
Flexibility
© 2008 Syntax Consulting, Inc; All rights reserved.
Testability is hard
UI testing is hardTooling isn’t there yetUnit tests must be easy and quick to run
“Any object that is difficult to test should have minimal behavior.”
- Martin Fowler
© 2008 Syntax Consulting, Inc; All rights reserved.
Acceptance testing
public class MyPresenter {
setProperty(String property) getValidationMessages()}
public class MyPresenterTest {
@Test testUseCase() { MyPresenter p = new MyPresenter();
p.setProperty(“value”); assertEquals(0, p.getValidationMessages(). size()) }}
© 2008 Syntax Consulting, Inc; All rights reserved.
MVP and data
© 2008 Syntax Consulting, Inc; All rights reserved.
MVP and logic
public class ExamplePresenter {
public void setName(String name)
public boolean getOkEnabled()
}
© 2008 Syntax Consulting, Inc; All rights reserved.
MVP continuum
© 2008 Syntax Consulting, Inc; All rights reserved.
Push vs pull
© 2008 Syntax Consulting, Inc; All rights reserved.
Passive view testing
© 2008 Syntax Consulting, Inc; All rights reserved.
Sample test case
public class ExamplePresenter {
private IView view;
public ExamplePresenter(IModel model, IView view) {
this.view = view;
public void setName(String name) this.view.setOkEnabled(name !=
null);
public class ExamplePresenterTest {
protected void testSetName() { // create mock model and view
ExamplePresenter presenter = new ExamplePresenter(mockModel, mockView);
presenter.setName(“Frank”);
assertTrue(mockView.isOkEnabled());
Presenter Presenter Test Case
© 2008 Syntax Consulting, Inc; All rights reserved.
Composite presenters
© 2008 Syntax Consulting, Inc; All rights reserved.
Agenda
A brief history lessonPresenter pattern in detailMVP and RCPRAP / RCP dual use demo
© 2008 Syntax Consulting, Inc; All rights reserved.
Presenter hierarchy
© 2008 Syntax Consulting, Inc; All rights reserved.
AbstractPresenter
Composite pattern Property change management (if data binding is used)
© 2008 Syntax Consulting, Inc; All rights reserved.
AbstractViewPartPresenter
Create in IViewPart.init() Route saveState() Mementos Filtering Sorting
© 2008 Syntax Consulting, Inc; All rights reserved.
AbstractEditPartPresenter
Create in IEditorPart.init() or extract from editor input Dirty state management Save and revert Form header messages Can have nested page and section presenters
© 2008 Syntax Consulting, Inc; All rights reserved.
AbstractDialogPresenter
Create in constructor Handle button enablement Handle button presses
© 2008 Syntax Consulting, Inc; All rights reserved.
AbstractWizardPresenter
Page management (page presenters or constants) Determines if wizard can finish Performs finish or cancel logic
© 2008 Syntax Consulting, Inc; All rights reserved.
AbstractWizardPagePresenter
Page management Handles isPageComplete()
© 2008 Syntax Consulting, Inc; All rights reserved.
Data binding
© 2008 Syntax Consulting, Inc; All rights reserved.
Data binding - view in control
© 2008 Syntax Consulting, Inc; All rights reserved.
Data binding - presenter in control
© 2008 Syntax Consulting, Inc; All rights reserved.
© 2008 Syntax Consulting, Inc; All rights reserved.
Data binding - beyond data
Control enablementSelectionsView still needs to call presenter to take
action (e.g. okPressed)Presenter can set a listener into view if
full control is desired
© 2008 Syntax Consulting, Inc; All rights reserved.
Agenda
A brief history lessonPresenter pattern in detailMVP and RCPRAP / RCP dual use demo
© 2008 Syntax Consulting, Inc; All rights reserved.
What is RAP?
Rich Ajax PlatformParallel UI toolkit including SWT, JFace
and RCP WorkbenchUI classes run on server and emit
Javascript to browserNow supports data binding!http://www.eclipse.org/rap/
© 2008 Syntax Consulting, Inc; All rights reserved.
RAP Example
© 2008 Syntax Consulting, Inc; All rights reserved.
Presenter dual use
© 2008 Syntax Consulting, Inc; All rights reserved.
MVP Summary
Replacement for MVCFlexibility and testabilityMVP and RCP work well togetherMVP prepares you for the future
© 2008 Syntax Consulting, Inc; All rights reserved.
www.rcpquickstart.com/training/presentations/mvp-and-rcp-ew2008
Check out my website:
Detailed notes for presentation at:
www.rcpquickstart.com
Email me:[email protected]
More Information