7/28/2019 Colossal Java Bean
1/12
Java Bean
Mubarak,
Solution Architect,
Colossal Software Technologies.
What is JavaBeanReusable Java components
Visual and non-visual components possible
any object that conforms to a few basic rules can be a bean
originally developed to provide java with a componenttechnology like vbx/ocx of VB
originally used for user interface widgets
Visual manipulation in builder tools
Beans support separate design-time and runtime interfaces
7/28/2019 Colossal Java Bean
2/12
What makes a Bean a Bean
Must have a zero-argument (empty) constructor
Should have no public instance variables
Persistent values should be accessed through methods
called getXxx and setXxx
Must support persistence (the bean is serializable)
Properties : Named attributes associated with a bean
that can be read or manipulated through methods
Event-Handling: Ability for beans to communicate with
themselves and with their outside world asynchronously
Introspection: Ability of seeing inside a bean and analyzing
what services it provides
Customization: Ability of manipulating the appearance or
behavior of a bean while designing the application
Persistence: Capturing the customized state of a bean forlater (re) use
Security: Access control based Java security
Characteristics of Java Beans
7/28/2019 Colossal Java Bean
3/12
Bean PropertiesSimple
represents a single property and can be defined with a
pair of get/set methods.
Indexed
represents an array of properties.
Bound
a bound property notifies other objects when its value
changes generates a PropertyChange event with
property name, old value and new value.
Constrained
an object with constrained properties allows otherobjects to veto a constrained property value change
Constrained property listeners can veto a change by
throwing a PropertyVetoException.
Simple Property To be a bean, you cannot have public fields instead you
should expose fields using simple property.
So, you should replace
public double speed;
with
private double speed;
public double getSpeed() {
return(speed);
}
public void setSpeed(double newSpeed) {speed = newSpeed;
}
7/28/2019 Colossal Java Bean
4/12
Simple Property
You can put constraints on values.
public void setSpeed(double newSpeed) {
if (newSpeed < 0) {
sendErrorMessage(...);
newSpeed = Math.abs(newSpeed);
}
speed = newSpeed;
}
If users of your class accessed the fields directly, then
they would each be responsible for checking constraints.
Simple Property You can change your internal representation without
changing interface
// Now using metric units (kph, not mph)
public void setSpeed(double newSpeed)
{
setSpeedInKPH = convert(newSpeed);
}
public void setSpeedInKPH(double newSpeed)
{
speedInKPH = newSpeed;
}
7/28/2019 Colossal Java Bean
5/12
Simple Property
You can perform arbitrary side effects.
public double setSpeed(double newSpeed)
{
speed = newSpeed;
updateSpeedometerDisplay();
}
If users of your class accessed the fields directly, then theywould
each be responsible for executing side effects.
Too much work and runs huge risk of having display beinconsistent from actual values.
Indexed Property Collections of properties accessed by an index (like an
array)
Methods to set & get array of values
public [] get();
public void set([]value);
Methods to set & get individual valuespublic get(int pos);
public void set(intpos,[]
value);
7/28/2019 Colossal Java Bean
6/12
Property binding If a beans properties are changed it is possible another bean
want to act on that change
private PropertyChangeSupport changes = newPropertyChangeSupport( this );
public void addPropertyChangeListener(PropertyChangeListener l ) {
changes.addPropertyChangeListener( l );
};
public void removeProperyChangeListener(
PropertyChangeListener l){
changes.removePropertyChangeListener( l );
};
Implementation firePropertyChange predefined method calls all registeredchange listeners
public void firePropertyChange( string propertyName,
Object oldValue, Object newValue);
Example
public void setBackgroundColor( java.awt.Color ColornewColor ) {
java.awt.Color oldColor = backgroundColor;
backgroundColor = newColor;
changes.firePropertyChange(backgroundColor,
oldColor, newColor );
};
7/28/2019 Colossal Java Bean
7/12
Property Listeners firePropertyChange predefined method calls all registered
change listeners which implements the interface:PropertyChangeListener
All listeners implements the interface:
PropertyChangeListener which has only 1 method:
public abstract void propertyChange( PropertyChangeEvent
evt );
Example
public class someButtonThatAlsoWantsToChangeColor
implements java.beans.PropertyChangeListener
{public void propertyChange( PropertyChangeEvent evt )
{
setBackgroundColor( inverse(evt.Color) );
};
Constrained Property Bean property is constrained when any change to it can be
vetoed.
Before a bean property changes: other beans might want to
stop that
change.
A bean which wants to stop the changes throws
PropertyVetoException
private VetoableChangeSupport vetos = newVetoableChangeSupport(this);
public void addVetoableChangeListener( VetoableChangeListenerl )
{ vetos.addVetoableChangeListener( l ); };
public void removeVetoableChangeListener(VetoableChan eListener l
7/28/2019 Colossal Java Bean
8/12
Property veto implementation
fireVetoableChange calls all registered veto listeners
public void setBackgroundColor( java.awt.Color ColornewColor ) {
java.awt.Color oldColor = backgroundColor;
vetos.fireVetoableChange( backgroundColor,
new java.awt.Color( oldColor ),
new java.awt.Color( newColor ) );
backgroundColor = newColor;
changes.firePropertyChange(backgroundColor,
oldColor, newColor );
};
Property veto Listeners fireVetoableChange calls all registered veto listeners Those listeners implement the vetoableListener interface
This interface has the method:
public abstract void vetoableChange(VetoableChangeEvent evt )
throws PropertyVetoException
Example:
public class ChangeColor implementsjava.beans.PropertyChangeListener
implements java.beans.VetoableChangeListener
{
public void propertyChange( PropertyChangeEvent evt );public void vetoableChange( VetoableChangeEvent evt )
throws PropertyVetoException
{
if( evt.id = BackgroundColor && evt.newColor == RED )
throw PropertyVetoExeption;
7/28/2019 Colossal Java Bean
9/12
Primary communication mechanism for Java Beans.
For plugging together components in application builders,Some
components act as sources and others as listeners ofevents.
Usually, events propagate state changes from the sourceObject to all its listeners.
Examples for events are mouse actions and widget updates.
Listener components need to register to receive events.
Event notifications are propagated by the event source viaimplicit method invocation.
Each Java Bean acts as an event server.
Event Handling
Event Mechanism
7/28/2019 Colossal Java Bean
10/12
Introspection
The process by which a builder tool finds out which
properties, methods, and events a bean supports.
searching for classes and methods that follow certain
naming conventions
by querying the BeanInfo of a class
BeanInfo is an interface whose methods allow the
interrogation of a beans properties, methods and
events
7/28/2019 Colossal Java Bean
11/12
Beans need to support a range of storage behavior for: the use of existing data formats (e.g. OpenDoc)
the storage of its internal state
Beans support two mechanisms:
use Java Serialization Mechanism which provides an
automatic way of storing and restoring the internalstate of
Java objects.
use Java Externalization Mechanism (extension ofabove)
which gives a class complete control over the writing ofits
state.
Besides properties (run-time state), beans also need to store
customized information (e.g. how to deal with references to
other beans).
Persistence
Saving an Objectpublic void SaveState()
{
FileOutputStream fos=null;
ObjectOutputStream oos=null;
try
{
fos=new FileOutputStream(State.ser);
oos=new ObjectOutputStream(fos);
oos.writeObject(currentState);
}catch(IOException e)
{
System.out.println(e.String());
}
}
7/28/2019 Colossal Java Bean
12/12
Loading an Object
public void loadState(){
FileInputStream fos=null;
ObjectInputStream oos=null;
try
{
fis=new FileInputStream(State.ser);
ois=new ObjectOutputStream(fis);
State currentState=(State)ois.readObject();
}
catch(ClassNotFoundException e)
{System.out.println(e.String());
}
}
Beans are packaged in a java archive (jar) file
the jar utility comes with the JDK
the jar file must be explicitly in your class path
in JDK 1.2.x all AWT and Swing classes are beans
Jarring the Bean