You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Mike Ottinger <mo...@tacitknowledge.com> on 2011/07/23 00:55:47 UTC

Design pattern for working with multiple implementations of an interface

Hi All,

	I'm using Felix with SCR annotations. I'm investigating a couple of approaches with declaring a component that makes use of a collection of objects representing various classes that all implement the same interface. This would be something similar I suppose to a configuration factory, although each instance would not be of the same type, rather they would be separate implementations to the same interface. One approach I know would work is to use the target attribute of the @Reference annotation to "handpick" each implementation as a separate dependency. So three implementations of my interface would result in three dependencies each with a unique property I can use as a value for the target attribute. This seems less that ideal as I'd need to add a new dependency for each implementation I make. I'm looking for something similar to a configuration factory where my bindXXX and unbindXXX methods can be called as each implementation is presented to Felix. Is such a thing possible?

Thanks!
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: Design pattern for working with multiple implementations of an interface

Posted by chetan mehrotra <ch...@gmail.com>.
What if you use @Reference with ReferenceCardinality set
ReferenceCardinality.OPTIONAL_MULTIPLE. Then you would get bind and
unbind callbacks
Have a look at Sling AdapterManagerImpl [1] which uses this approach
(though it uses Javadoc based SCR config mechanism). Same thing done
using annotations would look like
@Reference(
        name="AdapterFactory",
        referenceInterface = org.apache.sling.api.adapter.AdapterFactory.class,
        cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE,
        policy = ReferencePolicy.DYNAMIC
)
And then you add
protected void bindAdapterFactory(ServiceReference reference)
protected void unbindAdapterFactory(ServiceReference reference)

Chetan Mehrotra
[1] http://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/adapter/src/main/java/org/apache/sling/adapter/internal/AdapterManagerImpl.java

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org