|
Home TOC |
|
JavaBeans Component Design Conventions
JavaBeans component design conventions govern the properties of the class, and the public methods that give access to the properties.
A JavaBeans component property can be:
- Read/write, read-only, or write-only.
- Simple, which means it contains a single value, or indexed, which means it represents an array of values.
There is no requirement that a property be implemented by an instance variable; the property must simply be accessible using public methods that conform to certain conventions:
- For each readable property, the bean must have a method of the form:
PropertyClass getProperty() { ... }- For each writable property, the bean must have a method of the form:
setProperty(PropertyClass pc) { ... }In addition to the property methods, a JavaBeans component must define a constructor that takes no parameters.
The
Duke's Bookstoreapplication JSP pagesenter.jsp,bookdetails.jsp,catalog.jsp,showcart.jspuse thedatabase.BookDBanddatabase.BookDetailsJavaBeans components.BookDBprovides a JavaBeans component front end to the access objectBookDBAO. Both beans are used extensively by bean-oriented custom tags (see Custom Tags in JSPPages (page 461)). The JSP pages
showcart.jspandcashier.jspusecart.ShoppingCartto represent a user's shopping cart.The JSP pages
catalog.jsp,showcart.jsp, andcashier.jspuse theutil.CurrencyJavaBeans component to format currency in a locale-sensitive manner. The bean has two writable properties,localeandamount, and one readable property,format. Theformatproperty does not correspond to any instance variable, but returns a function of thelocaleandamountproperties.public class Currency { private Locale locale; private double amount; public Currency() { locale = null; amount = 0.0; } public void setLocale(Locale l) { locale = l; } public void setAmount(double a) { amount = a; } public String getFormat() { NumberFormat nf = NumberFormat.getCurrencyInstance(locale); return nf.format(amount); } }
|
Home TOC |
|