img Inber

(Page 1 of 3 )A walking tour of JavaBeans What JavaBeans is, how it works, and why you want to use it

发表于2004/7/9 14:20:00  1655人阅读

分类: java收藏

A walking tour of JavaBeans

What JavaBeans is, how it works, and why you want to use it

The success of rapid application development (RAD) systems like Borland's Delphi and Microsoft's Visual Basic have brought a great deal of attention to the concept of software components. Java has now entered the ring with its own component software specification, JavaBeans. This article -- the first of many JavaBeans columns -- gives a high-level description of JavaBeans, explains why it's useful to developers, and introduces basic JavaBeans concepts. (2,800 words)

Page 1 of 3


The Java Development Kit (JDK) 1.1 hit the streets in February, bringing with it a new API called JavaBeans. JavaBeans is the software component architecture for the Java language. Why should Java developers be interested in this component technology? In this article, we'll start with a brief explanation of component technology and why it's useful in system architecture. Then we'll look at JavaBeans to see what features the Beans API provides. We'll finish with a preview of a regular series of articles that will explain how the new core APIs work with and within Beans, and how to use the JavaBeans API to create your own software components.

What is software component technology?
Software components are to software what integrated circuits (ICs) are to electronics: "black boxes" that encapsulate functionality and provide services based on a specification. Software reusability has long been a holy grail of software engineering and a major goal of object-oriented programming. Software components are designed to be reusable, even interchangeable.

In the "old days," people experimenting with radio had to spend a lot of time building chassis, winding inductors, and wiring together smaller components to create amplifiers, filters, and oscillators. Today, these components come packaged as integrated circuits with inputs, outputs, and specifications that tell how each component works. The specification is a sort of contract between the creator of the component and its user, explaining how the component will behave in various circumstances. Combine these components correctly, and, voila!, you have a radio. (Or, voila!, a heart rate monitor or microwave oven. You get the picture.) If the component doesn't perform to its specification, it is considered defective and can be replaced.

Software components give programmers the same benefits that the above components give to people building radios. They provide specific functionality that can be reused in different places. Any software component conforming to the same specification can be replaced with another that performs the same function -- without breaking the system. Programmers can then connect software components together to create different applications, just as an electrical engineer wires together components to create a new radio, heart rate monitor, or microwave oven.

What is a software component?
Software components hide implementation, conform to interfaces, and encapsulate data, just like classes do in object-oriented languages. So how do components differ from classes? The answer: Almost all software components are also classes. What makes them components is their conformance to a software component specification. The JavaBeans Specification is the document that describes what a Java class must do to be considered a "Bean." Programmers (and, as importantly, integrated development environments [IDEs]) can depend on any class that advertises itself as a Bean to conform to the rules set out in the specification. If it doesn't conform, the contract has been broken and the Bean is defective.

To make a class into a component, the programmer must add functionality to the class that has little to do with what makes the class useful. The only requirement to make a class into a Bean is that the class implement the interface java.io.Serializable. The Serializable interface is a new Java 1.1 feature. Serializable classes know how to package themselves into streams of bytes to be transmitted through networks or saved to disk, awaiting later reincarnation.

Interfaces and classes defined in the package java.beans allow a Beans developer to control how the Beans user (a programmer using a particular Bean) may set and get Beans' properties, hook Beans up to communicate with other components, and ask a Beans to describe themselves. (More on all this later.) The component specification outlines how the component must implement these methods.

Given class files that contain Beans, a programmer can create instances of the Beans and connect them together to do useful work. Writing applications with software components instead of writing a traditional program is analogous to wiring together ICs to build a radio instead of using discrete components or rolling your own inductors. What's more, IDEs can analyze Beans, determining their methods and properties, and make wiring Beans together as simple (in some cases, anyway) as drag-and-drop! (If you work through Sun's Beans Tutorial, you'll see just how this works.)

Understanding JavaBeans gives a developer the knowledge necessary to use these new components as they become available. The concepts involved relate to many of the new (post-Java 1.1) features of the Java core. And what's most exciting, it's now possible for a developer to turn existing classes into reusable software components that automatically integrate with Beans-aware IDEs.

Next page >
Page 1 A walking tour of JavaBeans
Page 2 What JavaBeans is, and what it does
Page 3 Event handling


0 0



取 消