English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Suppose I have an interface called Account.

public interface Account {
   public int getAccountNumber();
}

Say I then create two implementations:

public class BankAccount implements Account {
   public int getAccountNumber() { return 100; }
}

public class TelephoneAccount implements Account {
   public int getAccountNumber() { return 200; }
}


... Now I want to create a class with a main method that when executed lists all implementations of the Account interface. It should also pick up any implementations that are stored within JAR files that are on the classpath etc.

So far I have tried using reflection and the apache commons discovery project, but have not been successful :-(

Any anyone help? :-)

2006-07-11 22:49:55 · 3 answers · asked by Tim C 1 in Computers & Internet Programming & Design

3 answers

The Java class loader cannot know whether a class implements an interface without actually loading the class. Therefore you are proposing to load every single class in the class path in order to find out if any of them implements your interface. THIS IS MADNESS. I will take you forever just to load the classes in the Java Runtime Library (although one might argue that it is *very* unlikely that one of those classes implements your interface :-) ).

Your algorithm for finding Account types is like this algorithm for finding all the elephants in Africa:
- start at the southern tip of Africa
- move from east to west and:
. . -- grab every animal in arm's reach and check it to see if it is an elephant
. . -- if so, put it in your backpack; if not, put it back where it was
- when you reach the shore/border move one "arm's reach" north and reverse direction and continue as before.
- stop when you reach the northern tip of Africa
- inventory the animals in your backpack
- DONE

There are several fairly major problems with this algorithm (as there are with yours):
- it takes too long to execute; your first elephant will die long before you find your last
- it requires too much backpack space; how many elephants will a typical backpack hold anyway?
- it assumes there are no obstacles to a straight-line path; lakes, cliffs, rivers, mountains, hungry panthers, etc.
- it assumes every elephant in Africa will stand still while you execute your search

However, YOUR search does not have to be this problematic. Why try to deal with all the wild elephants in Africa? Why not just deal with elephants in zoos? You are the one defining what it means to be an "elephant" (Account). Why not cage the elephants by building the cage into the definition of "elephant"? Why not build an AccountTypeRegistry and require that every account type be registered? It would not be hard to add a getRegistryId() to your Account interface. It would not be hard to set up your program/framework to only work with registered account types.

Basically you want to make the "contract" for your Account interface such that anyone who creates an account type but does not register it will miss out on all the goodies. That is how you ensure that you will always be able to find all the account types.

2006-07-14 13:06:56 · answer #1 · answered by BalRog 5 · 1 0

There is an easy way and a hard way.
Hard way:
I suppose if you find some way to iterate through all classes possible, you could check each one to see if it extends the interface. Not sure how to list all the classes though, gotta figure that one out.
Easy way:
eclipse already did it. For example, in the package explorer, right click the interface .java file and select 'show type hierarchy.'

2006-07-11 23:15:23 · answer #2 · answered by billyboy 3 · 0 1

Java delivers sequence framework, init java has provided you with each and every of the implementations you want. in case you prefer to layout a stack, it already there in java.util kit. in case you prefer to layout, in simple terms make higher a classification from LinkedList classification and implement mandatory function for the stack.

2016-10-14 09:32:25 · answer #3 · answered by carris 4 · 0 0

fedest.com, questions and answers