You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by oweijr oweirj <st...@yahoo.com> on 2012/01/07 03:33:44 UTC
Interfaces, implementations and circular dependencies
I have a set of interfaces and then multiple
implementations of the interfaces. Only one implementation is used in a
given instance of the application.
I'm trying to create separate
projects for the common interface and each of the implementations.
However, the problem I have is that in order to implement the common interface, each of the implementation projects needs to depend on the common interface project. I also need to create a factory method somewhere which will return the appropriate version of the implementation based on some parameters. It seems logical to put this factory into the common project. However, I cannot do this because the implementation projects depend on the common one and this creates a circular dependency in maven.
How does one typically solve this sort of problem. I could create yet another project which contains the factory and depends on all the other projects. Is there a more sensible way?
<parent> +--- <common-interface> +--- <implementation-1> -> depends on common-interface +--- <implementation-2> -> depends on common-interface
Where does the factory go? It depends on all 3 of the above.
TIA
Re: Interfaces, implementations and circular dependencies
Posted by Stephen Connolly <st...@gmail.com>.
the standard java way to solve this is the service loader pattern.
java 6 has explicit support in the serviceloader class, but it is easy to
use classloader.getResources(...) note that's a plural, to roll your own if
you need to support java 5 or earlier.
basically the factory looks for files called
/META-INF/services/fullyqualifiedinterfaceclassname
those files contain the name(s) of implementations of the interface.
no circular dependency required.
a more modern solution is to use dependency injection so that the
implementations have eg @Producers. this is effectively the same as
serviceloader but can be more flexible
- Stephen
---
Sent from my Android phone, so random spelling mistakes, random nonsense
words and other nonsense are a direct result of using swype to type on the
screen
On 7 Jan 2012 04:24, "oweijr oweirj" <st...@yahoo.com> wrote:
> I have a set of interfaces and then multiple
> implementations of the interfaces. Only one implementation is used in a
> given instance of the application.
> I'm trying to create separate
> projects for the common interface and each of the implementations.
> However, the problem I have is that in order to implement the common
> interface, each of the implementation projects needs to depend on the
> common interface project. I also need to create a factory method somewhere
> which will return the appropriate version of the implementation based on
> some parameters. It seems logical to put this factory into the common
> project. However, I cannot do this because the implementation projects
> depend on the common one and this creates a circular dependency in maven.
> How does one typically solve this sort of problem. I could create yet
> another project which contains the factory and depends on all the other
> projects. Is there a more sensible way?
> <parent> +--- <common-interface> +--- <implementation-1> -> depends on
> common-interface +--- <implementation-2> -> depends on common-interface
> Where does the factory go? It depends on all 3 of the above.
> TIA