Find number of implementations of interface

Given an interface, is it possible to find out how many implementations there are for that particular interface?

Say I have an Interface InterfaceObj. ClassA, ClassB and ClassC implement InterfaceObj. Is there some way, I can find out how many implementations there are for InterfaceObj? [in this case 3].

[318 byte] By [smartsagittariana] at [2007-10-2 5:44:29]
# 1
> Given an interface, is it possible to find out how> many implementations there are for that particular> interface?No.
CeciNEstPasUnProgrammeura at 2007-7-16 1:54:29 > top of Java-index,Java Essentials,New To Java...
# 2
Not in a clean, reliable way.You can search through elements of your classpath, but that won't work if any of them are at HTTP URLs.
targaryena at 2007-7-16 1:54:29 > top of Java-index,Java Essentials,New To Java...
# 3

> Given an interface, is it possible to find out how

> many implementations there are for that particular

> interface?

>

> Say I have an Interface InterfaceObj. ClassA, ClassB

> and ClassC implement InterfaceObj. Is there some way,

> I can find out how many implementations there are for

> InterfaceObj? [in this case 3].

This silly question again? Seems about every week or so someone has an apparant backwards design and so up pops this question yet again.

warnerjaa at 2007-7-16 1:54:29 > top of Java-index,Java Essentials,New To Java...
# 4

Well, it's more like this:

I have a Class which needs to implement certain methods in various components of the system. Now all these components implement an interface and hence have the required method. Say a new component gets added in the future, then the only change that would be required [if there was a way to keep track of the number of implementations] would be adding the new Class as such, instead of tweaking the original file invoking these components. Please tell me how I can improve this design so that minimal changes are required on addition of new components.

In a nutshell:

I have a Class MainClass. This invokes a method on all classes implementing the Interface MyPrototype. So if ClassA, ClassB, and ClassC implement that Interface, MainClass need not add a separate line of code for each Class implementing that interface. That way, addition / deletion of components can be handled efficiently. Or that is what I felt unless it is a very 'backward' design.

smartsagittariana at 2007-7-16 1:54:29 > top of Java-index,Java Essentials,New To Java...
# 5
So you are after plugins, normally this is done via searching a folder, or reading a props file, not by querying the system for all classes that follow patten XYZ.
mlka at 2007-7-16 1:54:29 > top of Java-index,Java Essentials,New To Java...
# 6
in a simple, stragithforward way, id just externalize the class names that implement the desired interface, say in a properties file... that way, you might even change the behavior of some class, by keep tracking of midification on the file and loading that again...
takeshi10a at 2007-7-16 1:54:29 > top of Java-index,Java Essentials,New To Java...
# 7

> Well, it's more like this:

>

> I have a Class which needs to implement certain

> methods in various components of the system. Now all

> these components implement an interface and hence

> have the required method. Say a new component gets

> added in the future, then the only change that would

> be required [if there was a way to keep track of the

> number of implementations] would be adding the new

> Class as such, instead of tweaking the original file

> invoking these components. Please tell me how I can

> improve this design so that minimal changes are

> required on addition of new components.

>

> In a nutshell:

>

> I have a Class MainClass. This invokes a method on

> all classes implementing the Interface MyPrototype.

> So if ClassA, ClassB, and ClassC implement that

> Interface, MainClass need not add a separate line of

> code for each Class implementing that interface. That

> way, addition / deletion of components can be handled

> efficiently. Or that is what I felt unless it is a

> very 'backward' design.

Sounds like you should be keeping a config file which lists the classes that you want to instantiate and invoke at runtime.

At runtime, you read that config and dynamically instantiate each class by name (using the reflection APIs), cast each instantiated object to the interface type, and invoke your desired method(s) using that.

warnerjaa at 2007-7-16 1:54:29 > top of Java-index,Java Essentials,New To Java...
# 8

I'm interested in doing this as well. I am working on a utility program, similar to DB2's SPUFI, for working with database via JDBC. I would love to simply detect the drivers and related files. Right now I use a configuration file, but because my company has more than one build for workstations -- not to mention developers reconfiguring their boxes -- installing this beastie is troublesome.

This utility is a port from a program I wrote in VB.NET, so this can be done in the Microsoft Hegemony, but for the Linux and Mac users, so far I am out of luck.

I've been looking at a classpath scan that writes a configuration file. The question is how ofter the scan triggers to update the config file. Even on a low-priority thread, this could slow the system.

sgelmana at 2007-7-16 1:54:29 > top of Java-index,Java Essentials,New To Java...
# 9

I'd like to do much the same thing. Have a basic program that knows what to do with a class that implements inteface IFace. On startup, have it search for classes which implement IFace. Load each class. It's such a trivial task to do with DLLs in Windows-land that I'm surprised to find it nearly impossible in Java-land. Since this is a database application, I ended up with a table in the db that has columns for Interface and ImplClassName so that when I want to add another implementation of IFace, I just add a new row to the table and the application startup code looks for the new class and uses it.

dalbertsona at 2007-7-16 1:54:29 > top of Java-index,Java Essentials,New To Java...
# 10

Hmmmm. You know if you used the Preferences API you could store the implementation in some sort of system specific repository.

On Windows this system specific repository would in fact be the registry.

Which is exactly what happens with your DLLs. So actually you can do the exact same thing with Java. It's just that Java does it in a more platform independent way.

cotton.ma at 2007-7-16 1:54:29 > top of Java-index,Java Essentials,New To Java...
# 11

As per the DependencyInversionPrinciple the High level modules should not depend upon low level modules.As a matter of fact the parent interface in this case should not even know or be aware of who if at all is implementing it.

In case your design requires that you need to go back to drawing board.

kilyasa at 2007-7-16 1:54:29 > top of Java-index,Java Essentials,New To Java...
# 12
*WARNING ZOMBIE THREAD*
cotton.ma at 2007-7-16 1:54:29 > top of Java-index,Java Essentials,New To Java...
# 13
> *WARNING ZOMBIE THREAD*nah, it's less than a year dead before some idiot revived it without reading any of the replies.
jwentinga at 2007-7-16 1:54:29 > top of Java-index,Java Essentials,New To Java...
# 14
i just wish as was cool as you
jeffkirbya at 2007-7-16 1:54:29 > top of Java-index,Java Essentials,New To Java...