Sunteți pe pagina 1din 6

JavaBeans

! JavaBeans is the component model for Java


Portable
JavaBeans •
• Platform-independent
The Component Model in Java • Written in Java
• API introduced in Feb. 1997
A bean is a reusable software component
CUGS !
! JavaBeans != Enterprise JavaBeans
Mikhail Chalabine
mikch@ida.liu.se
Reference: JavaBeans Tutorial @ java.sun.com

Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java

Programming model JavaBeans & Component Types


! Visual components
• Used in Swing, AWT
Assembling environment
• Visual application builders (visual composition)
JavaBean - Work flow: load, customize (size, color), save (persist)
- Eclipse VEP (Visual Editor Project) Button
JB instance
JavaBean - NetBeans
JB instance Applet,
JB instance Application,
JavaBean JB instance New bean
JB instance ! Non-visual components
JavaBean • In Java2EE, Hibernate
Server

Capture business
Client Network

logic or state JSP JavaBean Servlet
(outputs data) (carries) (produces data)

Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java
A reusable component in Java Beans’ core features
Class

! Hides implementation, conforms to interfaces,


encapsulates data Properties Events
! Is written to a standard (component specification)
• Implements the serializable interface (persistence)
• No-argument constructor
- E.g., instantiation through reflection
Methods
• Design patterns or BeanInfo class (introspection)
• Core features (methods, properties, events)

Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java

Beans’ features: Methods Bean’s features: Properties (1)

! Standard Java ! Appearance and behavior characteristics


import java.io.Serializable;

public class MyJavaBean implements Serializable {

! private String first_name;


Property color
! private float income;
...! Property size

! Visual components Button

• Builder tools can discover and expose


• Customization - modifying appearance or
Methods behavior at design time by
- Property editors (visual, programmable)
- Bean customizers (visual, programmable) Properties

Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java
Bean’s features: Properties (2) Bean’s features: Properties (3)
! Simple - A single-value bean
! Specification suggests to have ‘getters’ and property whose changes are P
‘setters’ independent of changes in any other
! public String getFirst_name() { property
! ! return first_name;
! }
! Indexed - A bean property that {P}
supports a range of values
! public void setFirst_name(String first_name) {
! ! this.first_name = first_name;
! Bound - A bean property for which
! } a change to the property results in a
notification being sent to some
P P’ ! JavaBean

! public float getIncome() { other bean


! ! return income;
! } ! Constraint - A bean property for !
which a change to the property P P’ JavaBean
! public void setIncome(float income) { results in validation by another
! ! this.income = income; bean. The other bean may reject the "
! }
change if it is not appropriate (veto). Properties

Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java

Bean’s features: Event model (1) Bean’s features: Event model (2)
! Fire (send) / handle (receive) notification ! Write event class
Event Action
Components broadcast events - Create your own custom event class named <NAME>Event or
use an existing event class, e.g. ActionEvent
and the underlying framework ActionEvent e;
delivers the events to the
components to be notified java.awt.event.MouseEvent ! Write event listener (handler, receiver)
- write <NAME>Listener interface and provide implementation of it
! Sources File Edit Window
or reuse existing listener interfaces, e.g., ActionListener
• Define and fire events or complete handlers, so-called, adapters, e.g. MouseAdapter()
~Click!~
public class ButtonHandler implements ActionListener() {...}
• Define methods for registering
listeners Button ! Write event source bean (Event generator)
- JButton button = new JButton(“Fire”);
! Listeners - In your custom bean implement add<NAME>Listener() and
• Get notified of events Source
remove<NAME>Listener() methods. Implemented in JButton.
• Register using methods defined Events
! Register event listener Events
by sources Listener button.addActionListener( new ButtonHandler());
Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java
Example: Alarm Clock Example

! Properties
• Current time class AlarmClock implements Serializable {
• Alarm time public AlarmClock() {…}
• Alarm status (set/not set)
public boolean getAlarmStatus() {…}
! Events
public void setAlarmStatus(boolean value) {
• Alarm (source) …
}


}

Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java

Example cont. Advanced features


CoffeeMachine
! Optional class (see below): AlarmClockInfo
alarmStarted(AlarmEvent e)
alarmStoped(AlarmEvent e) <<interface>> ! Optional class (study): AlarmClockCustomizer
AlarmListener
StereoSystem
alarmStarted(AlarmEvent e)
alarmStoped(AlarmEvent e)
alarmStarted(AlarmEvent e)
alarmStoped(AlarmEvent e)

AlarmClock AlarmEvent
Set of listeners

+addAlarmListener(AlarmListener l): void


+removeAlarmListener(AlarmListener l): void

Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java
Discovering features through introspection Reminder (lecture on Java Reflection)

! We concluded that Representing metalevel concepts Reflection is computations


- For a bean to be the source of an event, it must implement at the base level is called reification. about the metamodel in the
methods to add and remove listener objects for that type of base model
event /* Instantiate a metaobject */
Robot ourRobot= new Robot(...);
add<EventName>Listener(<EventName>Listener listener) New Metadata
remove<EventName>Listener(<EventName>Listener listener) /* Obtain its (meta) class */ Metadata
- For a bean to be the listener of an event, it must implement the Class rClass= ourRobot.getClass();
<EventName>Listener interface
rClass represents (reifies) the class Meta level
! We see that meta-level concept at the base level. Base level
- ‘add’, ‘remove’, ‘Listener’, <BeanClass>Info,
<BeanClass>Customizer form syntactic patterns
! We also said that Locate classes, methods, data accesses
Allocate new classes, methods, fields Computations
- component specification suggests that bean properties should
have setters and getters Remove classes, methods, fields

Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java

Discovering beans’ features (1) Discovering beans’ features (2)

! Automatic (implicit) ! Manual (explicit)


• adhering to design patterns makes a bean’s • BeanInfo class (visual components)
features discoverable through introspection - Code that defines and initializes properties
- java.io.Serializable;
import
- Make properties visible / invisible, etc.
- Expose / hide methods that the bean implements
public class MyJavaBean implements Serializable { - setHidden() etc.

! private String first_name;


!
- Use when
- Bean code does not follow the standard naming convention (no
! public String getFirst_name() {
introspection possible)
! ! return first_name;
! }
- You intend to hide some features
! public void setFirst_name(String first_name) {
! ! this.first_name = first_name;
! }
...

Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java
Further topics Evaluation

! Java techniques ! Strengths


• Serialization and persistence in JavaBeans • Simple - easy to use
! JavaBeans • Standard - mix vendors
• More on customizers • Applicable for GUI development
• More on properties ! Weaknesses
• Only suitable for GUI development
• Not usable for non-programmers
• Weak component market

Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java Mikhail Chalabine mikch@ida.liu.se JavaBeans - the Component Model in Java

S-ar putea să vă placă și