Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void...
Transcript of Leverageing the Model-View-Presenter Pattern in Rich ... · this.view = view; public void...
![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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/1.jpg)
© 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
![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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/2.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/3.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/4.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/5.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/6.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/7.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/8.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/9.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/10.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/11.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/12.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/13.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/14.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/15.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/16.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/17.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/18.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/19.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/20.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/21.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/22.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/23.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/24.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/25.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/26.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/27.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/28.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/29.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/30.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/31.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/32.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/33.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/34.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/35.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/36.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/37.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/38.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/39.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/40.jpg)
© 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](https://reader030.fdocuments.in/reader030/viewer/2022041016/5ec95caa18fe0851113bae00/html5/thumbnails/41.jpg)
© 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