public class BridgeFactory extends Object
Provides a factory with supporting classes that generates implementations of a logical interface whose methods are
delegated to various helper objects on both sides of an abstraction separation. See org.leialearns.bridge
.
This class is designed for use in Spring to define several beans, one for each logical interface
(near type). A bridge factory bean is defined by its far type, i.e., the type of
its concrete delegation index. The logical interface is determined by reflection (by looking up the return
type of the declareNearType
method.
Additional helpers can be specified as desired. Bindings for the methods specified in the logical interface are looked up when the factory is instantiated in the order in which the helpers are given. If a method is not found in one of the helpers it is looked up in the far type. If it is desired to search the far type before some of the helpers, include a null pointer in the array of helpers.
There are five types of helpers:
null
For helpers of type 2 a new instance is created using the default no-arg constructor for each bridge object
that is created by the factory. These instances are not configured by the spring bean factory. The helper
null
represents the far object. For the other three types of helpers, either the near object or the
far object is prepended to the parameter list, as appropriate. The use of helpers of type 4 is strongly
discouraged.
In the context of an ORM framework it is common to use not only the DAO of that corresponds the far type as a helper, but also DAOs of other entities. For example, the SymbolDAO can provide a method that finds all symbols that belong to a particular alphabet. This method can be exposed very naturally on the Alphabet bridged object.
<bean id="alphabetFactory" class="org.leialearns.bridge.BridgeFactory"> <constructor-arg value="org.leialearns.jpa.interaction.AlphabetDTO"/> <constructor-arg> <array> <ref bean="alphabetDAO"/> <ref bean="symbolDAO"/> <null/> <!-- AlphabetDTO --> <value type="java.lang.Class">org.leialearns.logic.interaction.AlphabetAugmenter</value> </array> </constructor-arg> </bean>
org.leialearns.bridge
Constructor and Description |
---|
BridgeFactory(Class<? extends FarObject<?>> farType,
Object... helpers)
Creates a new BridgeFactory instance.
|
BridgeFactory(String farTypeName,
Object... helpers)
Creates a new BridgeFactory instance.
|
Modifier and Type | Method and Description |
---|---|
Class<?> |
getFarType()
Returns the far type of the bridge.
|
<T> T |
getHelper(Class<T> type)
Returns a helper of the given type.
|
Object[] |
getHelpers()
Returns the array of helpers used by this factory instance
|
Object |
getNearObject(Object farObject)
Finds or creates a near object for the given far object.
|
Class<?> |
getNearType()
Returns the near type of the bridge.
|
void |
setRegistry(BridgeHeadTypeRegistry registry)
Sets the
BridgeHeadTypeRegistry , registers this factory
instance with it, and looks up bindings for the methods in the near type. |
public BridgeFactory(String farTypeName, Object... helpers)
farTypeName
- The name of the far type classhelpers
- The helpers that implement additional methods on the near typepublic Class<?> getNearType()
public Class<?> getFarType()
@Autowired public void setRegistry(BridgeHeadTypeRegistry registry)
BridgeHeadTypeRegistry
, registers this factory
instance with it, and looks up bindings for the methods in the near type.registry
- The BridgeHeadTypeRegistry instance to usepublic Object getNearObject(Object farObject)
farObject
- The far object that is to be wrapped by the near objectpublic Object[] getHelpers()
public <T> T getHelper(Class<T> type)
IllegalStateException
is thrown when no helper is found.type
- The type of the requested helperCopyright © 2013. All Rights Reserved.