Pages

Wednesday 15 January 2014

Java Beans

A Java Bean is a reusable software component (actually, a Java class) that can be manipulated visually in a builder tool
Java Beans are just ordinary Java classes that follow certain conventions - you don't need special tools to create them
It is useful to have some tools to help assemble and configure a Bean-based application

Examples of "builder tools":
1.BeanBox (part of Sun's basic Beans Development Kit (BDK) - available free from java.sun.com)
2.Sun Java Workshop
3.IBM VisualAge for Java
4,Symantec Visual Cafe
5.Borland JBuilder
6.Java Bean conventions

There are two primary conventions that must be followed in creating Java Bean classes:
each "property" of the Java Bean class is implemented by defining two public functions (a "get" function and a "set" function)
the "operations" of the Java Bean class are the other public functions defined in the class
/* file Thermometer.java */
public class Thermometer {
  /* these two functions define the "currentTemperature" property */
  public int getCurrentTemperature() { return temp; }
  public void setCurrentTemperature(int aTemp) { temp = aTemp; }

  private int temp;

  public void temperatureChanged(TempChangedEvent ev) { ... }
  ... other implementation details ...
}

Reflection in Java

The "builder" programs can use special functions in Java 1.1 to discover which operations are part of each class
the special Java functions are called the Java Reflection API
they are used in many places in the builders
Java builders can display a "property sheet" for each Java Bean

More Java Bean configuration

The reflection interface may also be used by the builder tool to help the "programmer" tie user interface events to particular operations:
Java code is generated by the Builder tools

Java applications can use the "set" operations to initialize properties
Builder tools can also create the code for new objects that can be "registered" to receive events from the appropriate user interface objects
this means: a Java Bean doesn't have to be modified by the builder tools
all of the tailoring code can be put into newly-created classes
this is almost like "callbacks" in C and C++-based framework software
Creating a Java Bean

Design an ordinary Java class:
decide what properties and public operations it will support
Make sure that each property has a "get" and "set" operation in the public interface of the Java Bean class
Make sure that the class conforms to the Java 1.1 event model
If the class is a "visual Bean", then it should probably be a derived class of one of the AWT classes (probably Component or Container)
A Java Bean may also use some other helper classes
You can bundle the ".class" files for the main Java Bean class and all of the helper classes into a "Java Archive" file (JAR file)
Java Beans make good interfaces

Java Beans can be used to provide an interface to other non-Java applications that use Microsoft's ActiveX component technology
there is a "bridge" between Java Beans and ActiveX
Java Beans can be used to encapsulate the interface to an external database
the "get" and "set" operations in a Java Bean might actually use the Java database interface classes (the JDBC classes) to interact with a relational database
Not all Java Beans are graphical - but they can still be used to assemble applications using an application builder tool
Alternative ways to make a Java Bean

It isn't necessary to make changes to an existing class to convert it to a Java Bean
instead, you can create a "BeanInfo" class that defines the properties and public operations
for example, the BeanInfo class for the Temperature class would be called TemperatureBeanInfo
this class can define functions that permit the builder tools to access the characteristics of the Java Bean
public class TemperatureBeanInfo extends java.beans.SimpleBeanInfo {
 /* the following function will return an array of PropertyDescriptor
    objects, one object per Java Bean property */
  public PropertyDescriptor[] getPropertyDescriptors() {
   ...
  }
}


Using Java Beans for Web programs

Java applets are programs designed to run within a Web browser
An applet is a Java class that is derived from the java.applet.Applet class
Applets usually define the following functions:
init() - this function will define what will be done when the applet is first downloaded (it will usually create some user interface objects and set them up to be displayed, but it might also connect to a database or set up some other resources)
paint() - this function will be used to draw fixed strings and images, which might need to be redrawn any time the window is moved or becomes visible
Under standard conditions, applets can access files on the Web server, but not on the local "client" machine


Conclusion

Java Beans is a technology that makes it possible to build flexible Java applications
Applications can be built by non-programmers (using a builder tool)

Creating new Java Bean classes requires certain skills:
1.the developer needs to understand and use the Java 1.1 event model
2.the developer must follow certain "naming conventions" when creating the operation names in a Java Bean class
3.extra information for the builder tools can be added in a BeanInfo class
4.Java Beans is supported by many of the new Java development tools
5.Java Beans can be used in tandem with many other technologies
ActiveX
relational databases

No comments:

Post a Comment