An object implements
Faceted in order to be
tightly integrated into the
Facet system. An object
returns a
Facet of itself
of a requested
Type via its
Faceted.getFacet(Type)
method. Any lifecycle management of
Facets is the
responsibility of the
Faceted object.
More typically -- and more powerfully -- we can create a
Facet for an object that
is not aware of the
Facet
system; we do this with the help of a
FacetRegistry. Some
"startup" component initializes a
FacetRegistry (typically that
available via class
FavaBeans)
with information about the
Types
of objects for which
Facets may be created, and
some
FacetFactory objects
to which the
FacetRegistry
may delegate the responsibility for actually constructing
Facets. A
FacetRegistry is
responsible for maintaining a list of already constructed
Facets for an
object.
A typical interaction with a
FacetRegistry starts by
adding some
FacetFactory
objects. Given:
interface WebAccessible { /* ... */ } interface HtmlAccessible extends WebAccessible { /* ... */ } interface Vehicle { /* ... */ } Vehicle theVehicle; /* assuming already exists */ FacetFactory fac0, fac1; /* assuming these already exist */ FacetRegistry theFacets = /* ... */;
We could add:
theFacets.addFactory(new JavaType(Vehicle.class), new JavaType(WebAccessible.class), fac0); theFacets.addFactory(new JavaType(Vehicle.class), new JavaType(HtmlAccessible.class), fac1);
We could then use the
FacetRegistry to obtain an
HtmlAccessible
Facet of the
Vehicle object:
HtmlAccessible h0 = (HtmlAccessible) theFacets.getFacet(theVehicle, new JavaType(HtmlAccessible.class));
The FacetRegistry would
cache this
Facet so
that:
theFacets.getFacet(theVehicle, new JavaType(HtmlAccessible.class)) == h0;
and would return the same
Facet for compatible
supertypes of that for which it was originally constructed:
theFacets.getFacet(theVehicle, new JavaType(WebAccessible.class)) == h0;
until the
FacetRegistry.clearFacets(Object)
method is called:
theFacets.clearFacets(theVehicle);
after which Facets
for this object are created anew:
HtmlAccessible h1 = (HtmlAccessible) theFacets.getFacet(theVehicle, new JavaType(HtmlAccessible.class)); h1 != h0;