Вы находитесь на странице: 1из 11

MODULE 3 JAVA BEANS

Definition and Features


JavaBeans enables software developers to design and create reusable software components that integrate with each other, with applications and with development tools. JavaBean can be an entire application used alone or embedded within other application or component that can be used with other application. JavaBeans is a set of Java objects that provides the infrastructure for the components to be written in Java language. JavaBeans includes objects for Customizing, Introspecting, and Event handling. It provides the component framework from which reusable components can be built. JavaBeans provides the means to get component information. Using the Bean Development Kit the users can implement their own components as well as describe the interface specifications to their own components. It also enables the users to define the event mechanisms, properties, and public methods. A bunch of Beans can be assembled to form an application or an applet but a Bean is neither an applet nor an application. A Bean can be used within applet and within an application. Besides defining the structure of components and containers a component model is also responsible for providing the following services.

Support for introspection allowing a builder tool to analyze how a bean works. Support for customization allowing a user to alter the appearance and behavior of a bean. Support for events allowing beans to fire events, and informing builder tools about both the events they can fire and the events they can handle. Support for properties allowing beans to be manipulated programmatically, as well as to support the customization mentioned above. Support for persistence allowing beans that have been customized in an application builder to have their state saved and restored. Typically persistence is used with an application builder's save and load menu commands to restore any work that has gone into constructing an application.

While Beans are intended to be used primarily with builder tools, they need not be. Beans can be manually manipulated by text tools through programmatic interfaces. All key APIs, including support for events, properties, and persistence, have been designed to be easily read and understood by human programmers as well as by builder tools.

Basic Structure of a JavaBean

A Bean, like an object in object oriented technology is comprised of primary things data and methods that act on the data as shown in the following fig.

Data

Methods

A Bean is capable of accessing different types of methods. For instance, the private methods are accessible only within the internals of a bean, whereas protected methods are accessible internally and in derived beans. Methods with the most accessibility are the public methods, which are accessible internally from derived beans and from other applications and components. The public methods of Beans are grouped according to their function and these functionally similar groups of public methods are called Interfaces. Interface helps the beans in exposing its functionality. Interfaces specify the protocol by which a particular Bean is interacted externally. To successfully manipulate and interact with the Bean, a programmer needs to know a Beans interface. It provides support for facilities such as persistence and application builder tool integration.

Private Methods

Protected Methods Data Interface A Public Methods Interface B

Bean Development Kit


The Bean Development Kit is a tool that allows the user to configure and interconnect a set of beans. The user can change the properties of a Bean, link two or more Beans and execute Beans. The BDK provides an easy way to explore the capabilities of Beans written by others. The BDK is a Java application.

Designing a Bean

In designing a bean, the user has to fully evaluate the functional requirements of a bean. The planning process helps in developing the bean and maintaining it. Beans are usually small piece of software. Every bean developer should have three ideas down as questions are as follows. How does the bean do? How is the bean used? How can we change the bean in the future? The design of a bean ultimately should culminate in a detailed description of the various parts of a bean. The three primary parts of a bean are Properties Methods Events When bean is introduced to a system, a series of introspection steps takes place that determines the properties, methods and events of a bean. 1. Laying out the Properties The heart of a bean is its internal state. The internal state of a bean is reflected by the beans member variables. Properties are public member variables are accessible externally. A property is a way for a component to maintain its state. All the member variables are important to a beans functionality, because the properties enable the user to query a bean for information or to modify a beans state from outside the bean. The developer is allowed to read or write properties through accessor methods. The developer has to define all the bean properties during the initial design. The value of the property of one bean can be tied up to the value of the property contained in another bean. The user should decide if any of the properties are bound or constrained. Defining the property is an important step in the design of a bean. 2. Defining public methods After determining the set of suitable properties, the developer should decide on some methods that interact with them. The primary goal is to define the public interface that will be used to access the bean, which is determined by the public methods implemented by the bean. Defining the public methods starts with the accessor method, which enables the user to read and write the values of properties. Defining accessor methods is a simple task of listing getter and setter methods for each property based on its read/write capabilities as the developer has already defined the properties for the bean. These methods form the direct link between the user and the beans properties.

