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
-
Indexed Properties
-
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
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.
Developing Java Beans
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.