Suppose this is a configuration data object that I read from DB/XML:
class ConfigurationClass{
//some configurations
public String getString(){
}
}
Now this is the class that uses this configuration object
class ConfigurationUser implements Reloadable{
private ConfigurationClass configuration;
//some extra logic required for making object ready to use
public void init(){
// some new object is created based on configuration
SomeObject obj = new SomeObject(configuration.getString());
//register to configuration manager
}
@Override
public void reload(){
//does whole reloading stuff
//creates a new instance of configuration class and reads new configuration which is not appropriate
}
}
Now when I have to reload the configuration then suppose I create a new instance of ConfigurationClass and inject it in ConfigurationUser class.
But I will also have to re-initialize the object, so that it can create new configuration dependent objects.
Is this a proper design to do it or there is a better approach? I thought of using Spring or Google Juice for DI, but again I will have to provide the callback to the configuration user class, that configuration has been reloaded.
I am new to using IoC containers so dont know if it is possible. Need to use this in real time server application so have to be strict about garbage production and performance.
Current solution:
public interface Reloadable{
public void reload();
}
I have a configuration manager to which I register all the configuration user instances, and when reload is to be done, manager just calls reload and it reads new configuration and re initializes all dependent objects.
Any help would be appreciated.
I think you’re looking for an Observer/Listener design pattern. Your configuration user should be an “observer” and a configuration itself will be a “subject”. First, the observer registers itself in a subject. Next, every time a subject is changed it notifies all observers, and they reload (or do anything they wish).