1

I have an interface TestInterface and different classes may implement the interface. Is it possible to have a collection of all the objects which implements the interface? The collection can be created using

LinkedList<TestInterface> store

where store is the name of the collection. But how to keep track of the object creations of classes which implement the TestInterface. And moreover where to keep the collection?

aps
  • 2,452
  • 10
  • 35
  • 47
  • Possible duplicate: http://stackoverflow.com/questions/205573/at-runtime-find-all-classes-in-a-java-application-that-extend-a-base-class – Mikola Jun 25 '11 at 03:46

3 Answers3

0

Not knowing what you want this for, it's hard to tell.

But anyway, yes you can create a collection but in order to track and store references to the TestInterface objects, you must control their creation.

The easiest way would be to have a TestInterfaceFactory (see AbstractFactory pattern), this is also a good place to keep the store collection with the instance references.

Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
0

Is it possible to have a collection of all the objects which implements the interface?

In theory yes.

In reality, it will only work if everything that creates an instance of the object also adds it to the list. And that probably makes it impractical ... unless you change the way the objects are created.

  • One approach use factories, but there is nothing to stop some code creating an instance without using your carefully implemented factory.

  • Another approach would be to replace the interface with an abstract base class whose constructor(s) guarantee that every new instance is added to the list. This can't be subverted using normal code. However, if you use Java Object Serialization (or similar) you'll need to put a "hook" into the base class to make sure that deserialized objects are added to the list.


Note that a collection that contains all instances of some interface or class is potentially a huge memory leak. You'll probably need to do something about this; e.g. using weak references.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I have a class called students and a class called soldiers. both of these classes implement the phone interface, i.e. both students and soldiers can communicate with each other using phone. I can in the future also create other classes such as doctors, engineers which may also implement the phone interface or some other interface such as internet. someone who uses phone can only communicate with those who use phone.similarly for internet. – aps Jun 25 '11 at 05:20
  • the problem is: when i create a "student" object or a "doctor" object, i can easily create add them to their respective collections inside their respective classes.but how to keep track of all those who are using phone...because otherwise i cannot communicate with other entities using phone. only doctors can communicate with doctors, students with students...but i want those who use phone to be able to communicate with each other, irrespective of what the other entity is... – aps Jun 25 '11 at 05:22
  • @aps - I think my answer covers this. There's no reason why the list needs to be class specific. – Stephen C Jun 25 '11 at 09:19
0

If you desperately need to make this automatic, then in principle, you could:

  • use the Java instrumentation framework, adding a ClassFileTransformer to ensure that any class implementing your interface is redefined to have its constructors automatically add instances to your collection (look at something like the Bytecode Engineering Library, BCEL, if you were to go down this route);
  • possibly easier but still quite a lot of work: use the JVM Tool Interface to write an agent that can query the heap on the fly for instances of your object.

Either way, if the interface is yours, then introducing a programming convention whereby all instances of implementations of that interface are added to the collection is definitely easier, if more error-prone.

Neil Coffey
  • 21,615
  • 7
  • 62
  • 83