Saturday 14 June 2014

Introduction to JavaBeans

Every Java user interface class is a JavaBeans component. Understanding JavaBeans will help you to learn GUI components.JavaBeans is a software component architecture that extends the power of the Java language by enabling well-formed objects to be manipulated visually at design time in a  pure Java builder tool, such as NetBeans and Eclipse.

JavaBean Rules :
  • A JavaBean must have a public, no-argument constructor (a default constructor). This is required so that Java frameworks can facilitate automated instantiation.
  • The JavaBean class attributes must be accessed via accessor and mutator methods that follow a standard naming convention (getXxxx and setXxxx, isXxxx for boolean attributes). It’s important to note that an “attribute” is a named memory location that contains a value, while a “property” refers to this set of methods used to access an attribute. This allows frameworks to automate operations on attribute values.
  • The JavaBean class should be serializable. This allows Java applications and frameworks to save, store, and restore the JavaBean state.That means a bean must implement the Serializable interface to ensure a persistent state.
package player; 
// The class is serialized for IO operations
public class PersonBean implements java.io.Serializable {
 // attributes declared as private
 private String name;
 private boolean counter;
 

 // Default Constructor
 public Person() { }

 // getXxx to access the name attribute
 public String getName() {
  return this.name;
 }

 // setXxxx to mutate the name attribute
 public void setName(String name) {
  this.name = name;
 }

 // isXxxx to access boolean attribute
 public boolean iscounter() {
  return this.counter;
 }

 // setXxx to mutate boolean attribute
 public void setCounter(boolean counter) {
  this.counter = counter;
 }
}
 
import player.PersonBean;
 
/**
 * Class TestPersonBean
 */
public class TestPersonBean {
    /**
     * Tester method  for class PersonBean
     */
    public static void main(String[] args) {
        PersonBean person = new PersonBean();
 
        person.setName("Bob");
        person.setcounter(false);
 
        // Output: "Bob [alive]"
        System.out.print(person.getName());
        System.out.println(person.isCounter() ? " [counter]" : " [alive]");
    }
} 
Advantages:
  • The properties, events, and methods of a bean that are exposed to another application can be controlled.
  • A bean may register to receive events from other objects and can generate events that are sent to those other objects.
  • Auxiliary software can be provided to help configure a java bean.
  • The configuration setting of bean can be saved in a persistent storage and restored at a later time.
 Disadvantages:
  • A class with a nullary constructor is subject to being instantiated in an invalid state. If such a class is instantiated manually by a developer (rather than automatically by some kind of framework), the developer might not realize that the class has been improperly instantiated. The compiler can’t detect such a problem, and even if it’s documented, there’s no guarantee that the developer will see the documentation.
  • Having to create a getter for every property and a setter for many, most, or all of them can lead to an immense quantity of boilerplate code.

No comments:

Post a Comment