Doing the right thing. Only if you have the app from the console runs, I would suggest to use
Commons CLI for parsing the input arguments.
Don't see the problem of validation in the constructor (
for example), but if you really want to change — enter a static factory method to which pomestite all the validation, and make the constructor private.
I probably would have done so: Input in the form of a Commons CLI Options, then convert it into a class structure setup (regardless of CLI, in the case of embedded mode and filling of this structure) without validation, a static factory method with this setting, where all the validation and setting of parameters of the business object.
Why is that? Just let us constructively, that is, for example, counter-reflections. - cathrine commented on October 3rd 19 at 02:56
A small clarification:
How to protect code from this:
service = new TService();
service.loadConfig();
service.doJob(); //Then at this stage may be erroneous data. - Patricia commented on October 3rd 19 at 02:59
In fact, a situation will arise that sushestvuet instance of the service is not configured (for example, validateConfig called but threw an exception) - cathrine commented on October 3rd 19 at 03:02
my practical experience confirms — the exception in the constructor is the path to the elusive problems and errors
1. For purposes of illustration, it can be placed at the end of loadConfig()
2. validateConfig you can call at any time, without calling loadConfig during runtime. This is simply on the machine I have every datvie execute in a separate function — debajit and easier to read code - Kian26 commented on October 3rd 19 at 03:05