I would like to know if this kind of design is right, wrong or both.
I have an app with several components (objects), each of them have a configuration (flags, capabilities, etc.)
At any time, I would like to retrieve the configs from the objects.
To so, I did this:
class ConfigRetriever {
public String getConfigString() {
String configString = "";
configString += "component 1 flag : "+component1.getFlag()+"\n";
configString += "component 2 flag : "+component2.getFlag()+"\n";
// ...
return( configString );
}
}
Somewhere else, when config is needed :
class SomeClass {
public void someMethod() {
ConfigRetriever configRetriever = new ConfigRetriever();
String configString = configRetriever.getConfigString();
// Do some stuff with configString...
}
}
I am quite new in object programming and it still feels weird to create an object (ConfigRetriever) for just one specific action (even if the object is able to do other stuff).
I also though about the singleton pattern, with some thing like this :
String configString = ConfigRetriever.getInstance().getConfigString();
It's a neat line, but since the object stays in memory until the end of the app, I don't really know what is right and what is wrong.
Is my design could be better ? How ? Why ?
UPDATE
Thank you for your answers. I think my question was a bit messy and I missed the point about what I was asking for.
The whole config and components story is here as an example of the kind of situation I am dealing with. This is quick and dirty code and I should have warn you about that. The real question was : "is it good to create an object just once (or sometimes) to access to one of its methods ?"
Well, reading your answers and thinking again about it, it seems the right answer would be "it depends of your goal, the classes, the responsabilities and so on..."
Do I want to store informations in my objet ? Can't be static method.
Is a singleton with permanent memory using is a problem ? Most of the time it is, because I think you have to get a good reason to maintain an object in memory with a global state. So, most of the time : no singleton.
Finally, is it a problem to create a class to be used to instantiate an object sometime ? It is not, go for it! :-)