Bean Internals  «Prev  Next»
Lesson 2Structure of a Bean
ObjectiveExplore the structural makeup of a Bean.

Structure of JavaBean

The complexity of some component object models leads developers to often look at software components as mystical entities. JavaBeans, however, holds very little complexity and therefore practically zero mystery. The best way to understand this is to consider the basic structure of a Bean:
  • Component Object Models
    Just as the assembly line approach to manufacturing during the industrial revolution sparked a whole new level of efficiency, software components are now attempting to dramatically improve the efficiency of software development. A software component is a piece of software isolated into a discrete, easily reusable structure. Software components are ideally supposed to act as software building blocks that can be easily used and reused together to create applications without reinventing the proverbial wheel over and over. JavaBeans is really just one offering in a long line of software component models, but for a number of reasons it is positioned to be the most promising attempt at providing a practical model.


JavaBeans related to Component-based Software Engineering

JavaBeans are a fundamental component in the context of component-based software engineering (CBSE) in Java. Here's how they relate:
  1. Component Model: JavaBeans provide a component model for Java, which is a key aspect of CBSE. A component in software engineering is a reusable piece of code that can be easily integrated into different systems or applications. JavaBeans are designed to be reusable software components that can be manipulated visually in builder tools.
  2. Properties: JavaBeans have properties that can be read or written through getter and setter methods. This adheres to the principle of encapsulation in CBSE, where components hide their internal workings and expose only what's necessary for interaction.
  3. Events: JavaBeans support event models, allowing them to fire events that other components can listen to and react upon. This facilitates loose coupling between components, a crucial aspect of CBSE where components should interact in a standardized way without needing to know the internal details of each other.
  4. Introspection: One of the key features of JavaBeans is that they can be introspected, meaning their properties, methods, and events can be discovered at runtime. This supports the plug-and-play nature of components in CBSE, where components can be dynamically loaded and configured.
  5. Reusability: JavaBeans are designed for reuse. They can be used in various environments, including desktop applications, web applications, and even in frameworks like JSF (JavaServer Faces) for web component development. This reusability aligns with CBSE's goal of promoting component reuse across different projects.
  6. Standardization: JavaBeans adhere to certain conventions (like naming methods for properties and events), which standardizes their interface. This standardization makes it easier for tools and developers to work with these components uniformly, which is a core principle of CBSE to ensure interoperability.
  7. Integration with Tools: JavaBeans were originally intended to be used within visual development environments, where developers could drag and drop components onto a form, configure their properties, and connect them through events. This visual programming paradigm is a direct application of CBSE principles in practical software development.
  8. Extensibility: While not exclusive to CBSE, JavaBeans can be extended or subclassed, allowing developers to create custom components that inherit and add to the functionality of existing beans, promoting modularity and extendibility, which are also key in CBSE.

In summary, JavaBeans embody many principles of component-based software engineering by providing a standardized, reusable, and introspective component model in Java. They facilitate the development of software systems where components can be assembled to create applications, much like physical components in other engineering disciplines, thereby supporting modularity, reusability, and ease of maintenance in software development.


Structure of JavaBean

This lesson marks your first exposure into really learning the internals of the JavaBeans technology. The details of JavaBeans are presented using an inside-out approach, meaning that you learn about the inner workings of a Bean and then progress to learning about how a Bean functions externally later in the course. This approach is beneficial in that it immediately reveals the structural simplicity of JavaBeans. This is not to say that there are not complex issues associated with any type of component software, including JavaBeans. JavaBeans is a very streamlined technology and is easy to comprehend at its core. The description of the basic structure of a Bean in this lesson will reinforce the simplicity of JavaBeans.
JavaBean Example The following Java class is used to represent customers in our system. We will name this class Customer. Customer is a simple Java class that defines eight properties:
id, firstName, lastName, street, city, state, zip, and country. 

Properties are attributes that can be accessed via the class�s fields or through public set and get methods. A Java class that follows this pattern is also called a JavaBean:
The following Java class follows the JavaBeans conventions.
A JavaBean is a Java class that:
  1. Has a public no-argument constructor (this class would implicitly have one if no constructor is defined).
  2. Provides getter and setter methods to access private fields.
  3. Implements `Serializable` (though this is often optional unless the object is to be serialized).

Your `Customer` class meets the key JavaBean requirements:
  • It has private fields (like `id`, `firstName`, `lastName`, etc.).
  • It provides public getter and setter methods for these fields.

While the class doesn't explicitly implement `Serializable`, that would be necessary if you need to serialize instances of `Customer`. Otherwise, the class is a valid JavaBean.
public class Customer {
 private int id;
 private String firstName;
 private String lastName;
 private String street;
 private String city;
 private String state;
 private String zip;
 private String country;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getStreet() {
  return street;
 }

 public void setStreet(String street) {
  this.street = street;
 }

 public String getCity() {
  return city;
 }

 public void setCity(String city) {
  this.city = city;
 }

 public String getState() {
  return state;
 }

 public void setState(String state) {
  this.state = state;
 }

 public String getZip() {
  return zip;
 }

 public void setZip(String zip) {
  this.zip = zip;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }
}


Bean Data Method

Javabean consisting of Data and Methods
Javabean consisting of Data and Methods

A Bean, just like any other object in an object-oriented environment, is comprised of two primary parts:
  1. data and
  2. methods that act on data.
The data part of a Bean completely describes the state of the Bean, while the methods provide a means for the Bean's state to be modified and for actions to be taken accordingly.

What are Components?

Components are self-contained elements of software that can be controlled dynamically and assembled to form applications and these components must also interoperate according to a set of rules and guidelines. They must behave in ways that are expected. The components bring functionality, while the environment brings structure and order. JavaBeans is Java's component model and allows users to construct applications by piecing components together either programmatically or visually (or both). Support of visual programming is paramount to the component model and is what makes component-based software development truly powerful. The model is made up of an architecture and an API and these elements provide a structure whereby components can be combined to create an application. This environment provides services and rules, the framework that allows components to participate properly. This means that components are provided with the tools necessary to work in the environment, and they exhibit certain behaviors that identify them as such. One very important aspect of this structure is containment and container provides a context in which components can interact. A common example would be a panel that provides layout management or mediation of interactions for visual components. Of course, containers themselves can be components. In the next lesson, you learn about Bean methods.

SEMrush Software