Title: Java Beans
1Java Beans
- RickardEnander_at_parallelconsulting.com
2Who is Rickard Enander?
- Uppsala Universitet 97
- Ph.D in something unimportant
- IBM 97-98
- methodology, OO, et.c.
- Parallel Consulting Group 98 -gt
- serious internet application
- Participated in courses like this one at KTH
3Whats demanded from you today?
- Some knowledge about Java
- Interest for components
- Patience with the demo daemons
4Agenda
- Architecture, component models
- Goals with the JavaBeans model
- Ways of working
- The interface of a Bean
- Properties
- Event model
- Something more about Java facilities
5ArchitecturesClient/Server, 3-tier and N-tier
- The evolution for architecture is
- Client/server (2-tier)
- 3-tier
- N-tier
- Why?
- A changing world...
6Component model mostly resides in the middle tier
- Component and component model?
- Many different definitions
- Different goals
- JavaBeans vs. CORBA
- Different with respect to OO-fundamentals
- CORBA vs. COM/DCOM
- Binaries or not
- COM vs JavaBeans
7Different definitions of the term component
A component is a piece of software small enough
to create and maintain, big enough to deploy and
support and with standard interfaces for
interoperability (Jed Harris)
A non-trivial, nearly independent, and
replaceable part of a system that fulfills a
clear function in the context of a well-defined
architecture. A component conforms to and
provides the physical realization of a set of
interfaces. RUP
8Component models today is very religious
- Statefull vs. stateless components
- should a component have state or not
- Microsoft vs. Java
- depends on who is talking
- think for yourselves
- dont talk before you know what your customer
prays to
9Modern component models often have a clear
programming model
- Programming model
- how we shall/should do
- different roles developing / distributing
- Without any strong programming model
- CORBA, COM/DCOM
- With a strong programming model
- JavaBeans but most EJB
10Components in the daily work
- Component based development
- Build apps from ready and tested things
- Small amount of new code lines
- Make connections graphically
- Pros
- Spread of investment
- Third party
- Better focus in own development
11Components in the daily work...
- Properties of a good component
- Is a well defined operation or entity
- Is general enough to be reused
- Configurable
- Is able to present its own interface
- Possible to assemble larger components
- Self containing ? possible to distribute
12Agenda
- Architecture, component models
- Goals with the JavaBeans model
- Ways of working
- The interface of a Bean
- Properties
- Event model
- Something more about Java facilities
13Goals of the JavaBeans model
- A Bean in JavaBeans is a reusable software
component that can be manipulated visually in a
builder tool. - Should be attractive to reuse
- Object oriented development
- Tools at assembly time should not influence run
time performance
14Java Beans is really a two class pattern
myClass
myClassBeanInfo
15JavaBeans is supported by some Java language
features
- Name convention
- Introspection / Refelction
- Event model
- Inner classes
- NLS - National Language Support
- Serializing
16The naming convention is simple must be followed
- Name convention
- Introspection / reflection
- Event model
- Inner classes
- NLS - National Language Support
- Serializing
- Setters and getters
- Events
17Introspection means that a tool can figure out
a JavaBean
- Name convention
- Introspection / reflection
- Event model
- Inner classes
- NLS - National Language Support
- Serializing
- Is built on the naming convention
- Is supported by Java Reflection
18The event model makes JBs talk without previous
knowledge
- Name convention
- Introspection / reflection
- Event model
- Inner classes
- NLS - National Language Support
- Serializing
- Built on observer pattern
- The most complicated part
- Inner classes is a language construction used to
support Events
19NLS is not JavaBean specific but needed for the
web
- Name convention
- Introspection / reflection
- Event model
- Inner classes
- NLS - National Language Support
- Serializing
- Many language problem
- Not easy but easier than with other techniques
- We will not handle this explicitly
20Serializing makes JavaBeans possible to persist
and transport
- Name convention
- Introspection / reflection
- Event model
- Inner classes
- NLS - National Language Support
- Serializing
- Was defined to support JB
- Also used in other situations
- Elegant
- Slow
21Agenda
- Architecture, component models
- Goals with the JavaBeans model
- Ways of working
- The interface of a Bean
- Properties
- Event model
- Something more about Java facilities
22Ways of working
- Configure and combine Beans to an application or
to a more complicated beans with a graphical tool - Save the assembled Bean
- identity
- initial state
- behaviour
- Distribute the Bean
23Three roles can be identified
- Developer of JavaBeans
- needs deep knowledge
- Toolmaker (and tool)
- needs deeper knowledge
- End user (bean assembler)
- needs to be able to draw straight lines -)
24Agenda
- Architecture, component models
- Goals with the JavaBeans model
- Ways of working
- The interface of a Bean
- Properties
- Event model
- Something more about Java facilities
25The interface of a Bean
- The interface is defined by
- Properities
- Events
- Methods
26The interface of a Bean ...
- How do you get the interface?
Introspection
BeanInfo
The BeanInfo-class is fetched
Discover by reflection
27The interface of a Bean ...
- BeanInfo
- getAdditionalBeanInfo()
- getBeanDescriptor()
- getDefaultEventIndex()
- getDefaultPropertyIndex()
- getEventSetDescriptors()
- getIcon(int)
- getMethodDescriptors()
- getPropertyDescriptors()
28Creation of BeanInfo
- The Introspector class creates BeanInfo
- BeanInfo class from developer
- Reflection
- Combination
- Reflection Class java.lang.Class
- Can get iformation about everything
- Can create instances
29Rules for a JavaBean
- A normal Java class
- Can be instanciated
- Has a constructor without arguments
- Implements java.io.Serializable
- Methods follows the naming conventions
- Eventually a BeanInfo-class
30Naming conventions
- Properties defined by name
- lttypegt x
- lttypegt getX() //Read
- void setX(lttypegt value) //Write
- isX() //If Boolean
- Example property Temperature
- int temperature
- int getTemperature()
- void setTemperature( int value )
31Agenda
- Architecture, component models
- Goals with the JavaBeans model
- Ways of working
- The interface of a Bean
- Properties
- Event model
- Something more about Java facilities
32Different types of properties
- Normal
- Indexed
- Bound
- Constrained
33Different types of properties Indexed
- A number of values indexed with an int
- lttypegt getX( int index )
- void setX( int index, lttypegt value )
- Implemented e.g. with an array
34Different types of properties Bound
- When the property value is changed all registered
listeners are notified - java.beans.PropertyChangeEvent
- Listeners registered with
- bean.addPropertyChangeListener(listener)
35Different types of properties Constrained
- When a property value is going to be changed the
change must be OKed - exception thrown for veto
- silent accept
- Property methods thows exception
- lttypegt getX()
- void setX( lttypgt value ) thows java.beans.Property
VetoException
36Change of properties in tool
- Tool editors
- Simple editorer, for simple data types
- Inherits from
- PropertyEditor - interface
- PropertyEditorSupport - class
- Coupling between property an special editor is
done in BeanInfo class - Complex editor, Cutomizer
37Change of properties in tool Customizer
- Class supporting the Interface java.bean.Customize
r - More complex customization
- Implemented by graphical tool used for
customization - Coupling between property an special editor is
done in BeanInfo class
38Agenda
- Architecture, component models
- Goals with the JavaBeans model
- Ways of working
- The interface of a Bean
- Properties
- Event model
- Something more about Java facilities
39Events
Source
Listener
Eventobject
40Event object
- Contains data about the event
- Source
- Other specific data
- Named to xxxEvent
- Example
- KeyEvent
- MouseEvent
41Event source
- If the source can generate the event xxx the
following methods are present - addxxxListener
- removexxxListener
- manages list of listeners
- Notifies the listener when xxx happens
42Event Listener
- Support the xxxListener interface
- must contain at least one methos
- may contain many methods
- Listeners can use adaptors
- An adaptor is an abstarct class supporting a
listener interface - the logical listener has a inner class inheriting
from the adaptor class
43Example of the use of an inner calss and an
adaptor
import java.awt.event. public class MyPanel
extends Canvas public MyPanel()
addMouseListener( new MyMouseListener() )
class MyMouseListener extends MouseAdapter
public void mouseClicked(MouseEvent e)
// Take care of mouse click here // or call
other method
44Event Class Diagram
xxxSource
yyySource
xxxAdapter
yyyAdapter
xxxAdapter- Child
yyyAdapter- Child
MyObectMultipleListener
45Example of classes for MouseEvent
- MouseEvent
- MouseListener
- MouseAdapter
- Source must implement
- addMouseEventListener(MouseListener m)
- removeMouseEventListener(MouseListener m)
46To think about when using Events
- You can define your own events
- extends AWTEvent
- tools will help you
- Events is executed from the source thread
- the listener should be synchronizerad
- How to handle exceptions
- the source can not handle then
47Agenda
- Architecture, component models
- Goals with the JavaBeans model
- Ways of working
- The interface of a Bean
- Properties
- Event model
- Something more about Java facilities
48Serializing
- To make an object to a bit stream
- Is automatically done
- class A implements Serializable
- If the standard behaviour is not enough, override
the methods - writeObject
- readObject
49Core Reflection, very short
- Uses the class Class for inspection
- Class xClass Class.forName(X)
- Mehod xMethods xClass.getMethods()
- Can also create instances
- X xInstance xClass.newInstance()
50Internationalization (NLS)
- Pattern for NLS
- Uses the classes
- Locale
- ResourceBoundle
- also on file
- Locally in all objects
- X.asString()
- X.format( Locale ) or X.format()