Java Bean Definition “A Java Bean is a reusable software component that can be manipulated...
-
Upload
homer-mcbride -
Category
Documents
-
view
222 -
download
0
Transcript of Java Bean Definition “A Java Bean is a reusable software component that can be manipulated...
Java Bean Definition
“A Java Bean is a reusable software component that can be manipulated visually in a builder tool.”
JavaBean
• Properties– Similar to instance variables.
• Methods– Same as normal Java methods.– Every property should have accessor (get) and mutator (set)– method.
• Events– Similar to Swing/AWT event handling.
Features of a Java Bean• Support for “introspection” so that a builder tool can analyze how
a bean works.
• Support for “customization” to allow the customisation of the appearance and behaviour of a bean.
• Support for “events” as a simple communication metaphor than can be used to connect up beans.
• Support for “properties”, both for customization and for programmatic use.
• Support for “persistence”, so that a bean can save and restore its customized state.
Why use JavaBeans?
• Component Granularity
• Portability
• Simplicity
• Uniform, high-quality API that will enable developers to write programs that will perform consistently everywhere.
JavaBeans Vs Class Libraries
• Beans should be used for software components that can be visually manipulated and customized.
• Class libraries should be used to provide functionality to programmers, but which doesn't benefit from visual manipulation.
Java Beans Concepts• A component is a self-contained reusable software unit
• Components expose their features (public methods and events) to builder tools
• A builder tool maintains Beans in a palette or toolbox.
• You can select a bean from the toolbox, drop it in a form, and modify its appearance and behavior.
• Also, you can define its interaction with other beans
• ALL this without a line of code.
Java Bean Characteristics
• a public class with 0-argument constructor
• it has properties with access methods
• it has events
• it can be customized
• its state can be saved
• it can be analyzed by a builder tool
Key Concepts
• A builder tool discover a bean’s features by a process known as introspection.– Adhering to specific rules (design pattern)
when–naming Bean features.– Providing property, method, and event
information–with a related Bean Information class.
• Properties (bean’s appearance and behavior characteristics) can be changed at design-time.
Key Concepts….
• Properties can be customized at design-time.• Customization can be done:
– using property editor– using bean customizers
• Events are used when beans want to intercommunicate
• Persistence: for saving and restoring the state• Bean’s methods are regular Java methods. • Beans will also be running in a multi-threaded
environment
Security Issues
• JavaBeans are subject to the standard Java security model
• The security model has neither been extended nor relaxed.
• If a bean runs as an untrusted applet then it will be subject to applet security
• If a bean runs as a stand-alone application then it will be treated as a normal Java application.
• Beans Development Kit (BDK)
– To start the BeanBox:• run.bat (Windows)• run.sh (Unix)
BDK
• Tool Box contains the beans available
• Bean Box window is the form where you visually wire beans together.
• Properties sheet:– displays the properties for the Bean currently
selected within the BeanBoxwindow.
MyFirstBeanimport java.awt.*;import java.io.Serializable;public class FirstBean extends Canvas implementsSerializable
{public FirstBean()
{setSize(50,30);setBackground(Color.blue);
} }
First Bean
• Compile: javac FirstBean.java
• Create a manifest file:– manifest.txt (jar file metadata)
• Name: FirstBean.Class• Java-Bean: True
• Create a jar file:– jar cfm FirstBean.jar mani.txt FirstBean.class
Properties
• Bean’s appearance and behavior– changeable at design time.
• They are private values
• Can be accessed through getter and setter methods getter and setter methods must follow some rules -- design patterns (documenting experience)
Design Patterns for properties
• Property is a subset of a bean’s state– Simple properties
• Has a single value– Public T getN();– Public void setN(T arg);– Public boolean isN(); ( isdotted)
– Indexed properties• Consists of multiple values
– Public T getN(index);– Public void setN(int index, T value);– Public T[ ] getN();– Public void setN(T values[ ]);
•
Introspection
• Process of analyzing a bean to determine its capabilities
• Allows an application builder tool to present information about a software component to the designer
– Simple naming conventions– A class that explicitly supplies this information
Properties
• A builder tool can:
– discover a bean’s properties– determine the properties’ read/write attribute– locate an appropriate “property editor” for
each type– display the properties (in a sheet)– alter the properties at design-time
Types of Properties
• Simple
• Index: multiple-value Properties
• Bound: provide event notification when value changes
• Constrained: how proposed changes can be okayed or vetoed by other object
Simple Properties
• When a builder tool introspect your bean it discovers two methods:
– public Color getColor()– public void setColor(Color c)
• The builder tool knows that a property named “Color” exists -- of type Color.
• It tries to locate a property editor for that type to display the properties in a sheet.
Simple Properties….• Adding a Color property
– Create and initialize a private instance variable
• private Color color = Color.blue;
– Write public getter & setter methods
public Color getColor(){return color;}public void setColor(Color c) {color = c;–repaint();}
Indexed Properties
• An indexed property is for when a single property can hold an array of values.
– The design pattern for these properties is:
public void setPropertyName (PropertyType[] list)
public void setPropertyName ( PropertyType element, int position)
public PropertyType[] getPropertyName ()
public PropertyType getPropertyName (int position)
• For instance, if you were to complete the item indexed property for the AWT List component, it might look something like this:
public class ListBean extends List { public String[] getItem () { return getItems (); } public synchronized void setItem (String item[]) { removeAll(); for (int i=0;i<item.length;i++) addItem (item[i]); } public void setItem (String item, int position) { replaceItem (item, position) } }
The String getItem (int position) routine already exists for List
Bound Properties• Provide event notification when value changes
• In order for the notification to happen, you need to maintain a watch list for PropertyChangeEvents via the PropertyChangeSupport class.
• First, you have to create a list of listeners to maintain: private PropertyChangeSupport changes =
new PropertyChangeSupport (this);
And then, you have to maintain the list: public void addPropertyChangeListener (PropertyChangeListener p)
{ changes.addPropertyChangeListener (p);} public void removePropertyChangeListener ( PropertyChangeListener p) { changes.removePropertyChangeListener (p); }
Constrained properties• Constrained properties are similar to bound
properties.
• In addition to maintaining a list of PropertyChangeListeners, the Bean maintains a list of VetoableChangeListeners.
• Prior to the Bean changing a property value, it asks the VetoableChangeListeners if its okay.
• If it isn't, the listener throws a PropertyVetoException, which you declare the set routine to throw.
Adding…private VetoableChangeSupport vetoes = new VetoableChangeSupport (this);
public void addVetoableChangeListener ( VetoableChangeListener v) {
vetoes.addVetoableChangeListener (v); }
public void removeVetoableChangeListener ( VetoableChangeListener v) { vetoes.removeVetoableChangeListener (v); }