3. Communicating with Events JavaBeans use the standard Java event model as its foundation. In terms of bean design, there are two different ways in accessing events. The first has to do with the events that a bean is capable of firing. The functionality of a bean determines the types of events it is capable of firing. The bean can fire standard events defined in the Java API, or the user can choose to define their own custom events. The second way in which events come when the developer is designing beans relates to events that a bean handles for itself. For instance, a button bean would need to catch and respond to mouse clicks, focus changes and some key presses. These are the types of things the developers need to decide when they start writing the code.

Manipulating Bean Properties


Properties are one of the most important aspects of JavaBean because they represent the internal state of the bean. Properties are discrete, named attributes of a bean that determine its appearance and behaviour. They represent one of the primary means by which users interact with beans. An application developer can customize both the appearance and behaviour of beans by altering the values of the property. Accessor Methods Property access is one of the biggest responsibilities of the JavaBeans API. Property access is managed through accessor methods. They are public methods defined in a bean that enable access to the value of a particular property. Getter and Setter Methods Accessor methods that are responsible for reading a beans properties are known as getter methods and accessor methods responsible for writing a beans properties are known as setter methods. Getter and setter methods form interface with the beans properties and outside world. The user can never access a property from the outside of a bean directly. Fig. illustrates how accessor methods enable access to a beans internal properties.
Bean Getter methods Setter methods Properties (Data) External Application or bean

Other public methods Private methods

Working with Accessor methods Accessor methods follow a naming convention to specify the action they perform and on which property they perform it. The two actions available to accessor methods are getting and setting a property. The names of accessor methods are based on these actions as given below:

An accessor method that gets a property must begin with get. An accessor method that sets a property must begin with set.

For example, if there is a bean that has a property called color, then the getter method for the bean would be getColor() and the setter method would be setColor(). The definitions of a pair of accessor methods are given below public Color getColor() public void setColor (Color c) : The getColor() method returns a Color object : The setColor() method takes a Color object as its parameter

CUSTOMIZATION
Customization involves the editing and manipulation of bean properties in a design-time environment. Customization plays an important role in JavaBeans development because it provides the means by which beans can be integrated and used within a visual design tool. JavaBeans offers a much simpler and more automatic solution that enables developers to put minimal effort into constructing the visual side of bean customization. The following are the application builder tool support provided by JavaBeans for the customization of beans. 1) Property Editors The application builder tool support in JavaBeans begins with property editors, which are visual interfaces for editing a particular type of property. Property editors form the basis of bean customization because they are implemented at the property level, which facilitates reusing them in more comprehensive scenarios such as property sheets and customizers. The aim of the property editors is to enable us to add visual editing capabilities to every conceivable bean property type. With each property type having a corresponding editor, it then becomes possible to edit entire beans simply as group pf properties. JavaBeans provide built in Java data type such as integers, booleans and strings. 2) Property Sheets Editing a bean usually involves interacting with a group of property editors because beans usually contain multiple properties. To facilitate the editing of complete beans, JavaBeans provides property sheets, which are visual interfaces that consists of all the property editors necessary to edit the public properties a bean. Property sheets are usually implemented as dialog boxes that contain a variety of different individual property editors, depending on the specific properties within a bean. Property sheets are the interfaces you will most often think of when it comes to editing a bean because they group all of a beans properties into one convenient location.
3) Customizers

For those cases in which a property sheet doesnt quite do a bean justice, JavaBeans provides another option : customizers. Bean customizers are more elaborate visual interfaces that enable us to edit beans in a hopefully more intuitive fashion. Customizers are similar to property sheets in that they enable us to edit a complete bean, but they differ in that they take on a totally different approach to presenting the individual property editors. Most customizers act like wizards, which are visual editors that gather property information in a multiple step process. Customizers often attempt to provide editing facilities within the context of a series of questions. By presenting property information in the form of questions, the task of customizing a bean is made much simpler. Customizers are designed and implemented entirely by bean developers.

API Support for Bean Customization


Following are the classes and interfaces that make up the customization portion of the JavaBeans API: PropertyEditorManager PropertyEditorSupport Customizer PropertyEditor

PropertyEditorManager The PropertyEditorManager class is used to locate the property editor for a property of a given type. Property editors capable of being located by the PropertyEditorManager class must implement the PropertyEditor interface. PropertyEditorSupport
The PropertyEditorSupport class is a helper class implementing the PropertyEditor

