Java basics-Brief Introduction to JavaBeans

Source: Internet
Author: User

Attributes of JavaBean

The attributes of JavaBean are a concept with the attributes of objects in general Java programs, or with all object-oriented programming languages. The specific embodiment of the attributes in the program is the variables in the class. In the JavaBean design, attributes are subdivided into four categories based on different functions: Simple, Index, Bound, and Constrained attributes.

3.1.1 Simple attributes

A simple attribute represents a variable that is accompanied by a pair of get/set methods (c-language processes or functions are called "methods" in Java programs. The property name corresponds to the get/set method name related to the property. For example, if there are setX and getX methods, it indicates that there is an attribute named "X. If there is a method named isX, it usually implies that "X" is a Boolean attribute (that is, the value of X is true or false ). For example, in the following program:


Public class alden1 extends Canvas {
String ourString = "Hello"; // The property name is ourString and the type is string.
Public alden1 () {// alden1 () is the constructor of alden1, which has the same meaning as the constructor in C ++.
SetBackground (Color. red );
SetForeground (Color. blue );
}
/* "Set" attribute */
Public void setString (String newString ){
OurString = newString;
}
/* "Get" attribute */
Public String getString (){
Return ourString;
}
}

3.1.2 Indexed attributes

An Indexed attribute represents an array value. Use the set/get method corresponding to this attribute to obtain the values in the array. This attribute can also be set at a time or get the value of the entire array. Example:


Public class alden2 extends Canvas {
Int [] dataSet = {1, 2, 3, 4, 5, 6}; // dataSet is an indexed attribute.
Public alden2 (){
SetBackground (Color. red );
SetForeground (Color. blue );
}
/* Set the entire array */
Public void setDataSet (int [] x ){
DataSet = x;
}
/* Set a single element value in the array */
Public void setDataSet (int index, int x ){
DataSet [index] = x;
}
/* Obtain the entire array value */
Public int [] getDataSet (){
Return dataSet;
}
/* Obtain the specified Element value in the array */
Public int getDataSet (int x ){
Return dataSet [x];
}
}

3.1.3 Bound attributes

A Bound attribute notifies other objects when the value of this attribute changes. Each time the attribute value changes, this property ignishes A PropertyChange event (in a Java program, the event is also an object ). The event encapsulates the attribute name, the original value of the attribute, and the new value after the attribute changes. This type of event is passed to other beans. The Bean that receives the event should be defined by itself.



Figure 3.1 is a simple Bound attribute. When PushButton's background attribute and Dialog's background attribute bind, if PushButton's background attribute changes, the Dialog's background attribute also changes. Example:


Public class alden3 extends Canvas {
String ourString = "Hello"; // ourString is a bound attribute.
Private PropertyChangeSupport changes = new PropertyChangeSupport (this );
/** Note: Java is an object-oriented language. To use a method, you must specify the method of the object to be used, in the following program, you need to perform the ignition event operation. The method used for this operation is in the PropertyChangeSupport class. Therefore, a changes object is declared and instantiated above. The following describes how to use the firePropertyChange method of changes to ignify the property change event of ourString. */

Public void setString (string newString ){
String oldString = ourString;
OurString = newString;
/* The property value of ourString has changed, so the Ignition Property Change event */
Changes. firePropertyChange ("ourString", oldString, newString );
}
Public String getString (){
Return ourString;
}
/** The following code is used by the development tool. We cannot predict which beans alden3 will combine into an application, and what other components will be related to this change if the ourString attribute of alden3 changes, therefore, the Bean alden3 needs to reserve some interfaces for the development tool. The development tool uses these interfaces to connect other JavaBean objects with alden3. */

Public void addPropertyChangeListener (PropertyChangeLisener l ){
Changes. addPropertyChangeListener (l );
}
Public void removePropertyChangeListener (PropertyChangeListener l ){
Changes. removePropertyChangeListener (l );
}

Using the above Code, the development tool calls the addPropertyChangeListener method of changes to register other JavaBean into the listener queue l of the ourString attribute. l is a Vector Array that can store any Java objects. The development tool can also use the removePropertyChangeListener method of changes to deregister the specified object from l, so that changes to the ourString attribute of alden3 are no longer related to this object. Of course, when programmers write code by hand, they can also directly call these two methods to hook other Java objects with alden3.
3.1.4 Constrained attributes

When the value of a JavaBean constrained attribute changes, other Java objects that have established a connection with this attribute can reject the change of the attribute value. The listener of the constrained attribute throws PropertyVetoException to prevent the change of the attribute value. Process 3.2



For example, the constrained attribute in the following program is PriceInCents.


Public class JellyBean extends Canvas {
Private PropertyChangeSupport changes = new PropertyChangeSupport (this );
Private VetoableChangeSupport Vetos = new VetoableChangeSupport (this );
/* Like the aforementioned changes, you can use the method in the Vetos of the VetoableChangeSupport object to prevent the PriceInCents value change under specific conditions. */


......
Public void setPriceInCents (int newPriceInCents) throws PropertyVetoException {
/* Throws PropertyVetoException in the method name is used to throw exceptions when other Java objects reject changes to PriceInCents. * // * First save the original property value */

Int oldPriceInCents = ourPriceInCents;
/** Rejection event of ignition attribute change */
Vetos. fireVetoableChange ("priceInCents", new Integer (OldPriceInCents), new Integer (newPriceInCents ));

/** If other objects reject changes in priceInCents, the program will throw an exception and no longer execute the following two statements. The method ends. If no other object rejects the change of priceInCents, add ourPriceIncents to the new value in the following code and ignify the property change event */

OurPriceInCents = newPriceInCents;
Changes. firePropertyChange ("priceInCents", new Integer (oldPriceInCents), new Integer (newPriceInCents ));
}

/** Same as the aforementioned changes, APIs must also be reserved for the PriceInCents attribute so that other objects can be registered in the PriceInCents rejection to change the listener queue or deregister the object from it.

Public void addVetoableChangeListener (VetoableChangeListener l)
{Vetos. addVetoableChangeListener (l );
}
Public void removeVetoableChangeListener (VetoableChangeListener l ){
Vetos. removeVetoableChangeListener (l );
}
......
}
As shown in the preceding example, a constrained attribute has two types of listeners: Property Change listener and rejection listener. The listener who rejects attribute changes has corresponding control statements in its own object code. When the monitored constrained attribute changes, the listener determines whether to reject the change of this attribute value in the control statement.
In short, whether the constrained attribute value of a Bean can be changed depends on whether other beans or Java objects allow this change. The conditions that allow or not are defined by other beans or Java objects in their own classes.



JavaBean event
Event processing is the core of the JavaBean architecture. Through the event processing mechanism, some components can be used as the event source to issue events that can be received by the descriptive environment or other components. In this way, different components can be combined in the constructor, and components communicate with each other through event transmission to form an application. In terms of concept, an event is a transmission mechanism in which a certain state changes between the "source object" and "listener object. Events have many different purposes, such as mouse events, window boundary change events, and keyboard events that are often handled in Windows. Java and JavaBean define a general and extensible

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.