Lesson 1
JavaBeans Internals
This module takes you inside a Bean to learn about the structure and organization of data and methods, the two primary parts of a Bean. You learn all about properties, which model the public portion of a Bean's data and are accessible externally through accessor methods.
You take a look at the different types of properties, including simple properties, indexed properties, bound properties, and constrained properties.
Toward the end of the module you work through a practical exercise of creating a property and its associated accessor methods.
- Module Learning Objectives:
After completing this module, you will have the knowledge and skills necessary to:
- Design and code properties for your own Beans
- Define accessor methods used to access properties
- Understand when and why to use indexed, bound, and constrained properties
Function of 1) simple properties, 2) indexed properties, 3) bound properties, and 4) constrained properties
JavaBeans, a component architecture for Java, uses several types of properties to define the characteristics of a bean. Here's a breakdown of the four types you've mentioned:
- Simple Properties**
- **Function:** These are basic properties where a bean has getter and setter methods for direct access and modification.
- **Example:**
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
- **Usage:** Used for straightforward data access without any additional logic or constraints.
-
Indexed Properties**
- **Function:** These properties represent an array or collection where individual elements can be accessed or modified by index.
- **Example:**
private String[] items;
public String[] getItems() {
return items;
}
public void setItems(String[] items) {
this.items = items;
}
public String getItem(int index) {
return items[index];
}
public void setItem(int index, String item) {
items[index] = item;
}
- **Usage:** Useful when you need to manage collections within a bean, allowing for both bulk and individual element manipulation.
- Bound Properties**
- **Function:** These properties notify listeners when their values change. This is useful for maintaining consistency across multiple beans or updating UI components automatically.
- **Example:**
private String title;
private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
String oldTitle = this.title;
this.title = title;
propertyChangeSupport.firePropertyChange("title", oldTitle, title);
}
- **Usage:** Ideal for scenarios where changes in one bean should trigger updates in other parts of the application.
-
Constrained Properties**
- **Function:** These properties allow other objects to veto a change before it happens. This ensures that certain conditions are met before a property can be changed.
- **Example:**
private int age;
private VetoableChangeSupport vetoableChangeSupport = new VetoableChangeSupport(this);
public void addVetoableChangeListener(VetoableChangeListener listener) {
vetoableChangeSupport.addVetoableChangeListener(listener);
}
public void removeVetoableChangeListener(VetoableChangeListener listener) {
vetoableChangeSupport.removeVetoableChangeListener(listener);
}
public int getAge() {
return age;
}
public void setAge(int age) throws PropertyVetoException {
vetoableChangeSupport.fireVetoableChange("age", this.age, age);
this.age = age;
}
- **Usage:** Used in scenarios where data integrity or business rules must be strictly enforced before allowing a property change.
Each type of property in JavaBeans serves a specific purpose, enhancing the flexibility and functionality of beans in different application contexts.
The two primary components of a JavaBean are indeed:
- Data: Represented by private fields that encapsulate the object's state. These fields typically store the bean's properties, which define its characteristics and behavior.
- Methods: Public methods that provide access to and manipulate the object's data. These methods allow other parts of your program to interact with the bean, get or set its properties, and trigger its actions.
By following these conventions, JavaBeans become reusable and modular components that can be easily integrated into different applications.
This makes them a valuable tool for developers who want to build clean, maintainable, and flexible software.
JavaBeans Architecture
The
JavaBeans architecture is supposed to scale well from small to large; simple beans can be used to build larger beans. A small bean may consist of a single class; a large bean may have many. Beans can also work together through their container to provide services to other beans. Simple beans are little more than ordinary
Java objects.
- In fact, any Java class that has a default (empty) constructor could be considered a bean.
- A bean should also be serializable, although the JavaBeans specification does not strictly require that.
These two criteria ensure that we can create an instance of the bean dynamically and that we can later save the bean as part of a group or composition of beans. There are no other requirements. Beans are not required to inherit from a base bean class, and they do not have to implement any special interface. A useful bean should send and receive events and expose its properties to the world. To do so, it follows the appropriate design patterns for naming the relevant methods so that these features can be automatically discovered. Most nontrivial beans intended for use in a visual application builder IDE also provide information about themselves in the form of a BeanInfo class. A BeanInfo class implements the BeanInfo interface, which holds methods that describe a bean's features in more detail, along with extra packaging, such as icons for display to the user. Normally, this "bean info" is supplied by a separate class that is named for and supplied with the bean.
In the next lesson, the structural makeup of a Bean will be discused.
Developing Java Beans