Associating Objects with Values: The TypeMetadataRegistry

A Interface org.favabeans.base.TypeMetadataRegistryTypeMetadataRegistry assigns an object to a value via the best matching Class org.favabeans.base.TypeType of the object. It is one of the main ways whereby FavaBeans requires no intrusion into the domain object model: we can bind all the view-related information that we might need to domain objects solely by defining the appropriate Class org.favabeans.base.TypeTypes and adding information to Interface org.favabeans.base.TypeMetadataRegistryTypeMetadataRegistrys in the UI.

As a practical and simple example, let's assume that we have the following classes:

Class Person { /* ... */ }
class Employee extends Person { /* ... */ }
class Doctor extends Employee { /* ... */ }

and we choose some icons to be used for representing instances of some of these classes, and add them to some centrally available Interface org.favabeans.base.TypeMetadataRegistryTypeMetadataRegistry:

Icon personIcon = /* ... */;
Icon employeeIcon = /* ... */;

TypeMetadataRegistry tmr = /* ... */;

tmr.put(new JavaType(Person.class), "icon", personIcon);
tmr.put(new JavaType(Employee.class), "icon", employeeIcon);

A UI element could then use this Interface org.favabeans.base.TypeMetadataRegistryTypeMetadataRegistry to display the icons for a number of objects:

TypeMetadataRegistry tmr = /* ... */;

Person[] people = new Person[] {
    new Person("Pat Okoye"),
    new Employee("Joy Albright"),
    new Doctor("Peace Freeman"),
};

for (int i = 0; i < people.length; i++) {
    Icon theIcon = (Icon)tmr.getForObject(people[i], "icon");
    /* display the object using 'theIcon' */
}

and, in this way, use our preferred icons for the Person and Employee objects. Furthermore, the UI element would automatically represent the Doctor object via the best matching icon which, in our example, happens to be that which we associated with class Employee.