D
Downturn
I'm not sure if this is possible, or even a very good idea, but here's
what I'm trying to accomplish.
I have a top level interface, InterfaceMain. Then I have a bunch of
interfaces that extend InterfaceMain. Now, I've got another interface,
InterfaceCollection, which extends the java.util.Collection interface
(doesn't add anything, it's just there for typing purposes).
InterfaceCollection is generic, though what I really want is for
InterfaceCollection to be used for anything that implements
InterfaceMain or any interface that extends InterfaceMain.
So then I have a bunch of classes that implement one of the interfaces
that extend InterfaceMain, and I have a class that is to implement
InterfaceCollection.
This all seems rather straightforward so far except that the elements
that will be in a collection of InterfaceCollection aren't actually
instantiated until that specific entry is to be returned via
Iterator.next(). The problem I'm having is trying to figure out which
one of the implementation classes to instantiate based on the type of
the generic that the InterfaceCollection class was instantiated with.
I'll try to explain in a slightly different way that may help:
Read this as kind of an inheritance tree:
InterfaceMain
|
--- InterfaceA
|
--- InterfaceB
|
--- InterfaceC
InterfaceCollection
|
--- InterfaceCollectionImplA
InterfaceAImpl implements InterfaceA
InterfaceBImpl implements InterfaceB
InterfaceCImpl implements InterfaceC
// Assume getCollection() creates an instance of
InterfaceCollectionImplA
InterfaceCollection<InterfaceB> collectionObject = getCollection();
--------------
So now I've got an instance InterfaceCollectionImplA instantiated with
the generic type InterfaceB as collectionObject. As I said above, the
elements in collectionObject have not yet been instantiated. However,
when collectionObject was instantiated it was essentially given a list
of the objects that are part of its collection, so it has the data
necessary for the objects that should be instantiated on the fly.
Now, trying to implement the Iterator is what's killing me. The
Iterator is supposed to instantiate the right instance of the
implementation class (InterfaceAImpl, InterfaceBImpl, or
InterfaceCImpl) based on the interface used as the generic type, in
this case InterfaceB, so it should be instantiating InterfaceBImpl.
Is it possible to determine in InterfaceCollectionImplA what interface
was used for the generic value (InterfaceA, InterfaceB, or InterfaceC)
and then based on that instantiate the correct implementation class
(InterfaceAImpl, InterfaceBImpl, InterfaceCImpl)? Is this really even
that great of an idea? Should I be doing something completely
different?
I hope I've done a decent job of explaining the problem.
Thanks very much.
Matt
what I'm trying to accomplish.
I have a top level interface, InterfaceMain. Then I have a bunch of
interfaces that extend InterfaceMain. Now, I've got another interface,
InterfaceCollection, which extends the java.util.Collection interface
(doesn't add anything, it's just there for typing purposes).
InterfaceCollection is generic, though what I really want is for
InterfaceCollection to be used for anything that implements
InterfaceMain or any interface that extends InterfaceMain.
So then I have a bunch of classes that implement one of the interfaces
that extend InterfaceMain, and I have a class that is to implement
InterfaceCollection.
This all seems rather straightforward so far except that the elements
that will be in a collection of InterfaceCollection aren't actually
instantiated until that specific entry is to be returned via
Iterator.next(). The problem I'm having is trying to figure out which
one of the implementation classes to instantiate based on the type of
the generic that the InterfaceCollection class was instantiated with.
I'll try to explain in a slightly different way that may help:
Read this as kind of an inheritance tree:
InterfaceMain
|
--- InterfaceA
|
--- InterfaceB
|
--- InterfaceC
InterfaceCollection
|
--- InterfaceCollectionImplA
InterfaceAImpl implements InterfaceA
InterfaceBImpl implements InterfaceB
InterfaceCImpl implements InterfaceC
// Assume getCollection() creates an instance of
InterfaceCollectionImplA
InterfaceCollection<InterfaceB> collectionObject = getCollection();
--------------
So now I've got an instance InterfaceCollectionImplA instantiated with
the generic type InterfaceB as collectionObject. As I said above, the
elements in collectionObject have not yet been instantiated. However,
when collectionObject was instantiated it was essentially given a list
of the objects that are part of its collection, so it has the data
necessary for the objects that should be instantiated on the fly.
Now, trying to implement the Iterator is what's killing me. The
Iterator is supposed to instantiate the right instance of the
implementation class (InterfaceAImpl, InterfaceBImpl, or
InterfaceCImpl) based on the interface used as the generic type, in
this case InterfaceB, so it should be instantiating InterfaceBImpl.
Is it possible to determine in InterfaceCollectionImplA what interface
was used for the generic value (InterfaceA, InterfaceB, or InterfaceC)
and then based on that instantiate the correct implementation class
(InterfaceAImpl, InterfaceBImpl, InterfaceCImpl)? Is this really even
that great of an idea? Should I be doing something completely
different?
I hope I've done a decent job of explaining the problem.
Thanks very much.
Matt