Title: Java Beans
1Java Beans
Definition - A bean is a reusable software
component that can be manipulated using a
builder tool. Nature of beans -Beans can be
small GUI components such as buttons or they
can be substantial system components with
no visible representation. Benefits - The
ability to be able to manipulate (e.g.
interconnect) groups of beans automatically
within a builder tool (i.e. without having to
programmatically connect them) opens up the
possibility of plug and play in a visual
tool. Means of achieving pluggability
- Design patterns A prescribed set of design
rules to assist a builder tool identify bean
anatomy. BeanInfo class A way of making beans
self documenting Introspection Facilities in
Java for self analysis.
P. Martin 2001 - JB-1
2Design Patterns
Construction To
enable builder tools to instantiate beans
they must either have a zero parameter
constructor provide a serialised prototype.
(Beans are normally required to be
serialisable) Bean features Beans are
characterised by a number of features -
Properties their properties which may be
edittable by a builder tool during
configuration. Events that they generate as
an event source or respond to as a
listener. Methods which form the public
interface of the component Whilst methods can be
identified by introspection, automatic
configuration and inter-connection of beans
requires use of standard naming conventions.
P. Martin 2001 - JB-2
3Bean Properties
These are the beans private attributes (or more
specifically those we wish to be able to
edit). They come in four varieties - Simple
properties Single valued attributes Indexed
properties Arrays of attributes Bound
properties Properties whose changes might
affect other components and need to be
relayed to them. Constrained
properties Whose changes might be forbidden
by other components for some reason.
P. Martin 2001 - JB-3
4Simple Properties
Single valued edittable
attributes, e.g. hatColour and boolean
attribute bareHeaded. Must supply get and set
methods as illustrated below.
P. Martin 2001 - JB-4
5Indexed Properties
Arrays of edittable attributes, e.g. int
marks. Must supply get and set for both
individual components and the whole collection -
P. Martin 2001 - JB-5
6Defining Events
Before dealing with bound
and constrained properties, the mechanism
for identifying event source beans is needed. If
a bean gives rise to an event E, it must provide
add and remove methods for Elisteners as follows
-
P. Martin 2001 - JB-6
7Bound Properties
These are not
discernable from method names and may require
additional BeanInfo support - see later. As
other beans must be notified of property changes,
this bean must be a source of property change
events, so there must be add and remove methods.
P. Martin 2001 - JB-7
8Bound Properties(continued)
Can also add property
change listeners for specific bound properties in
one of two ways - By providing the property
name as a String parameter -
Or, by including the property name in the
add/remove methods. This latter approach
obviates the need for BeanInfo here.
P. Martin 2001 - JB-8
9Support for Property Change Notification
Java provides support for the
registering and broadcasting of property change
events in the PropertyChangeSupport class.
With this, it is only necessary to wrap the
add/remove methods and make calls to a
firePropertyChange method, e.g.
sup.fireProperyChange(value,5,10)
public class Demo implements Serialisable
private int value private
PropertyChangeSupport sup new
PropertyChangeSupport(this) public
Demo() public void addPropertyChangeListene
r(PropertyChangeListener p)
sup.addPropertyChangeListener(p)
public void removePropertyChangeListener(PropertyC
hangeListener p) sup.removePropertyChan
geListener(p) //end Demo
P. Martin 2001 - JB-9
10Constrained Properties
Here, property
changes can be forbidden by listener objects.
This is done by a listener throwing a
PropertyVetoException. The add and remove
methods for listener registration and the set
method for the property become -
The two other forms of add/remove method can also
be used as can a VetoableChangeSupport class.
P. Martin 2001 - JB-10
11Introspection
Introspection
and reflection refer to Javas capability to
discover the names of methods and the types of
parameters and return values from methods. This
is of great help to a Builder Tool in presenting
information to the component assembler. The
class Object This provides the method Class
getClass() which is inherited by all objects
and returns an instance of the Class
class. The class Class This class provides its
instances with many methods for retrieving
features of the class of an object,
e.g.- Constructor getConstructors()
Method getMethods() Field
getFields()
P. Martin 2001 - JB-11
12Introspection (continued)
The package
java.lang.reflect contains key meta classes which
provide means of examining the features of a
classes attributes and methods - The class
Method This class provides methods for
extracting the features of a method, e.g.
- String getName() int getModifiers() Cla
ss getParameterTypes() Class
getReturnType() The class Field This allows
fields to be examined and set - Class
getType() void setInt(Object o, int i) int
getModifiers() int getInt() String
getName() above 2 also for
other data types A Constructor class similarly
allows parameter types to be determined.
P. Martin 2001 - JB-12
13Additional Bean Information
Accessing
descriptive information To further
assist a bean builder tool in introspection, the
java.beans package provides a
BeanInfo interface which gives access to a series
of feature descriptor classes such as
BeanDescriptor, EventSetDescriptor,
MethodDescriptor and PorpertyDescriptor, each of
which contains user documentation on
the bean and its features. Accessing
editors and customisers
PropertyDescriptor objects provide access to
custom PropertyEditors if the default
editors are inappropriate and the BeanDescriptor
class gives access to a Customizer
class which a bean developer can use to take over
the configuration process
entirely. The package also contains definitions
of property change support classes and listener
interfaces.
P. Martin 2001 - JB-13