leverageing the model-view-presenter pattern in rich ... · this.view = view; public void...

41
© 2008 Syntax Consulting, Inc; All rights reserved. Leveraging the Model-View- Presenter Pattern in Rich Client Applications Patrick Paulin Eclipse RCP Trainer and Consultant RCP Quickstart [email protected] www.rcpquickstart.com/training/presentations/mvp-and-rcp-ew2008

Upload: others

Post on 22-May-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Leveraging the Model-View-Presenter Pattern in Rich ClientApplications

Patrick PaulinEclipse RCP Trainer and ConsultantRCP Quickstart

[email protected]

www.rcpquickstart.com/training/presentations/mvp-and-rcp-ew2008

Page 2: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Agenda

A brief history lessonPresenter pattern in detailMVP and RCPRAP / RCP dual use demo

Page 3: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

UI architecture

Forms and controlsModel-View-Controller (MVC)Model-View-Presenter (MVP)

Page 4: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Model / View separation

Page 5: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 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());

}});

Page 6: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Model-View-Controller (MVC)

Page 7: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

What is a controller, anyway?

Page 8: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Model-View-Presenter (MVP)

Page 9: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Agenda

A brief history lessonPresenter pattern in detailMVP and RCPRAP / RCP dual use demo

Page 10: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

What is MVP?

Page 11: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Why MVP?

Manage complexityFlexibilityTestability

Page 12: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Managing complexity

Page 13: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Flexibility

Page 14: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 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

Page 15: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 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()) }}

Page 16: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

MVP and data

Page 17: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

MVP and logic

public class ExamplePresenter {

public void setName(String name)

public boolean getOkEnabled()

}

Page 18: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

MVP continuum

Page 19: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Push vs pull

Page 20: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Passive view testing

Page 21: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 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

Page 22: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Composite presenters

Page 23: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Agenda

A brief history lessonPresenter pattern in detailMVP and RCPRAP / RCP dual use demo

Page 24: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Presenter hierarchy

Page 25: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

AbstractPresenter

Composite pattern Property change management (if data binding is used)

Page 26: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

AbstractViewPartPresenter

Create in IViewPart.init() Route saveState() Mementos Filtering Sorting

Page 27: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 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

Page 28: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

AbstractDialogPresenter

Create in constructor Handle button enablement Handle button presses

Page 29: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

AbstractWizardPresenter

Page management (page presenters or constants) Determines if wizard can finish Performs finish or cancel logic

Page 30: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

AbstractWizardPagePresenter

Page management Handles isPageComplete()

Page 31: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Data binding

Page 32: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Data binding - view in control

Page 33: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Data binding - presenter in control

Page 34: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Page 35: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 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

Page 36: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Agenda

A brief history lessonPresenter pattern in detailMVP and RCPRAP / RCP dual use demo

Page 37: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 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/

Page 38: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

RAP Example

Page 39: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

Presenter dual use

Page 40: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 2008 Syntax Consulting, Inc; All rights reserved.

MVP Summary

Replacement for MVCFlexibility and testabilityMVP and RCP work well togetherMVP prepares you for the future

Page 41: Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void setName(String name) this.view.setOkEnabled(name != null); public class ExamplePresenterTest

© 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