Search Omandev

Monday, May 19, 2008

Java Beans

As software or application developers, we are constantly being asked to build applications in less time and with less money. And, of course, these applications are expected to be better and faster than ever before.

Object-oriented techniques and component software environments are in wide use now, in the hope that they can help us build applications more quickly. Development tools like Microsoft's Visual Basic have made it easier to build applications faster by taking a building-block approach to software development (developing the software as pieces of blocks and mending them up to deliver complete software for the proposed requirement). Such tools provide a visual programming model that allows you to include software components rapidly in your applications.

The Java Beans architecture brings the component development model to Java.

Component Model

Components are self-contained elements of software that can be controlled dynamically and assembled to form applications. There are set of rules and guidelines where all components must bind to them. The set of rules are set to provide a standards form for all components which encourages usability and a clear understanding. Components are expected to exhibit certain behaviors and characteristics in order to participate in the component structure and to interact with the environment, as well as with other components. Most articles would define components like a society of software citizens; the citizens (components) bring functionality, while the society (environment) brings structure and order.

Java Beans component Model

Java Beans is a component model architect brought to Java. It 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; it's what makes component-based software development truly useful and powerful.

The model is made up of architecture and an API (Application Programming Interface). Together, these elements provide a structure whereby components can be combined to develop an application. This environment provides services and rules, which allows a standard structure. This means that components are provided with the tools necessary to work in the environment, and they exhibit certain behaviors that identify them. One very important aspect of this structure is containment. A 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.

The Java Beans Architecture

JavaBeans is architecture for both using and building components in Java. This architecture supports the features of software reuse, component models, and object orientation. One of the most important features of JavaBeans is that it does not alter the existing Java language (i.e. if you know how to write software in Java, you know how to use and create Beans). The strengths of Java are built upon and extended to create the JavaBeans component architecture.

Although Beans are intended to work in a visual application development tool, they don't necessarily have a visual representation at run-time (although many will). What this does mean is that Beans must allow their property values to be changed through some type of visual interface, and their methods and events should be exposed so that the development tool can write code capable of manipulating the component when the application is executed.

Java Bean set of rules

Creating a Bean doesn't require any advanced concepts. All you need is follow the following coding conventions

  • Implements java.io.Serializable interface
  • Provides no argument constructor
  • Provides getter and setter methods for accessing it's properties

Serializable interface allows objects to be stored/retrieved using ObjectOutputStream/ObjectInputStream
  • class must be declared as public
  • class must implement Serializable interface
  • class must have a default constructor
  • all data members must be primitive, serializable objects or declared transient 

1 comments:

khalid almamari said...

good post. thanks Saud.

Post a Comment