Winston Gutkowski wrote:
Daniel Larsson wrote:EDIT: I just realised I never actually said I wanted the classes to initialise them selves. ...
Hunh? This seems to be getting more and more bizarre.
Quite apart from wondering why you would want to do this, or even the wisdom of having singletons to start with, I agree with Manuel:
back up and explain to us what you're trying to do, not how you want to do it.
Winston
Well I can't blame you, I'm not sure it follow any standard patterns but I find it fitting so far to my purpose and logic, I'm self taught so some eccentricities are to be expected I suppose ;). So why singletons? Because I need the reference to the class, as it will be used in lists. The singletons are stateless actually, they implement an interface and that's it, no real reason to keep them bound to a single instance, but no reason not to either. The reason I want them in a list is that the application will ask another class that manages these classes and produces lists of them(This might be what is called factory methods?). The parameters for retrieving a list of these classes is a list of properties, and the properties of each implementation is retrieved in a method in each implementing class. Thus I want a kind of masterlist of every applicable instance so I can loop through it and select the implementations that have the correct configuration of properties.
I plan to let the application use this list to produce a new list for every possible configuration and be able to retrieve them without creating new lists. There aren't THAT many configurations that will happen so its a marginal expense to have them all in memory instead of making new ones quite often and inducing unnecessary GC(its a lag sensitive application), and also hopefully speeding up the retrieval.
All of this nonsense is because I want to have all the code when implementing new classes for the interface inside the one file, making it easier to plugin new ones and remove old ones without having to remember to clear a manual list somewhere. Over the top? Perhaps. But its a good learning experience! =)
I think I have found a solution, using the classpath I can list all the classfiles in the package that contain these implementations, and thus create instances trough reflection by using the string. This makes me wonder though, what happens with portability of my app when I use that kind of reflection? Anything I should be mindful of? Also can I compile new classes by themselves and just put them in the correct package folder and expect them to be used appropriately?