Você está na página 1de 27

Java Beans

What are Java Beans? ...


Definition:

“A Java bean is a reusable software component .They are


classes written in the Java programming language”

Software component is a software package that


encapsulates a set of related functions
Advantages of JavaBeans
 It enables developers to write reusable
components once and run them anywhere --
benefiting from the platform-independent power
of Java technology.
 This approach not only save time ,money and
effort but produce more reliable applications
 The goal of JavaBeans is to create a system
whereby application developers can take a set of
beans from a stock library and wire them together
to make a full application
Features of JavaBeans
 Support for introspection
 Allowing user to analyze how a bean works

 Support for customization


 allowing a user to alter the appearance and

behavior of a bean
 Support for events
 Allowing beans to communicate

 Support for properties


 Allowing the user to manipulate the bean

programmatically and support customization


 Support for persistence
 allowing a bean that have been customized to

save their state and reloaded later


Designing java bean
 Properties
 Events
 Introspection
 Customization
Properties
 Allowing the user to manipulate the bean
programmatically.
 Several types of properties:
 Simple
 Indexed

 Bound

 Constrained

A bean provide two methods for each property :


one to get the property and one to set the
property
Design Pattern rules

For any property named P of type T the get and set methods has the
following syntax
1.Simple Properties public T getP()
public void setP ( T value)
Here type can be integer or string
2.Boolean Properties public boolean isP()
public void setP(boolean value)
Here type is boolean
3.Indexed Properties public T[] getP()
public void setP(T[] values)
public T getP(int index)
public void setP(int index,T value)
Here type can be array of another type
Coding examples for Properties

package simpleBean.example;
public class SimpleBean
{
private String name;
private String password;
public String getName(){return name;}
public void setName(String name){ this.name =
name;}
public String getPassword(){return password;}
public void setPassword (String name)
{ this.password = password;}
}
Coding examples for Properties
package simpleBean.example;
public class ArrayBean
{
private String things[ ];
public String[ ] getThings()
{return things;}
public void setThings (String things)
{ this.things = things;}
public String getThings (int i)
{return things[i];}
public void setThings (int i, String things)
{ things[i] = things;}
Events
 A bean communicates with other beans by generating
events
 Two types of objects are involved:
 “Source” objects.

 “Listener” objects.

 Source fires event, recipient (listener) handles the event.


 Source must register listeners.
Event
source Register listener

Fire event Event


Event listener
Object
Bean Events
• Define a new Event class which extends
java.util.EventObject class.eg Type Event
• Define a new interface for listeners to implement, this
must be an extension of java.util.EventListener.
eg TypeListener
• The source bean provide add method for registering to
allow event listeners to notify when an E event occurs.
public void addTypeListener(TypeListener el)
The source bean provide remove method that allow a
listener to unregister an interest to specific event
public void removeTypeListener(TypeListener el).
Events
package eventBean.example;
public class PurchaseEvent extends Java.util.EventObject
{
private String itemName;
public PurchaseEvent(Object source, String itemName)
{
super(source);
this.itemName=itemName;
}
public String getItemName(){return itemName;}
}
Events...
package eventBean.example;
public interface PurchaseListener extends Java.util.EventListener
{
public void PurchaseMade(PurchaseEvent e){ }
}
Bound properties
 A property that generates a notification when
the property is modified is called a bound
property.
 It is a special event
 A Bean fire a PropertyChangeEvent when
one of its properties changes, in order to alert
other Beans (PropertyChangeListener) to the
change
 The get and set methods same as regular
property
 The add and remove methods for the registration
and unregistration of listeners that are notified
when any bound property value change are

public void
addPropertyChangeListener(
PropertyChangeListener el)
public void removePropertyChangeListener(
PropertyChangeListener el)  
Constrained property
 When bean tries to set a property to an unacceptable
value it generates or throws a VetoEvent such a
property is said to be constrained property
 It is also special event

 The source bean must send out a

PropertyChangeEvent to the list of interested


VetoableChangeListener objects. If any of these
objects throws a PropertyVetoException, the property
value is not changed, and propagated back to the
property set method.
public void setP(T) throws PropertyVetoException el)
 Methods for the registration and unregistration
of listeners that are notified when any
constrained property value changes
public void addVetoableChangeListener(
VetoableChangeListener el)
public void removeVetoableChangeListener(
VetoableChangeListener el)
Introspection
 A mechanism that allows analyzing a bean's design
patterns to reveal the bean's properties, events, and
methods.
 Two ways to analyze a bean:
 low-level service .

enabling wide access to the structural internals of a


bean to provide advanced development features by
application builders.
 High level service.

 provide access to limited portions of a bean's

internals,to develepors which typically consist of a


bean's public properties and methods
The JavaBeans supplies a set of classes and
interfaces to provide introspection
 Introspector class
determine the list of properties supported by a bean.
 BeanInfo interface
defines a set of methods that allow bean implementers
to provide additional information about their bean .
 FeatureDescriptor class
provide a name and brief description of features in the
bean.
 BeanDescriptor
.
This class provides global information about a bean.
 EventSetDescriptor

 This class represents a set of events that a bean is

capable of generating.
 MethodDescriptor

Represents a publicly accessible method This class


provides methods for accessing information such as a
method's parameters.
 PropertyDescriptor

This class provides methods for accessing the type of a


property along with its accessor methods and check
whether it is bound or constrained
 IndexedPropertyDescriptor
This class provides methods for accessing the type of an
indexed property along with its accessor method.
 ParameterDescriptor
This class provide detailed parameter information for
bean developers .
 FeatureDescriptor
This class serves as a common base class .It
represents bean information that is common across all
these classes, such as the name of an event, method, or
property.
Customization

 Customization allows a user to alter the


appearance and behavior of a bean
 It gives users the ability to visually modify a
bean’s properties to meet their needs.
 use property sheets to allow users to make
property changes.
 Property sheet displays a list of properties
associated with a bean.
 Each property listed should have a
property editor associated with it.
 A property editor is a class that allows you
to edit the property of a bean visually.
 All property editors must implement the
java.beans.PropertyEditor interface.
 The easiest way to create a simple
PropertyEditor is to extend the
java.beans.PropertyEditorSupport class
 It is a helper class that implements
PropertyEditor interface.
Customization using customizers
 For more sophisticated beans, the
JavaBeans has defined a Customizer
interface that behaves like a wizard
allowing the user to configure that bean in
useful way.
 Wizards are graphical user interfaces that
guide the user step by step through the
customization process of users configuring
beans.
 Customizers often attempt to provide
editing facilities within the context of a
series of questions

 All bean customizers must implement the


java.beans.Customizer interface and
extend the java.awt.Panel class.
Persistence
 Persistence is the mechanism by which
beans are stored in a place for later use.
 Persistence in JavaBeans is tightly linked
to serialization in java.
 Serialization is the process of reading or
writing an object to a file.
 JavaBeans uses serialization is in its
automatic approach to persistence, which
involves storing and retrieving a bean
based on its properties .

Você também pode gostar