desktop patterns and data binding
play

Desktop Patterns and Data Binding Karsten Lentzsch Desktop Patterns - PowerPoint PPT Presentation

J-Fall 2006 Desktop Patterns and Data Binding Karsten Lentzsch Desktop Patterns and Data Binding JGoodies J-Fall 2006 Goal Learn how to organize presentation logic and how to bind domain data to views Desktop Patterns and Data Binding


  1. J-Fall 2006 Desktop Patterns and Data Binding Karsten Lentzsch Desktop Patterns and Data Binding JGoodies

  2. J-Fall 2006 Goal Learn how to organize presentation logic and how to bind domain data to views Desktop Patterns and Data Binding JGoodies

  3. J-Fall 2006 Agenda Introduction Autonomous View Model View Presenter Presentation Model Data Binding Desktop Patterns and Data Binding JGoodies

  4. J-Fall 2006 Agenda Introduction Autonomous View Model View Presenter Presentation Model Data Binding Desktop Patterns and Data Binding JGoodies

  5. J-Fall 2006 Legend Presentation (View) Presentation Logic Notifies Refers to Domain Object Desktop Patterns and Data Binding JGoodies

  6. J-Fall 2006 Legend ● Domain/business logic ● Examples: ● Book ● Person ● Address ● Invoice ● More generally: object graph Domain Object Desktop Patterns and Data Binding JGoodies

  7. J-Fall 2006 Legend ● Handlers for: ● List selection changes ● Check box selection Presentation Logic ● Drag drop end ● UI models ● ListModel ● TableModel ● TreeSelectionModel ● Swing Actions Desktop Patterns and Data Binding JGoodies

  8. J-Fall 2006 Event Handling vs. Presentation Logic ● Toolkit handles fine-grained events: ● Mouse entered, exited ● Mouse pressed ● Radio button pressed, armed, rollover ● Application handles coarse-grained events: ● Radio button selected ● Action performed ● List items added ● Domain property changed Desktop Patterns and Data Binding JGoodies

  9. J-Fall 2006 Legend ● Container: Presentation (View) ● JPanel, JDialog, JFrame ● Contains components: ● JTextField, JList, JTable ● Component initialization ● Panel building code ● GUI state: ● Check box pressed ● Mouse over Desktop Patterns and Data Binding JGoodies

  10. J-Fall 2006 Legend ● Role1 and Role2 Role1 “sit together” in a class ● Can access each other Role2 ● Separated layers Desktop Patterns and Data Binding JGoodies

  11. J-Fall 2006 Legend ● A refers to B ● A holds a reference to B A ● B indirectly refers to A Notifies Refers to B Desktop Patterns and Data Binding JGoodies

  12. J-Fall 2006 All Mixed Together Presentation (View) Presentation Logic Domain Desktop Patterns and Data Binding JGoodies

  13. J-Fall 2006 Pattern: Separated Presentation Presentation (View) Presentation Logic Domain Desktop Patterns and Data Binding JGoodies

  14. J-Fall 2006 Business Logic in the Presentation Presentation (View) Presentation Logic Business Logic Domain Desktop Patterns and Data Binding JGoodies

  15. J-Fall 2006 Decouple Domain from Presentation ● The domain shall not reference the presentation ● Presentation refers to domain and modifies it ● Advantages: ● Reduces complexity ● Multiple presentations Desktop Patterns and Data Binding JGoodies

  16. J-Fall 2006 Separated Presentation with Observer Presentation (View) Presentation Logic Refers to Notifies Domain Desktop Patterns and Data Binding JGoodies

  17. J-Fall 2006 Agenda Introduction Autonomous View Model View Presenter Presentation Model Data Binding Desktop Patterns and Data Binding JGoodies

  18. J-Fall 2006 Pattern: Autonomous View Presentation (View) Presentation Logic Desktop Patterns and Data Binding JGoodies

  19. J-Fall 2006 Autonomous View ● Often one class per window or screen ● Often a subclass of JDialog, JFrame, JPanel ● Contains: ● Fields for UI components ● Component initialization ● Panel building/layout ● Model initialization ● Presentation logic: listeners, operations Desktop Patterns and Data Binding JGoodies

  20. J-Fall 2006 Example GUI Composer field is enabled, if classical is selected Desktop Patterns and Data Binding JGoodies

  21. J-Fall 2006 Autonomous View Sample (1/2) public class AlbumDialog extends JDialog { private final Album album; private JTextField artistField; ... public AlbumDialog(Album album) { ... } private void initComponents() { ... } private void initPresentationLogic() { ... } private JComponent buildContent() { ... } Desktop Patterns and Data Binding JGoodies

  22. J-Fall 2006 Autonomous View Sample (2/2) class ClassicalChangeHandler implements ChangeListener { public void stateChanged(ChangeEvent e) { // Check the classical state. boolean classical = classicalBox.isSelected(); // Update the composer field enablement. composerField.setEnabled(classical); } } Desktop Patterns and Data Binding JGoodies

  23. J-Fall 2006 Autonomous View: Tips ● Build dialogs, frames, panels ● Extend JDialog, JFrame, JPanel if necessary. Do you extend or use HashMap? Desktop Patterns and Data Binding JGoodies

  24. J-Fall 2006 Autonomous View ● Common and workable ● Has disadvantages: ● Difficult to test logically ● Difficult to overview, manage, maintain, and debug, if the view or logic is complex ● Consider to separate the logic from the view Desktop Patterns and Data Binding JGoodies

  25. J-Fall 2006 Presentation Logic Separated Presentation (View) Presentation Logic Domain Desktop Patterns and Data Binding JGoodies

  26. J-Fall 2006 Separated Logic: Advantages I ● Allows to test the presentation logic logically ● Simplifies team synchronization ● Each part is smaller and easier to overview ● Allows to build “forbidden zones” ● For team members ● Before you ship a new release ● Layout changes allowed ● Design is done, but bug fixes in the logic are still allowed Desktop Patterns and Data Binding JGoodies

  27. J-Fall 2006 Separated Logic: Advantages II ● Thin GUI: ● Easier to build, understand, maintain ● Can follow syntactical patterns ● More team members can work with it ● Logic can ignore presentation details, e.g. component types (JTable vs. JList) ● Logic can be reused for different views Desktop Patterns and Data Binding JGoodies

  28. J-Fall 2006 Separated Logic: Disadvantages ● Extra machinery to support the separation ● Extra effort to read and manage multiple sources Desktop Patterns and Data Binding JGoodies

  29. J-Fall 2006 Separating Logic from the View ● Can simplify or add complexity ● Separation costs vary with the pattern used ● Opinion: typically you benefit from the separation My personal guideline for team projects: ● Use Autonomous View for message dialogs ● Otherwise separate the logic from the view Desktop Patterns and Data Binding JGoodies

  30. J-Fall 2006 Agenda Introduction Autonomous View Model View Presenter Presentation Model Data Binding Desktop Patterns and Data Binding JGoodies

  31. J-Fall 2006 Pattern: Model View Presenter (MVP) View Presenter Model Desktop Patterns and Data Binding JGoodies

  32. J-Fall 2006 Model View Presenter View Presenter •Holds UI components •Reads domain data •Holds GUI state •Sets GUI state •Inits components •Presentation Logic •Builds panel •Changes the domain Model •Holds domain data •Provides business logic Desktop Patterns and Data Binding JGoodies

  33. J-Fall 2006 Album Example: Autonomous View AlbumDialog JTextField JTextField JCheckBox ChangeHandler JTextField Album Desktop Patterns and Data Binding JGoodies

  34. J-Fall 2006 Album Example: Model View Presenter AlbumView AlbumPresenter JTextField JTextField JCheckBox ChangeHandler JTextField Album Desktop Patterns and Data Binding JGoodies

  35. J-Fall 2006 From Autonomous View ... public class AlbumDialog extends JDialog { private JTextField artistField; public AlbumDialog(Album album) { ... } private void initComponents() { ... } private JComponent buildContent() { ... } private final Album album; private void initPresentationLogic() { ... } private void readGUIStateFromDomain() { ... } private void writeGUIStateToDomain() { ... } class ClassicalChangeHandler implements ... class OKActionHandler implements ... } Desktop Patterns and Data Binding JGoodies

  36. J-Fall 2006 ... to Model View Presenter class AlbumView extends JDialog { JTextField artistField; public AlbumView() { ... } private void initComponents() { ... } private JComponent buildContent() { ... } } public class AlbumPresenter { private final AlbumView view; private Album album; private void initPresentationLogic() { ... } private void readGUIStateFromDomain() { ... } private void writeGUIStateToDomain() { ... } class ClassicalChangeHandler implements ... class OKActionHandler implements ... } Desktop Patterns and Data Binding JGoodies

  37. J-Fall 2006 ... to Model View Presenter class AlbumView extends JDialog { JTextField artistField; public AlbumView() { ... } private void initComponents() { ... } private JComponent buildContent() { ... } } public class AlbumPresenter { private final AlbumView view; private Album album; private void initPresentationLogic() { ... } private void readGUIStateFromDomain() { ... } private void writeGUIStateToDomain() { ... } class ClassicalChangeHandler implements ... class OKActionHandler implements ... } Desktop Patterns and Data Binding JGoodies

  38. J-Fall 2006 Presenter: Example Logic class ClassicalChangeHandler implements ChangeListener { public void stateChanged(ChangeEvent e) { // Check the view's classical state. boolean classical = view.classicalBox.isSelected(); // Update the composer field enablement. view.composerField.setEnabled(classical); } } Desktop Patterns and Data Binding JGoodies

Recommend


More recommend