JavaBeans Are Objects

 <  Day Day Up  >  

Swing is just one of the areas where Java uses packages of classes to assist in the development of software. Because objects are the complete packages we describe, we can extrapolate this concept to that of components. There are many definitions of components ; depending on what language or model you're using, you may run into different terms. I teach students that they might encounter one or more of these terms:

  • Component

  • Control

  • Widget

  • Bean

Typically, the term control is associated with Microsoft products like Visual Basic. The term JavaBean, or simply bean, is associated with Java. In his book Building Web Applications with UML , Jim Conallen defines the JavaBean component model as consisting of the following:

  • Introspection ” Enables an external component or tool to analyze how a JavaBean works. It can expose the details of its interface.

  • Events ” Allow JavaBeans to communicate and connect with one another.

  • Properties ” Allow developers to customize JavaBeans.

  • Persistence ” Allows customized JavaBeans to retain their customization.

In essence, a JavaBean is a self-contained, self-aware object. As with applets, beans offer a really good example of what an object should be. Not only are beans self-contained and self-aware, they're objects that are responsible for themselves . Figure 13.6 illustrates the JavaBean component model.

Figure 13.6. The JavaBean model.

graphics/13fig06.gif

A good example of a JavaBean is some sort of user interface component, such as a button. A class is created for the button GUI ”a class most probably called Button . This Button class has many of the typical object characteristics. First, it probably inherits from some other GUI object ”perhaps an object called Component . Regardless of how the Button class is built, it is a totally reusable component. As with all properly built classes, the Button class is responsible for itself. Thus, the Button class knows how to build itself, respond to appropriate events, and so on.

JavaBeans and GUIs

Quite often, components are thought of as user interface components. Although it is true that components fit naturally into the GUI model, a component can be a non-GUI design as well. You can create a command- line-based component with absolutely no GUI involved. Basically, any type of Java object can be implemented as a JavaBean.


One of the neat things about components such as JavaBeans is that they can be combined to create even more useful and complex components. For example, suppose you want to create a special dialogue box that allows a user to verify that she wants to save a file. You can create a component called Verify to perform such a task. Figure 13.7 displays the familiar Microsoft Word implementation of this design. This Verify component provides a container that holds three Button objects.

Figure 13.7. Higher-level component.

graphics/13fig07.jpg

Components like this provide a standard look and feel, as well as reusable code. Java can even use components that were written in other languages via special interfaces. Microsoft's Component Object Model (COM) is one example of such a language.

One thing that is important to realize is that JavaBeans are not the same thing as Enterprise JavaBeans. Although they are both components, JavaBeans target the desktop. These components are used for the types of things that we discussed previously, such as client graphical user interfaces. Enterprise JavaBeans, however, are components that target the server. As the name implies, Enterprise JavaBeans are used over a network. Figure 13.8 shows the Enterprise JavaBean model, which we will explore in greater detail in the next chapter.

Figure 13.8. The EJB model.

graphics/13fig08.gif

Perhaps the most important aspect of a JavaBean is that applications can inspect the bean to determine what it can do and how to use it. This is often called introspection . For example, suppose you are using a graphical user interface. With the toolkit visible, you can inspect all the components available before you even decide to use one. You can also inspect these components to determine what properties and methods are available. Introspection is very important for developing components and objects in general. In this way, you can develop reusable components that can be stored in a collection, such as a package or library. Or, you can place a component on a server and let applications over the network use the component. We will cover this topic further in the next chapter.

 <  Day Day Up  >  


Object-Oriented Thought Process
Object-Oriented Thought Process, The (3rd Edition)
ISBN: 0672330164
EAN: 2147483647
Year: 2003
Pages: 164
Authors: Matt Weisfeld

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net