An ethical question here.
I'm planning on using several manager classes in my new project that will be performing various tasks across the whole project. These classes are singletons, but require construction based on parameters.
As to when/where this construction has to happen, I have mixed feelings. I have these options so far:
Option A
It's easy to just pass these parameters to the getInstance
method while having a default null
value. On the very first call the parameters will be used, and any additional calls completely ignore them.
While this works, doing so feels rather unlogical, for the following reasons:
It makes documentation unclear.
getInstance
' first parameter must be of typeCollection
, but can benull
... what's going on here? You can argue that writing a line about this in the description will clear it up, but I'd prefer clarification to be unneccesary.It feels faulty to pass
getInstance
any construction parameters. This is due to the fact that the method name does not explicity hint towards construction, making it unclear it will happen.
Option B
I'm thinking about a setup
method. This method takes all parameters, calls the class constructor, and changes the internal class state to initialized
.
When calling the getInstance
method prior to setup
, it will throw a NotInitializedException
. After setup has been called, any additional calls to setup
will result in a PreviouslyInitializedException
.
After setup
has been called, getInstance
becomes available.
Personally, this option appeals more to me. But it feels excessive.
What option do you prefer? And why?