1 / 96

ObjectEditor

ObjectEditor. Prasun Dewan Comp 114. ObjectEditor. Automatic user-interface generation. You only write computation code Separate object to do I/O Main just instantiates it Can replace it with own UI later. Serves as training wheels Serves to separate UI from computation. Example Class.

azia
Télécharger la présentation

ObjectEditor

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ObjectEditor Prasun Dewan Comp 114

  2. ObjectEditor • Automatic user-interface generation. • You only write computation code • Separate object to do I/O • Main just instantiates it • Can replace it with own UI later. • Serves as training wheels • Serves to separate UI from computation

  3. Example Class package bmi; publicclass ABMICalculator implements BMICalculator { publicdouble calculateBMI (double weight, double height) { return weight/(height*height); } }

  4. Example Main Class package main; import bus.uigen.ObjectEditor; import bmi.ABMICalculator; public class ABMIDisplayer { publicstaticvoidmain(String[] args) { ObjectEditor.edit(new ABMICalculator()); } }

  5. ABMICalculator UI

  6. ABMICalculator UI

  7. Adding a library in JBuilder

  8. Adding a library in JBuilder

  9. Adding a library in JBuilder

  10. Adding a library in JBuilder

  11. Adding a library in JBuilder

  12. Adding a library in JBuilder

  13. Adding a library in JBuilder

  14. Location of Libraries • http://www.cs.unc.edu/~dewan/oe • Use Internet explorer (not Netscape) to download files • Library names • oe.jar • shapes.jar • oe2.jar version 2 of oe.jar • Try oe2 first and in case of bugs use oe.jar • oe.jar used by comp14 students • oe has lots of bugs! • Send me mail for workarounds bugs.

  15. Location of Libraries

  16. Unchanging value retyped What-if BMI Calculations

  17. State: Data Remembered by an Object between computations BMI Spreadsheet

  18. accesses accesses Belong to a single method Belong to all methods of an instance Instance Variables ABMICalculator Instance ABMISpreadsheet Instance calculateBMI getBMI Body Body Instance Variables Parameters local variable global variable

  19. Identical Instances Different Instances State-less Vs State-ful Objects ~ car radios without presets ~ car radios with presets

  20. ABMISpreadsheet publicclass ABMISpreadsheet { double height, weight; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return weight/(height*height); } } Height Weight BMI

  21. Name: P publicclass C { Type: T Editable publicvoid setP(T newValue) { ... } Getter method Setter method } newP Violates Bean Conventions obtainP Read-Only and Editable Properties Typed, Named Unit of Exported Object State Bean public T getP() { ... } Readonly • Conventions for • humans • tools

  22. Read-Only Editable Independent Editable Independent Read-only Dependent Properties Classification • publicclass ABMISpreadsheet { • double height; • publicdouble getHeight() { • return height; • } • publicvoid setHeight(double newHeight) { • height = newHeight; • } • double weight; • publicdouble getWeight() { • return weight; • } • publicvoid setWeight(double newWeight) { • weight = newWeight; • } • publicdouble getBMI() { • return weight/(height*height); • } • … Height Weight BMI

  23. Calling Getter and Setter Methods • When ObjectEditor window is created • Getter method of each property called to display initial value of property • When property is changed to a new value • Setter method of property is called with new value as actual parameter • Getter method of each property is called to refresh display

  24. Calling Getter and Setter Methods publicclass ABMISpreadsheet { double height = 1.77; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } double weight = 77; publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return weight/(height*height); } }

  25. publicclass ABMISpreadsheet { double height = 1.77; publicdouble getHeight() { System.out.println (“getHeight Called”); return height; } publicvoid setHeight(double newHeight) { System.out.println (“setWeight Called”); height = newHeight; } double weight = 77; publicdouble getWeight() { System.out.println (“getWeight Called”); return weight; } publicvoid setWeight(double newWeight) { System.out.println (“setWeight Called”); weight = newWeight; } publicdouble getBMI() { System.out.println (“getBMI Called”); return weight/(height*height); } } Tracing Method Calls

  26. Actual Trace

  27. How will above UI change? publicclass ABMISpreadsheet { double height, weight; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return calculateBMI(weight, height); } publicdouble calculateBMI(double weight, double height) { return weight / (height*height); | } Modified ABMISpreadsheet

  28. Properties + Class Menu

  29. Editing in slow motion : Initial value

  30. Editing in slow motion: text string edited

  31. Editing in slow motion: return triggers setter and getter calls

  32. publicclass ABMISpreadsheet { double height, weight; publicdouble height() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return calculateBMI(weight, height); } publicdouble calculateBMI(double weight, double height) { return weight / (height*height); | } Renaming getter method

  33. ObjectEditor does not know it is getter

  34. publicclass ABMISpreadsheet { double height, weight; double getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return calculateBMI(weight, height); } publicdouble calculateBMI(double weight, double height) { return weight / (height*height); | } Reducing Access

  35. Height is not a readable property

  36. Changing setter publicclass ABMISpreadsheet { double height, weight; publicdouble getHeight() { return height; } publicint setHeight(double newHeight) { height = newHeight; return height; } publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return calculateBMI(weight, height); } publicdouble calculateBMI(double weight, double height) { return weight / (height*height); | }

  37. Height is not a writeable property

  38. An alternative class • public class AStringHistory implements StringHistory { • public staticfinalint MAX_SIZE = 50; • String[] contents = new String[MAX_SIZE]; • int size = 0; • publicint size() { return size;} • public String elementAt (int index) { return contents[index]; } • boolean isFull() { return size == MAX_SIZE; } • publicvoid addElement(String element) { • if (isFull()) • System.out.println("Adding item to a full history"); • else { • contents[size] = element; • size++; • } • } • } Variable number of dynamically created indexed properties

  39. Write method (name does not matter to OE) Arbitrary Type (Must be Object Type to be recognized by ObjectEditor) Read methods ObjectEditor Conventions for Variable-Sized Collection • publicinterface I { • publicvoid addElement (T t); • public T elementAt (int index); • publicint size(); • }

  40. Initial State

  41. Adding an element Name does not matter to ObjectEditor

  42. Adding an element

  43. Adding an element ObjectEditor calls all elementAt () and getter() methods after each method call

  44. Adding another element

  45. Adding another element

  46. Public Class Constants

  47. Public Class Constants

  48. Non Public Class Constant • public class AStringHistory implements StringHistory { • staticfinalint MAX_SIZE = 50; • String[] contents = new String[MAX_SIZE]; • int size = 0; • publicint size() { return size;} • public String elementAt (int index) { return contents[index]; } • boolean isFull() { return size == MAX_SIZE; } • publicvoid addElement(String element) { • if (isFull()) • System.out.println("Adding item to a full history"); • else { • contents[size] = element; • size++; • } • } • }

  49. Non-Public Class Constant Non constants menu

  50. Public Instance Constant • public class AStringHistory implements StringHistory { • publicfinalint MAX_SIZE = 50; • String[] contents = new String[MAX_SIZE]; • int size = 0; • publicint size() { return size;} • public String elementAt (int index) { return contents[index]; } • boolean isFull() { return size == MAX_SIZE; } • publicvoid addElement(String element) { • if (isFull()) • System.out.println("Adding item to a full history"); • else { • contents[size] = element; • size++; • } • } • }

More Related