Java Beans - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

Java Beans

Description:

CORBA vs. COM/DCOM. Binaries or not. COM vs JavaBeans. Different definitions of the ... CORBA, COM/DCOM. With a strong programming model. JavaBeans but most EJB ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 51
Provided by: tommyma
Category:
Tags: beans | dcom | java

less

Transcript and Presenter's Notes

Title: Java Beans


1
Java Beans
  • RickardEnander_at_parallelconsulting.com

2
Who 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

3
Whats demanded from you today?
  • Some knowledge about Java
  • Interest for components
  • Patience with the demo daemons

4
Agenda
  • Architecture, component models
  • Goals with the JavaBeans model
  • Ways of working
  • The interface of a Bean
  • Properties
  • Event model
  • Something more about Java facilities

5
ArchitecturesClient/Server, 3-tier and N-tier
  • The evolution for architecture is
  • Client/server (2-tier)
  • 3-tier
  • N-tier
  • Why?
  • A changing world...

6
Component 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

7
Different 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
8
Component 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

9
Modern 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

10
Components 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

11
Components 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

12
Agenda
  • Architecture, component models
  • Goals with the JavaBeans model
  • Ways of working
  • The interface of a Bean
  • Properties
  • Event model
  • Something more about Java facilities

13
Goals 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

14
Java Beans is really a two class pattern
myClass
myClassBeanInfo
15
JavaBeans is supported by some Java language
features
  • Name convention
  • Introspection / Refelction
  • Event model
  • Inner classes
  • NLS - National Language Support
  • Serializing

16
The naming convention is simple must be followed
  • Name convention
  • Introspection / reflection
  • Event model
  • Inner classes
  • NLS - National Language Support
  • Serializing
  • Setters and getters
  • Events

17
Introspection 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

18
The 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

19
NLS 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

20
Serializing 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

21
Agenda
  • Architecture, component models
  • Goals with the JavaBeans model
  • Ways of working
  • The interface of a Bean
  • Properties
  • Event model
  • Something more about Java facilities

22
Ways 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

23
Three 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 -)

24
Agenda
  • Architecture, component models
  • Goals with the JavaBeans model
  • Ways of working
  • The interface of a Bean
  • Properties
  • Event model
  • Something more about Java facilities

25
The interface of a Bean
  • The interface is defined by
  • Properities
  • Events
  • Methods

26
The interface of a Bean ...
  • How do you get the interface?

Introspection
BeanInfo
The BeanInfo-class is fetched
Discover by reflection
27
The interface of a Bean ...
  • BeanInfo
  • getAdditionalBeanInfo()
  • getBeanDescriptor()
  • getDefaultEventIndex()
  • getDefaultPropertyIndex()
  • getEventSetDescriptors()
  • getIcon(int)
  • getMethodDescriptors()
  • getPropertyDescriptors()

28
Creation 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

29
Rules 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

30
Naming 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 )

31
Agenda
  • Architecture, component models
  • Goals with the JavaBeans model
  • Ways of working
  • The interface of a Bean
  • Properties
  • Event model
  • Something more about Java facilities

32
Different types of properties
  • Normal
  • Indexed
  • Bound
  • Constrained

33
Different 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

34
Different types of properties Bound
  • When the property value is changed all registered
    listeners are notified
  • java.beans.PropertyChangeEvent
  • Listeners registered with
  • bean.addPropertyChangeListener(listener)

35
Different 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

36
Change 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

37
Change 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

38
Agenda
  • Architecture, component models
  • Goals with the JavaBeans model
  • Ways of working
  • The interface of a Bean
  • Properties
  • Event model
  • Something more about Java facilities

39
Events
  • Delegation model

Source
Listener
Eventobject
40
Event object
  • Contains data about the event
  • Source
  • Other specific data
  • Named to xxxEvent
  • Example
  • KeyEvent
  • MouseEvent

41
Event 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

42
Event 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

43
Example 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
44
Event Class Diagram
xxxSource
yyySource
xxxAdapter
yyyAdapter
xxxAdapter- Child
yyyAdapter- Child
MyObectMultipleListener
45
Example of classes for MouseEvent
  • MouseEvent
  • MouseListener
  • MouseAdapter
  • Source must implement
  • addMouseEventListener(MouseListener m)
  • removeMouseEventListener(MouseListener m)

46
To 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

47
Agenda
  • Architecture, component models
  • Goals with the JavaBeans model
  • Ways of working
  • The interface of a Bean
  • Properties
  • Event model
  • Something more about Java facilities

48
Serializing
  • 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

49
Core 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()

50
Internationalization (NLS)
  • Pattern for NLS
  • Uses the classes
  • Locale
  • ResourceBoundle
  • also on file
  • Locally in all objects
  • X.asString()
  • X.format( Locale ) or X.format()
Write a Comment
User Comments (0)
About PowerShow.com