| An implementation of the strategy pattern. The strategy pattern allows new functionality
to be assigned to an existing class. As implemented here, this is a smart lookup between a
particular class (called the subject class) and some object instance that can provide
the extra functionality (called the strategy). The implementation of the strategy is
not relevant to the StrategyRegistry class.
Strategies are registered before they can be used; the registration maps a particular class to a
strategy instance. The strategy instance will be used when the subject class matches the
registered class, or the subject class inherits from the registered class.
This means that a search must be made that walks the inheritance tree (upwards from the subject
class) to find a registered mapping.
In addition, strategies can be registered against interfaces. Searching of interfaces
occurs after searching of classes. The exact order is:
- Search for the subject class, then each super-class of the subject class (excluding
java.lang.Object)
- Search interfaces, starting with interfaces implemented by the subject class, continuing
with interfaces implemented by the super-classes, then interfaces extended by earlier interfaces
(the exact order is a bit fuzzy)
- Search for a match for java.lang.Object, if any
The first match terminates the search.
The StrategyRegistry caches the results of search; a subsequent search for the same subject class
will be resolved immediately.
StrategyRegistry does a minor tweak of the "natural" inheritance. Normally, the parent class of
an object array (i.e., Foo[] ) is simply Object , even though you
may assign Foo[] to a variable of type Object[] . StrategyRegistry
"fixes" this by searching for Object[] as if it was the superclass of any object
array. This means that the search path for Foo[] is Foo[] ,
Object[] , then a couple of interfaces
java.lang.Cloneable ,
java.io.Serializable , etc. that are implicitily implemented by arrays), and then,
finally, Object
This tweak doesn't apply to arrays of primitives, since such arrays may not be
assigned to Object[] .
author: Howard M. Lewis Ship See Also: org.apache.hivemind.lib.util.StrategyRegistryImpl since: 1.1 |