If you have used Delphi, or Visual Basic, you are already familiar with the notion of a bean. The idea is the same; the programming language is different. A Java Bean is a reusable software component that works with Java. More specifically: a Java Bean is a reusable software component that can be visually manipulated in builder tools.
Definition: A Java Bean is a reusable software component that can be visually manipulated in builder tools. To understand the precise meaning of this definition of a Bean, clarification is required for the following terms:
- Software component
- Builder tool
- Visual manipulation
Each of these will be addressed in turn.
Reusable Software Components
Reusable software components are designed to apply the power and benefit of reusable, interchangeable parts from other industries to the field of software construction. Other industries have long profited from reusable components. Reusable electronic components are found on circuit boards. A typical part in your car can be replaced by a component made from one of many different competing manufactuers. Lucrative industries are built around parts construction and supply in most competitive fields. The idea is that standard interfaces allow for interchangeable, reusable components.
Reusable software components can be simple like familiar push buttons, text fields list boxes, scrollbars, dialogs, for example
Button Beans
Slider Beans
Spinbutton Beans
These are the kinds of component sets, toolkits, or widget libraries traditionally sold by third parties. Lately we've seen vendors selling more complex software components like calendars,
and spreadsheets:
If you have used visual component assembly tools such as Visual Age, Delphi, Visual Basic, PARTS, Mojo, or even PowerBuilder, you are familiar with the notion of software components. Visual Basic Extensions (VBX's) were the first widely used software components, followed shortly by language-independent OLE Custom Controls (OCXs). UNIX programmers will be more familiar with widgets and toolkits.
Reusable components add standardized interfaces and object introspection mechanisms to widgets allowing builder tools to query components about their properties and behavior. Software components need not be visible in a running application; they only need to be visible when the application is constructed.
For example, the builder tool above shows a calculator component that is built from 16 button components, a text field component, and a panel upon which the buttons and a text display are placed. In addition, you can see five invisible components. These specific components hold values or strings used by the calculator for intermediate calculations, operation codes, and display strings, as well a boolean flag to determine when evaluation of a new expression is started.
A programmer can move, query, or visually hook together components such as numeric value holders, disk accessors, or network socket components with a mouse while operating a builder tool. However, such components would do their work invisibly while the application is running.
With Beans, you can purchase custom components for Java from third parties. You can also purchase builder tools or application contruction programs supporting Beans Application construction tools let you you to build Java applications by visually selecting components from pallets, panels, or menus, drop them into a form, or client window, and hook up events to event handlers using the mouse as your primary input mechanism.
Components can be nested and arbitrarily complex. For example, a calculator built from components becomes, itself, a component. Custom-built components are easily added to builder tool palettes. More complex components include barcharts,
graphical diagram editors,
and word processors:
Beans, Widgets, Controls, and Components
Depending on your background, you may use different words to describe GUI components for a given software platform. If you come from a Windows background, you probably think in terms of visual controls, possibly Visual Basic Extensions (VBXs) or OLE Controls (OCXs) and now Active X Controls. If you're more accustomed to environments like Motif or X Windows, you probably think in terms of toolkits or widgets.
Beans are to widgets as VBXs or OCXs are to native Windows controls. The main difference between Beans and native platform controls is that they define a design-time interface, which allows application designer tools, or builder tools to query components and ask them what kinds of properties they define, as well as what kinds of events they can generate or respond to.
In visual application builder environments Beans are sometimes referred to as reusable software components, or custom controls.
The main thing that distinguishes software components from standard Windows controls, or Motif widgets is their packaging. Software components or custom controls are packaged with design-time information (properties and methods) that allow builder tools to determine their capabilities.
A second difference is that end users, or third party suppliers can build custom beans independently of the maker of the GUI platform or operating system. These third-party components can then be distributed by IS departments to internal enterprise clients, or they can be sold as add-on components to anyone who uses builder tools to construct applications.
Beans or Class Libraries
It's logical to wonder: "What is the difference between a Java Bean and an instance of a normal Java class?"
What differentiates Beans from typical Java classes isintrospection. Tools that recognize predefined patterns in method signatures and class definitions can "look inside" a Bean to determine its properties and behavior. A Bean's state can be manipulated at the time it is being assembled as a part within a larger application. The application assembly is referred to as design time in contrast to run time. In order for this scheme to work, method signatures within Beans must follow a certain pattern in order for introspection tools to recognize how Beans can be manipulated, both at design time, and run time.
In effect, Beans publish their attributes and behaviors through special method signature patterns that are recognized by beans-aware application construction tools. However, you need not have one of these construction tools in order to build or test your beans. The pattern signatures are designed to be easily recognized by human readers as well as builder tools. One of the first things you'll learn when building beans is how to recognize and construct methods that adhere to these patterns.
Not all useful software modules should be Beans. Beans are best suited to software components intended to be visually manipulated within builder tools. Some functionality, however, is still best provided through a programatic (textual) interface, rather than a visual manipulation interface. For example, an SQL, or JDBC API would probably be better suited to packaging through a class library, rather than a Bean.
May Your Palette be Rich and Full
Still, Beans are tremendously useful, especially for programmers who consider themselves domain experts (financial analysts, scientists, linguists, bank loan officiers, investment analysists, factory process experts), rather than systems programmers.
Ideally, enterprise programmers and domain experts will have a rich pallet of Beans from which to assemble Beans applications.
Definition: Application Builder Tools
The primary purpose of beans is to enable the visual construction of applications. You've probably used or seen applications like Visual Basic, Visual Age, or Delphi. These tools are referred to as visualapplication builders, or builder tools for short.
Typically such tools are GUI applications, although they need not be. There is usually a palette of components available from which a program designer can drag items and place them on a form or client window.
In Windows environments the form is often called the client window area.
The form, or client window, represents the target application under construction and is presented, during design, as it will appear when it runs independently of the builder program.
The most tell-tale window shared by many popular application builders is a property sheet, sometimes called a property editoror simply a properties window.
A property sheet is used to modify properties and eventsassociated with components. In keeping with Java AWT terminology some property editors use the term action in place of event.
Applications built with powerful components can appear complex even when they take little effort to build.
Linking Components in Builder Tools
The calculator program shown earlier is a good example of building a complex program with no handwritten code.
The calculator was built without writing any Java code. Instead, components were linked by using a mouse. Generated events and event-handler methods were selected through pop-up menus.
Every time a button is pressed, an event is fired and side effects result. Example side effects include appending digits to the invisible EnteredString storage buffer, performing a math operation, or displaying text in the display pane. To program an event sequence, select a button as the event source, and indicate that you want to fire an event through a keystroke or menu command. You'll see a menu, or a property sheet, listing events that can be fired. Select an event, then drag a link to the target object which should receive the event. You will be presented with a menu listing event handlers available for the target object.
The following sequence of screens shows a more complex example in a different builder tool. An animation component is added to a subpanel within a client window. First an Animation Bean is selected from the builder's palette and dropped on a panel object in the client form.
A Bean is customized by selecting action items from a property sheet, which in turn invokes a custom Bean editor to select a specific event from a list of events that can be generated by an Animation Bean. A list of events is acquired by introspection; the builder tool uses Java introspection APIs to query beans dropped on the form.
Default behavior for event handlers is usually acceptable, but in some cases you'll want to manually override handler definitions with a text editor.
In this builder tool, much of the programming, including selection of events and event handlers, is done using a class hierarchy browser. After selecting the event-handler method, a template for the event handler is generated, while you flesh out details manually by writing code in the browser.
However not all programs require text editing. As mentioned, the calcualtor example was built without resorting to text coding. The maze of links required to hookup components can be overwhelming, as you can see in the following figure.
But, most builder tools allow you to easily edit links and filter the display of links so that you see only the level of detail that you want.
Nesting Components
Once you have assembled components into an application, often that application itself can be turned into a component. An earlier example showed an Animation component dropped onto a panel to display an animation. Most of the code for the example was created through menu selections. The component was customized primarily by specifying the names of animation files to be displayed from the disk. The mouseDrag event handler was overriden through the browser for detailed customization.
Once custom properties and behavior are defined, they can be preserved by making a new Bean out of the customized instance. This is usually accomplished by serializing the customized Bean using Java's built-in support for object serialization. Most builder tools provide a way to turn compound components into custom Beans. In this example you select the Animation Bean, and issue a menu command to turn it into a Bean component.
The builder generates code to turn the assembly (property and behavior definitions) into a Bean. The bean is added to the component palette of the builder tool. Alternately the bean can be packaged in a JAR archive file for sale, or distribution to clients.
You could take the new custom Bean and use it to compose a compound assembly, creating yet another custom Bean. For example, you could group three instance of a MyAnimationBean on a panel and save the whole assembly as a single new Bean. The compound MyThreeBeans custom component is added to your builder's component palette as a single Bean.
But I hate GUI builders
Over and over again, you hear advanced programmers say that builder tools are more trouble than they are worth. "If you really know what you are doing," they say, "You're best off building code for GUI applications by hand. I've tried GUI builder tools, and they're more trouble than they're worth."
To be clear, a GUI builder, or window builder, is a different beast than an application builder. Many programmers are familiar with window builders and their limitations. In a window builder you design screens visually, and code is generated that becomes part of an application built with traditional compilation tools.
Application builders let you do all of this, but in addition, they let you visually hook up components, select events to be fired, and handlers for events through mouse drag, or menu selection. Very little code needs to be written by hand to get the initial component interaction working properly--at least in comparison to a GUI builder or a window builder.
Also, GUI bulders are typically one-way tools. They allow you to build your interface visually and generate code from a project file. In most cases, however, if you edit the generated code manually, the changes are not propagated backward to the visual representation of the application in the project file.
Tools like Delphi and Visual Basic are much more sophisticated than traditional GUI builders in that modified code does propagate backward to the visual representation. Powerful language parsers are required to make this work properly. The text is parsed and the new representation of visual objects is merged with the old representation (sort of like a binary diff utility). Because older window builders did not provide such a capability, many programmers became frustrated with the tools once they moved past early design stages. They rightly concluded that only a minor amount of work goes into the physical layout of GUI components. Most of the work for any application goes into component hook up, event generation, and event handling methods.
Unfortunately, many programmers, familiar only with the older window builder tools, believe the current generation of application builders suffer from the same restrictions. However, the current generation of builder tools provides enough flexibility and power to warrant another look.