Content text Unit - 3.pdf
Advance Java Programming https://ctal-advancejava.blogspot.com/ Page 1 Unit-3 Java Beans 1 What Is a Java Bean? 2 Advantages of Java Beans 3 Introspection 3.a. Design Patterns for Properties 3.b. Design Patterns for Events 3.c. Methods and Design Patterns 3.d. Using the BeanInfo Interface 4 Bound and Constrained Properties 5 Persistence 6 Customizers 7 The Java Beans API 7.a. Introspector 7.b. PropertyDescriptor 7.c. EventSetDescriptor 7.d. MethodDescriptor 8 A Bean Example 1 What Is a Java Bean? A Java Bean is a software component that has been designed to be reusable in a variety of different environments. There is no restriction on the capability of a Bean. It may perform a simple function, such as checking the spelling of a document, or a complex function, such as forecasting the performance of a stock portfolio. Beans are important, because they allow us to build complex systems from software components. These components may be provided by you or supplied by one or more different vendors. Java Beans defines an architecture that specifies how these building blocks can operate together. To better understand the value of Beans, consider the following. Hardware designers have a wide variety of components that can be integrated together to construct a system. Large applications grow in complexity and become very difficult to maintain and enhance.
Advance Java Programming https://ctal-advancejava.blogspot.com/ Page 2 JavaBeans is a portable, platform-independent model written in Java Programming Language. Its components are referred to as beans. In simple terms, JavaBeans are classes which encapsulate several objects into a single object. It helps in accessing these object from multiple places. JavaBeans contains several elements like Constructors, Getter/Setter Methods and much more. JavaBeans has several conventions that should be followed: • Beans should have a default constructor (no arguments) • Beans should provide getter and setter methods o A getter method is used to read the value of a readable property o To update the value, a setter method should be called • Beans should implement java.io.serializable, as it allows to save, store and restore the state of a JavaBean you are working on. Components of JavaBeans The classes that contained definition of beans is known as components of JavaBeans. These classes follows certain design conventions. It includes properties, events, methods and persistence. There are two types of components, GUI based and non GUI based. For instance JButton is example of a component not a class. Properties (date members): Property is a named attribute of a bean, it includes color, label, font, font size, display size. It determines appearance, behavior and state of a bean. Methods: Methods in JavaBeans are same as normal Java methods in a class. It doesn’t follow any specific naming conventions. All properties should have accessor and getter methods. Events: Events in JavaBeans are same as SWING/AWT event handling. Persistence: Serializable interface enables JavaBean to store its state. JavaBean has no argument constructor.
Advance Java Programming https://ctal-advancejava.blogspot.com/ Page 3 JavaBean component 2. Advantages of JavaBean The following are the advantages of JavaBean: o Bean obtains all the benefits of Java’s “write-once, run-anywhere” paradigm. o The JavaBean properties, event and methods can be exposed to another application. o It provides an easiness to reuse the software components. o Reusability in different environments. o JavaBeans are dynamic, can be customized. o Can be deployed in network systems o A Bean may register to receive events from other objects and can generate events that are sent to other objects. o The configuration settings of a Bean can be saved in persistent storage and restored at a later time. Disadvantages of JavaBean The following are the disadvantages of JavaBean: o JavaBeans are mutable. So, it can't take advantages of immutable objects. o Creating the setter and getter method for each property separately may lead to the boilerplate code. 3. Introspection Introspection can be defined as the technique of obtaining information about bean properties, events and methods. Basically introspection means analysis of bean capabilities. Introspection is the automatic process by which a builder tool finds out which properties, methods, and events a bean supports. Introspection describes how methods, properties, and events are discovered in the beans that we write. This process controls the publishing and discovery of bean operations and properties. Without introspection, the JavaBeans technology could not operate. The first method, simple naming conventions are used. These allow the introspection mechanisms to infer information about a Bean.
Advance Java Programming https://ctal-advancejava.blogspot.com/ Page 4 In the second way, an additional class that extends the BeanInfo interface is provided that explicitly supplies this information. 3.a. Design Patterns for Properties A JavaBean property is a named feature that can be accessed by the user of the object. The feature can be of any Java data type, containing the classes that you define. A JavaBean property may be read, write, read-only, or write-only. JavaBean features are accessed through two methods in the JavaBean's implementation class: 1. getPropertyName () For example, if the property name is firstName, the method name would be getFirstName() to read that property. This method is called the accessor. 2. setPropertyName () For example, if the property name is firstName, the method name would be setFirstName() to write that property. This method is called the mutator. There are two types of properties: simple and indexed: Simple Properties A simple property has a single value. It can be identified by the following design patterns: where, N is the name of the property T is its type public T getN( ) public void setN(T arg) Example: private double depth, height, width;