I’m trying to get my head around the usage of the factory pattern, when I wish to disable features within an application.
Lets say for example I have a factory called LoggerFactory, which creates a logger instance.
If in the configuration logging is disabled in my application:
Should the logger factory pass back an instance of the logger that is a dummy, that doesn’t do anything? So any code using the logger does not need to change.
Or, is it the responsibility of the code using the logger, not to use the logger if it is disabled in the configuration?
Or, is it the responsibility of the logger itself not to do anything, if it is disabled?
Thanks for the help.
If you have many different logger-classes (e.g. log to file and log to network) then I think returning a dummy logger when logging is disabled is the simplest and most flexible solution. It will require no change to the code that uses the logger, and it will require no changes to the other loggers.
The idea of making the user of the logger responsible is bad, as that would spread the feature of disabling the logging through the entire code, thus polluting application logic.