|
One of the most popular patterns in Java is the JavaBeans component model (http://java.sun.com/products/javabeans/). Originally designed to allow visual design tools to generate AWT user interfaces, the JavaBeans specification provides additional guidelines on top of the basic contracts implied by a given Java class. The JavaBeans specification provides mechanisms for methods of a Java class to be visible to a builder tool, organized into properties, methods, and events. A property is available via accessor methods, such as String getFirstName() and void setFirstName(). Methods are ordinary Java methods. Events define standard methods for allowing one component to notify one or more components of an arbitrary event. Visual development tools use these various systems to enable visual construction of user interfaces: a button is dragged onto a panel, the properties are set (such as the label and size), and events are wired (for example, clicking on the button closes the window). For server-side development, the most popular aspects of the JavaBeans specification are the constructor and property patterns. Specifically, a standard JavaBean must have a no-argument constructor and get/set accessor methods corresponding to the various properties. For example, the simple JavaBean shown in Listing 7-1 has two String properties (first name, last name) and a single int property (clearance). Listing 7-1. Simple JavaBeanpackage com.cascadetg.ch07; public class User { // Private variables private String firstName; private String lastName; private int clearance; // Accessor methods public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getClearance() { return clearance; } public void setClearance(int clearance) { this.clearance = clearance; } } While the JavaBeans specification describes rules for how components should be written and the expected behavior of the tools, no implementation is provided. Generally speaking, it is assumed that the low-level java.lang.reflect.* package will be used to obtain information about the Java classes, and the tool will generate code as needed. Over time, it has become clear that frameworks, not just visual design tools, can take advantage of the JavaBeans patterns. For example, the object/relational bridge framework Hibernate uses JavaBean patterns to help work with relational databases in a more natural fashion.
While it is possible to use the low-level reflection package to deal with JavaBeans, it is easier to use the Jakarta Commons BeanUtils package. For anyone given to thinking in terms of broader architectural design and framework development, it is easy to think of other areas in which the BeanUtils package may be useful. Obviously, this package would be of interest to anyone interested in building visual development tools. Similarly, dependency injection, dynamic configuration, and runtime binding of application elements such as the user interface and other systems are all potentially of interest. In this chapter we will look at how to use the Jakarta Commons BeanUtils package to build a simple framework for converting objects based on the JavaBeans standard to HTML forms and back. |
|