Indexed property design patterns
If you recall, an indexed property is a property representing an array of values.
Because indexed properties require slightly different accessor methods, it only makes sense that their design patterns differ a little from those of other properties.
Following are the design patterns for indexed properties:
public PropertyType getPropertyName(int index);
public void setPropertyName(int index, PropertyType x);
public PropertyType[] getPropertyName();
public void setPropertyName(PropertyType[] x);
The first pair of design patterns defines accessor methods for getting and setting individual
elements in an indexed property. The second pair of patterns defines accessor methods for getting and setting the entire property array as a whole.
Applying these design patterns to a property of type int named <scores would yield the following method definitions:
Developing Java Beans
Indexed properties
In addition to simple single-value properties, we also support indexed properties. An indexed property supports a range of values. Whenever the property is read or written you just specify an index to identify which value you want. Property indexes must use the Java datatype int.
The original Sun Documentation said the following regarding the int restriction .
We may relax this restriction in the future to allow other index types. However it seems that "int" indexes are in practice the most common and useful kind of property index.
A component may also expose an indexed property as a single array value.
For example, if there is an indexed property "tom" of type string it may be possible from a scripting environment to access an individual indexed value using "b.tom[3]" and also to access the same property as an array using "b.tom". For indexed properties the accessor type signatures are:
void setter(int index, PropertyType value); // indexed setter
PropertyType getter(int index); // indexed getter
void setter(PropertyType values[]); // array setter
PropertyType[] getter(); // array getter
The indexed getter and setter methods may throw a java.lang.ArrayIndexOutOfBoundsException
runtime exception if an index is used that is outside the current array bounds. In order to change the size of the array you must use the array setter method to set a new
(or updated) array.
public int getScores(int index);
public void setScores(int index, int s);
public int[] getScores();
public void setScores(int[] s);