interface that is used to make the construction of custom property editors a little easier. Customizer The Customizer interface defines the overhead required to provide a complete visual editor for a bean. Classes implementing the Customizer interface are typically derived from the Component class so that they can be used within the context of a dialog box or panel. PropertyEditor The PropertyEditor interface defines a means of visually editing a single bean property of a given type. Because JavaBeans provides standard property editors for built-in data types, we are only required to develop property editors for custom data types.

INTROSPCTION
The JavaBeans API provides a great deal of support for introspection, which enables any interested party to find out information about the structure and functionality of a bean. The introspection services provided by the JavaBeans API are divided into two parts: low-level services and high level services. The low-level services are typically used by application builder tools, which make heavy use of bean internals to provide advanced development features. This level of access isnt appropriate for application developers, because it expose bean internals that arent meant to be accessible at the application level. For developers, the high-level API services are more appropriate. The high-level services provide access to a limited portion of a beans internals, which typically consist of a beans public properties, methods, and events. The high-level introspection services rely on the low-level services; the primary difference between the two is that the high-level services limit access to bean internals. The first approach of JavaBeans automatically assessing information about a bean uses the reflection facilities in the core Java API. The reflection facilities are responsible for analyzing the low-level structure of a bean and returning information. The introspection facilities in the JavaBeans API take this information and apply design patterns to it to determine the specifics about the public properties, methods, and events supported by a bean. The explicit approach to defining bean information requires a little more work on the part of bean developers. The JavaBeans API provides support classes that are used by developers to explicitly define the public information for a bean.

API support for Introspection


Following are the classes and interfaces that make up the introspection portion of the JavaBeans API
BeanDescriptor MethodDescriptor ParameterDescriptor PropertyDescriptor EventSetDescriptor FeatureDescriptor IndexedPropertyDescriptor IntrospectionException Introspector SimpleBeanInfo BeanInfo

BeanDescriptor

The BeanDescriptor class provides global information about a bean, including the name of

the bean and the beans customizer.


MethodDescriptor

The MethodDescriptor class represents a publicly accessible method. This class provides methods for accessing information such as a methods parameters.
ParameterDescriptor

The ParameterDescriptor class represents the parameters to a method and is mainly provided as a means for bean developers to provide detailed parameter information. PropertyDescriptor The PropertyDescriptor class represents a publicly accessed property. This class provides methods for accessing the type of a property along with its accessor methods and whether it is bound or constrained EventSetDescriptor The EventSetDescriptor class represents a set of events that a bean is capable of generating. The events defined in an EventSetDescripto class are all deliverable as method calls on a single event listener interface. FetureDescriptor The FeatureDescriptor class serves as a common base class for the EventDescriptor, MethodDescriptor and PropertyDescriptor classes. It represents bean information that is common across these classes, such as the name of an event, method, or property. IndexedPropertyDescriptor The IndexedPropertyDescriptor class represents a publicly accessible indexed property. This class provides methods for accessing the type of an indexed property along with its accessor methods. IntrospectionException The IntrospectionException class is used to bring attention to an error that has occurred during introspection, such as an accessor method with an invalid type signature. Introspector

The Introspector class provides the overhead necessary to analyze a bean and determine its public properties, methods and events. SimpleBeanInfo The SimpleBeanInfo class is a support class designed to make it easier for bean developers to provide explicit information about a bean. This class basically implements every method in the BeanInfo interface but returns a value that triggers the automatic introspection services. This enables developers to provide selective bean information by overriding specific methods, without having to implement every method in the BeanInfo interface. BeanInfo The BeanInfo interface defines a set of methods that can be used to find out information explicitly provided by a bean, such as lists of properties, methods, and events. Developers who want to provide explicit bean information must implement the BeanInfo interface and provide suitable information. We can explicitly expose a Beans features in a separate, associated class that implements the BeanInfo interface. By associating a BeanInfo class with your Bean, we can: Expose only those fetures we want to expose. Rely on BeanInfo to expose some Bean features while relying on low-level reflection to expose others. Associate an icon with the target Bean. Specify a customizer class. Segregate features into normal and expert categories Provide a more descriptive display name, or additional information about a Bean feature. BeanInfo defines methods that return descriptors for each property, method, or event that you want exposed. Heres the prototypes for these methods: PropertyDescriptor[] getPropertyDescriptors(); MethodDescriptor[] getMethodDescriptors(); EventSetDescriptor[] getEventSetDescriptors(); Each of these methods returns an array of descriptors for each feature.

Вам также может понравиться