I am trying to add a dynamic logging class to my program (for changing where log messages go e.g. System.out or a file). It extends the base abstract Logger class which has a static method log(String). Is it possible to set the logger class and then invoke the log method without making an instance of the logger or using reflection?
Something like this:
public class MainController {
private Class<? extends Logger> mLogger;
// ...
public void setLogger(Class<? extends Logger> logger) {
mLogger = logger;
}
public Class<? extends Logger> getLogger() {
return mLogger;
}
// ...
}
public class BrokenTest {
// ...
private void showErrorMessage(String message) {
mMainController.getLogger().log(message); // Can't call .log on Class
}
}
Something like this:
This will call a static method named
logwith aStringparameter on the class returned bygetLogger().UPDATE
But from your description, I believe using an interface and separate implementations would be better. This way, you can work with instances and normal polymorphic calls.