Let's say, I have an interface
ISomething. I also have up to
n implementations of the interface, for example
BSomething. I want to determine all the implementations of that interface and save them to a list or another useful data structure.
One solution I know is, find all implementations via a reflection. My question is: Is this the best way to do it? Is there a pattern that each implementation registers itself to the system by calling a method? If yes, how is it called? As the project grows, looping over all the classes just two find the two implementations seems inefficient to me.
The language here is vb.net but the question targets object oriented languages in general.
Edit: To be a bit more specific on the question.
I have a UI which can create projects. Previously, these projects had different data but were all of the same type. A project is generally generated by copying a template file into a folder and then filling some values.
The situation now is, that a project can either be of type
A or type
B. The difference is, that a different template file is copied and also some other data is loaded differently. There is also a chance, that in the future there might be the type
C, but I don't know this yet. My idea was the creation of an interface and several implementations of this interface. The UI would then, upon loading, look for all implementations of that interface and give the user a choice via a dropdown or similar control.
I thought when another developer creates
C, it would be good if this new functionality automatically appears in the user interface without the other developer touching the code for the UI. I was hoping to avoid endless if-else chains and also I wanted to avoid an explicit specification of each project type.
This idea lead to the question above.