Any class can export properties by implementing the interface HasProperties:
Each property is associated with a name, which must be a valid Java identifier. The getProperty() method returns a Property handle to the named property, which can in turn be used to access that property’s values or obtain other information about it. getAllPropertyInfo() returns a PropertyInfoList providing information about all the properties associated with the class.
A Property handle supplies the following methods:
Returns the property’s value. As a rule, returned returned values should be treated as read-only.
Sets the property’s value (unless it is read-only, see Section 1.2).
Returns a Range object for a property (see Section 1.1.1), which is used to determine which values are admissible to set. If all values are admissible, getRange() can return null.
Returns the object to which this property handle belongs.
Returns static information about this property (see Section 1.2).
A simple example of property usage is given below. Assume that we have a class called DeformableModel which contains a property called stiffness, and we want to set the stiffness to 1000. This could be done as follows:
Of course, DeformableModel will likely have a method called setStiffness that can be used to set the stiffness directly, without having to got through the Property interface. However, the purpose of properties is not to facilitate attribute access within specially constructed code; it is to facilitate attribute access within generic code that is hidden from the user. For instance, suppose I want to query a property value from a GUI. The GUI must obtain the name of the desired property from the user (e.g., through a menu or a text box), and then given only that name, it must go and obtain the necessary information from the object exporting that property. A Property allows this to be done in a manner independent of the nature of the property itself.
In theory, one could embed the methods of Property directly within the HasProperties interface, using methods with signatures like
The main reason for not doing this is performance: a property handle can access the attribute quickly, without having to resolve the property’s name each time.
Each property handle contains a back-pointer to the object containing, or hosting, the property, which can be obtained with the getHost() method.