Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I keep track of (enumerate) all classes that implement an interface
    text
    copied!<p>I have a situation where I have an interface that defines how a certain class behaves in order to fill a certain role in my program, but at this point in time I'm not 100% sure how many classes I will write to fill that role. However, at the same time, I know that I want the user to be able to select, from a GUI combo/list box, which concrete class implementing the interface that they want to use to fill a certain role. I want the GUI to be able to enumerate all available classes, but I would prefer not to have to go back and change old code whenever I decide to implement a new class to fill that role (which may be months from now)</p> <p>Some things I've considered: </p> <ol> <li>using an enumeration <ul> <li>Pros: <ol> <li>I know how to do it</li> </ol></li> <li>Cons <ol> <li>I will have to update update the enumeration when I add a new class</li> <li>ugly to iterate through</li> </ol></li> </ul></li> <li>using some kind of <code>static</code> list object in the interface, and adding a new element from within the definition file of the implementing class <ul> <li>Pros: <ol> <li>Wont have to change old code</li> </ol></li> <li>Cons: <ol> <li>Not even sure if this is possible</li> <li>Not sure what kind of information to store so that a factory method can choose the proper constructor ( maybe a map between a string and a function pointer that returns a pointer to an object of the interface )</li> </ol></li> </ul></li> </ol> <p>I'm guessing this is a problem (or similar to a problem) that more experienced programmers have probably come across before (and often), and there is probably a common solution to this kind of problem, which is almost certainly better than anything I'm capable of coming up with. So, how do I do it?</p> <p>(P.S. I searched, but all I found was this, and it's not the same: <a href="https://stackoverflow.com/questions/976208/how-do-i-enumerate-all-items-that-implement-a-generic-interface">How do I enumerate all items that implement a generic interface?</a>. It appears he already knows how to solve the problem I'm trying to figure out.)</p> <p>Edit: I renamed the title to "How can I keep track of... " rather than just "How can I enumerate..." because the original question sounded like I was more interested in examining the runtime environment, where as what I'm really interested in is compile-time book-keeping.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload