Atom Feed SITE FEED   ADD TO GOOGLE READER

Modelling Properties in Java

We're having a properties discussion over on the beansbinding (JSR-295) mailing list. Here's a recent revision of the API:
/**
* Models the relationship between two related types, such as
* the address of a customer or the size of a List.
*
* <p>A property is not bound to a particular base object - rather it
* is shared by all instances of a class.
*
* @param <B> the base object type, such as Customer
* @param <V> the value type, such as an Address or String
*/
public interface Property<B,V> {
/**
* Returns the value of this property on the specified object.
* @throws PropertyNotReadableException
*/
V get(B baseObject);

/**
* Returns the value of this property on the specified object,
* or the specified default if this property cannot be resolved.
*/
V get(B baseObject, V defaultValue);

/**
* Sets the value of this property on baseObject to newValue.
* @return the previous value of the property
* @throws PropertyNotWritableException
*/
V set(B baseObject, V newValue);

Class<B> getBaseObjectType();
Class<V> getValueType();

/**
* Returns a brief description of this property such as
* "address" or "size".
*/
String toString();
}

Naturally for binding, properties can be observable:
/**
* Mixin for properties that can notify observers when
* they change.
*/
public interface ObservableProperty<B,V> extends Property<B,V> {
/**
* Register the specified listener to receive notification when
* this property is changed.
*/
void addPropertyChangeListener(B baseObject, PropertyChangeListener listener);

/**
* Deregister the specified listener from receiving notification
* when this property is changed.
*/
void removePropertyChangeListener(B baseObject, PropertyChangeListener listener);
}

There's still many unresolved issues:
  • Should properties be type-specific or instance-specific? If they're type-specific, properties need an instance to be passed to the get() method (as above). Instance-specific properties are bound to a particular baseObject which does not need to be passed in.
  • Should Property be an interface or an abstract class?
  • What's a better name for baseObject, the thing that has the property?

    If you have any ideas, please join the discussion!
  • better name for baseObject: bean? Just a suggestion. I know it is kind of warping the definition of bean...
    Well, in Cocoa it's called Key/Value Binding.
    Re: a better name than baseObject for the owner of a property?

    How about "proprietor"?
    It's a well-known English word with the advantage of an etymological affinity to "proprietary," the English word directly derived from the Latin term for a property owner. See
    http://www.etymonline.com/index.php?search=proprietor&searchmode=term