Skip to main content.

Web Based Programming Tutorials

Homepage | Forum - Join the forum to discuss anything related to programming! | Programming Resources

Java 1.2 Unleashed

Java 1.2 Unleashed -- Ch 24 -- The Software Component Assembly Model


Java 1.2 Unleashed

Previous chapterNext chapterContents


- 24 -

The Software Component Assembly Model




In this book you develop almost all of your applications and applets from scratch because you're trying to learn Java. Once you become comfortable in Java programming and go out to develop real-world Java applications and applets, you won't be so eager to start from scratch. You'll want to use available off-the-shelf Java components whenever possible.

Java components are available in a number of forms. For example, you can go down to your local software superstore and purchase a set of Java API packages. You can then use these new APIs to create objects that are instances of the classes and interfaces of the new APIs. You're familiar with this process because you've been learning to do this with the API of JDK 1.2.



If you purchase a visual Java development tool, such as Symantec's Visual Café for Java, you'll be able to drag and drop GUI components onto the interface design of your applications and applets. Some of these GUI components may be new components that are not included with the JDK 1.2. Many of these components are made available as Java beans.

You don't need to purchase a visual design tool to get access to Java beans. Many beans are available for download via the Web. You can use these beans with freely available visual design tools, such as the JavaBeans Development Kit (BDK) from JavaSoft.

In this chapter, you'll be introduced to the JavaBeans software component model. You'll learn how the software component model works and how software components are used to simplify the development of complex software. You'll study the features of component-based software development and learn how JavaBeans supports these features. When you finish this chapter, you'll understand how JavaBeans are used in component-based software development.


NOTE: This chapter introduces the concepts of the software component model and summarizes how JavaBeans supports these concepts. Chapter 26, "Developing Beans," takes a detailed look at the JavaBeans API and shows how to develop simple beans.

Components and Containers

JavaBeans are Java software components that are designed for maximum reuse. They are often visible GUI components, but can also be invisible algorithmic components. They support the software component model pioneered by Microsoft's Visual Basic and Borland's Delphi. This model focuses on the use of components and containers.

Components are specialized, self-contained software entities that can be replicated, customized, and inserted into applications and applets. Containers are simply components that contain other components. A container is used as a framework for visually organizing components. Visual development tools allow components to be dragged and dropped into a container, resized, and positioned.

You are familiar with the concept of components and containers from your study of the AWT. The components and containers of the JavaBeans component model are similar in many ways to the Component and Container classes of the AWT.

In other ways, the components and containers of JavaBeans extend beyond the Component and Container classes of the AWT.

The important point to remember about JavaBeans components and containers is that they support a hierarchical software development approach where simple components can be assembled within containers to produce more complex components. This capability allows software developers to make maximum reuse of existing software components when creating new software or improving existing software. Figure 24.1 summarizes the use of components and containers.

FIGURE 24.1. Using components and containers.

Introspection and Discovery

Component interfaces are well-defined and may be discovered during a component's execution. This feature, referred to as introspection, allows visual programming tools to drag and drop a component onto an application or applet design and dynamically determine what component interface methods and properties are available. Interface methods are public methods of a bean that are available for use by other components. Properties are attributes of a bean that are implemented by the bean class's field variables and accessed via accessor methods.

JavaBeans support introspection at multiple levels. At a low level, introspection can be accomplished using the reflection capabilities of the java.lang.reflect package. These capabilities allow Java objects to discover information about the public methods, fields, and constructors of loaded classes during program execution. Reflection allows introspection to be accomplished for all beans. All you have to do is declare a method or variable as public and it can be discovered using reflection.

An intermediate level introspection capability provided by JavaBeans utilizes design patterns. Design patterns are method naming conventions that are used by the introspection classes of java.beans to infer information about reflected methods based on their names. For example, design patterns can be used by visual design tools to identify a bean's event generation and processing capabilities by looking for methods that follow the naming conventions for event generation and event listening. Design tools can use design patterns to obtain a great deal of information about a bean in the absence of explicitly provided information.

Design patterns are a low overhead approach to supporting introspection in component development. All you have to do is adhere to the naming convention of design patterns and visual design tools will be able to make helpful inferences about how your components are used.

At the highest level, JavaBeans supports introspection through the use of classes and interfaces that provide explicit information about a bean's methods, properties, and events. By explicitly providing this information to visual design tools, you can add help information and extra levels of design documentation that will be automatically recognized and presented in the visual design environment.

