component-based software engineering introduction to java beans paul krause and sotiris...
TRANSCRIPT
Component-Based Component-Based Software EngineeringSoftware Engineering
Introduction to Java BeansIntroduction to Java Beans
Paul KrausePaul Krause
andand
Sotiris MoschoyiannisSotiris Moschoyiannis
Java BeansJava Beans
ContentsContents
DefinitionDefinition Bean BasicsBean Basics
What is a Java Bean?What is a Java Bean?
““A Java Bean is a reusable software A Java Bean is a reusable software component that can be manipulated component that can be manipulated visually in a builder tool”visually in a builder tool”
JavaSoftJavaSoft
Sources of Builder ToolsSources of Builder Tools
NetBeans:NetBeans:http://www.http://www.netbeansnetbeans.org.org
JBuilder:JBuilder:http://www.http://www.borlandborland.com/.com/jbuilderjbuilder//
Note, you also need to have the Java Note, you also need to have the Java SDK installed.SDK installed.
Bean BooksBean Books
Developing Developing Java BeansJava Beans
Robert Robert EnglanderEnglander
O’ReillyO’Reilly RecommendedRecommended
JavaBeans JavaBeans ProgrammingProgramming
J. O’Neil and J. O’Neil and
H. SchildtH. Schildt
McGraw HillMcGraw Hill Good. Contains Good. Contains many examples.many examples.
JavaBeans by JavaBeans by ExampleExample
Henri JubinHenri Jubin Prentice HallPrentice Hall Simpler than the Simpler than the above, but with above, but with useful examplesuseful examples
NetBeans – the NetBeans – the Definitive GuideDefinitive Guide
Boudreau, Boudreau, Glick, Greene, Glick, Greene, Spurlin, WoehrSpurlin, Woehr
O’ReillyO’Reilly Not so good on Not so good on Beans. More Beans. More about the IDE.about the IDE.
Java BeansJava Beans
ContentsContents
DefinitionDefinition Bean BasicsBean Basics
The Component ModelThe Component Model
JavaBeans is Java’s component modelJavaBeans is Java’s component model The model is made up of an architecture The model is made up of an architecture
and an APIand an API The API makes it possible to write The API makes it possible to write
component software in Javacomponent software in Java The architecture provides the framework The architecture provides the framework
(services and rules) that allows (services and rules) that allows components to participate properlycomponents to participate properly
The Component ModelThe Component Model
Discovery and RegistrationDiscovery and Registration Raising and Handling of EventsRaising and Handling of Events PersistencePersistence Visual PresentationVisual Presentation Support for Visual ProgrammingSupport for Visual Programming
The Component ModelThe Component Model
Discovery and RegistrationDiscovery and Registration
Locate a component at run-time and Locate a component at run-time and determine its supported interfacesdetermine its supported interfaces
Registration process for a component to make Registration process for a component to make itself and its interfaces knownitself and its interfaces known
This mechanism allows components and This mechanism allows components and applications to be developed independentlyapplications to be developed independently
The Component ModelThe Component Model
Raising and Handling of EventsRaising and Handling of Events
Beans (or JavaBeans components) use Beans (or JavaBeans components) use events to communicate with other Beansevents to communicate with other Beans
A Bean that wants to receive events (a A Bean that wants to receive events (a listener listener Bean) registers its interest with the Bean) registers its interest with the Bean that triggers the event (a Bean that triggers the event (a sourcesource Bean) Bean)
The Component ModelThe Component Model
PersistencePersistence
Persistence enables Beans to save and Persistence enables Beans to save and restore their staterestore their state
JavaBeans uses Java Object Serialization to JavaBeans uses Java Object Serialization to support persistence support persistence
The Component ModelThe Component Model
Visual PresentationVisual Presentation
The Bean is free to choose its own visual The Bean is free to choose its own visual presentation (fonts, colours, shape, etc)presentation (fonts, colours, shape, etc)
Many of these characteristics will be Many of these characteristics will be properties of the Bean (some might be properties of the Bean (some might be persistent too)persistent too)
The Component ModelThe Component Model
Support of Visual ProgrammingSupport of Visual Programming
User can select a component from the User can select a component from the toolbox and place it into a containertoolbox and place it into a container
Properties of the component can then be Properties of the component can then be edited to create the desired behaviouredited to create the desired behaviour
Bean’s PropertiesBean’s Properties
Properties are a Bean’s appearance and Properties are a Bean’s appearance and behaviour characteristics that can be behaviour characteristics that can be changed at design timechanged at design time
By following specific naming conventions, By following specific naming conventions, the properties of a Bean that are the properties of a Bean that are “revealed” to the world can be identified“revealed” to the world can be identified
Conventions for Access Conventions for Access MethodsMethods
Simple Properties:Simple Properties: For a property of type For a property of type TypeType and name and name Name:Name: public public TypeType get getNameName( );( ); public void setpublic void setNameName((TypeType valuevalue););
Boolean Properties:Boolean Properties: public boolean ispublic boolean isNameName( );( ); public void setpublic void setNameName(boolean (boolean valuevalue););
Bean MethodsBean Methods
A Bean may be implemented by a Java A Bean may be implemented by a Java ClassClass
That Class contains a number of methods That Class contains a number of methods that may be used to access and control that may be used to access and control the Beanthe Bean
These are generally all the public methods These are generally all the public methods of the Class that implements the Beanof the Class that implements the Bean
EventsEvents
JavaBeans components interact by JavaBeans components interact by generating “Events”generating “Events”
Several components may register an Several components may register an interest in an Event that is generated by a interest in an Event that is generated by a specific componentspecific component
Occurrence of the Event triggers methods Occurrence of the Event triggers methods to be called in all the components that are to be called in all the components that are “listening” for it“listening” for it
IntrospectionIntrospection
The process by which builder tools The process by which builder tools discover a Bean’s featuresdiscover a Bean’s features
Beans support introspection in two ways:Beans support introspection in two ways: By adhering to specific rules (By adhering to specific rules (design patternsdesign patterns) )
when naming properties, methods and eventswhen naming properties, methods and events By explicitly providing property, method and By explicitly providing property, method and
event info within a event info within a Bean InformationBean Information class class
IntrospectionIntrospection
Low-level reflection:Low-level reflection: Follow Bean coding style (we have seen)Follow Bean coding style (we have seen) Analysis of the Bean’s class can then reveal Analysis of the Bean’s class can then reveal
properties and methodsproperties and methods Revealing complex properties:Revealing complex properties:
Implement a “BeanInfo” classImplement a “BeanInfo” class
CustomisationCustomisation
Beans expose properties so they can be Beans expose properties so they can be customised at design timecustomised at design time
Customisation is supported in two ways:Customisation is supported in two ways: By using property editorsBy using property editors By creating more sophisticated Bean By creating more sophisticated Bean
customiserscustomisers
CustomisationCustomisation
Simple propertiesSimple properties Development tool will build property sheets Development tool will build property sheets
dynamicallydynamically User may then edit the properties to User may then edit the properties to
customise the Beancustomise the Bean For the Advanced UserFor the Advanced User
Create a specific customiser for a BeanCreate a specific customiser for a Bean This is kept separate to the Bean Class, as This is kept separate to the Bean Class, as
with a BeanInfo Classwith a BeanInfo Class
Further FeaturesFurther Features
VisibilityVisibility
It is not necessary for a Bean to be visible at It is not necessary for a Bean to be visible at run-time (e.g. Bean controlling access to a run-time (e.g. Bean controlling access to a device or data feed)device or data feed)
It is necessary however for a Bean to support It is necessary however for a Bean to support the visual builder tool. Even an ‘invisible’ run-the visual builder tool. Even an ‘invisible’ run-time Bean shall be shown on the builder tooltime Bean shall be shown on the builder tool
Further FeaturesFurther Features
MultithreadingMultithreading
Always assume your code will be used in a Always assume your code will be used in a multithreaded environmentmultithreaded environment
Make sure that your Beans are thread-safeMake sure that your Beans are thread-safe Multithreading in JavaBeans is no different Multithreading in JavaBeans is no different
than multithreading in Javathan multithreading in Java
Further FeaturesFurther Features
SecuritySecurity
By default assume that your Beans are By default assume that your Beans are running in a non-trusted appletrunning in a non-trusted applet
Apply security restrictions such asApply security restrictions such as• Allow no access to the local file systemAllow no access to the local file system• Limit socket connections to the host systemLimit socket connections to the host system
SummarySummary
Beans build on Java features that already Beans build on Java features that already existexist
We add a Builder ToolWe add a Builder Tool We use design patternsWe use design patterns We record information about the Classes We record information about the Classes
that implement Beansthat implement Beans