Figure 24.2 illustrates the introspection and discovery capabilities of JavaBeans. These capabilities are important in that they allow software components to be developed in such a way that information about the components can be obtained automatically by visual design tools.

FIGURE 24.2. Introspection and visual design.

Interface Methods and Properties

Properties determine the appearance or behavior of a component. A component's properties can be modified during the visual design of an application. Most visual design tools provide property sheets to facilitate the setting of a component's properties. Property sheets identify all of the properties of a component and often provide help information related to specific properties. The properties and help information are discovered by visual design tools using introspection. Figure 24.3 provides an example of a property sheet.

In JavaBeans, all properties are accessed through special interface methods, referred to as accessor methods. There are two types of accessor methods: getter methods and setter methods. Getter methods retrieve the values of properties, and setter methods set property values.

FIGURE 24.3. Using property sheets to customize component properties.

Interface methods are methods that are used to modify the behavior or state of a component or to retrieve information about the state of a component. These methods are often used to support event handling. Most visual design tools provide the capability to connect events generated in one component with the interface methods of other components. For example, suppose a container contains two button components, labeled Start and Stop, and a ticker tape component, as shown in Figure 24.4. Also suppose that the buttons generate the button-clicked event when they are clicked and the ticker tape component provides the startTape() and stopTape() interface methods. Most visual design tools let you graphically connect the button-clicked events of the Start and Stop buttons with the startTape() and stopTape() methods. This allows you to implement your interface without having to write the code to connect your interface components.

FIGURE 24.4. Interfaces methods can be connected to events.

Persistence

The property sheets of visual design tools are used to tailor the properties of components for specific applications. The modified properties are stored in such a manner that they remain with the component from design to execution. The capability to store changes to a component's properties is known as persistence. Persistence allows components to be customized for later use. For example, during design, you can create two button beans--one with a blue background color and a yellow foreground color and another with a red background color and a white foreground color. The color modifications are stored along with instances of each bean object. When the beans are displayed during a program's execution, they are displayed using the modified colors.

JavaBeans supports persistence through object serialization. Object serialization is the capability to write a Java object to a stream in such a way that the definition and current state of the object are preserved. When a serialized object is read from a stream, the object is initialized and in exactly the same state it was in when it was written to the stream. Figure 24.5 summarizes how object serialization supports persistence. Chapter 40, "Using Object Serialization and JavaSpaces," covers object serialization.

FIGURE 24.5. Persistence is implemented through object serialization.

Events

Visual development tools allow components to be dragged and dropped into a container, resized, and positioned. The visual nature of these tools greatly simplifies the development of user interfaces. However, component-based visual development tools go beyond simple screen layout. They also allow component event handling to be described in a visual manner.

You should be familiar with events, having worked with event handling code in most of the examples in this book. In general, events are generated in response to certain actions, such as the user clicking or moving the mouse or pressing a keyboard key. The event is handled by an event handler. Beans can handle events that occur local to them. For example, a button-based bean is required to handle the clicking of a button. Beans can also call upon other beans to complete the handling of an event. For example, a button bean can handle the button-clicked event by causing a text string to be displayed in a status-display bean. Visual development tools support the connection of event sources (for example, the button bean) with event listeners (for example, the status-display bean) using graphical design tools. In many cases, event handling can be performed without having to write event-handling code. You'll see a concrete example of this when you use the BDK in the next chapter. This code is automatically generated by the visual design tools. Figure 24.6 graphically depicts the relationship between event sources and event listeners.

FIGURE 24.6. Event sources fire events that are handled by event listeners.

Visual Design

One of the ultimate benefits of using a component-based approach to software development is that you can use visual design tools to support your software development efforts. These tools greatly simplify the process of complex software development. They also allow you to develop higher-quality software, more quickly, and at a lower cost. Some of the features typically found in component-based visual design tools are as follows:

Figure 24.7 summarizes the various ways in which visual design tools simplify the process of component-based software development. If you've never used a visual design tool, you're in for a big surprise. Even the freely available BeanBox of the JavaBeans development kit provides a number of useful tools for facilitating the development of component-based software.

FIGURE 24.7. Visual design tools greatly simplify the process of component-based software development.

Summary

In this chapter you were introduced to component-based software development. You learned how the software component model works and how software components are used to simplify the development of complex software. You studied the features of component-based software development and learned how JavaBeans supports these features. In the next chapter you'll download, install, and familiarize yourself with the JavaBeans development kit (BDK) provided by JavaSoft.


Previous chapterNext chapterContents

© Copyright, Macmillan Computer Publishing. All rights reserved.