After careful reading the facts about singletons (the code smell, not the pattern) I wonder:
How can I refactor my code to get rid of them?
Despite almost everyone agreeing that bad singletons are, well, bad, I could not found any practical advice on how to replace them. Either it's very trivial or very hard.
There are some ways I can think of but all of it seem to bloat my code tremendously.
For example let's say I have a "global" AppConfig
class which holds license information about the product and describing the features available to the user.
What I can think of:
- Create a common base class for each and every class of my projects which holds an
AppConfig
instance. (BAD: Impossible for cases where you already have a base class, e.g. forms) - Create a common interface with a
setAppConfig
method. - Create a global
AppConfigFactory
which can createAppConfig
instances (BAD: Only shifts the problem to another class) - Pass the instance as parameter to every method which needs it. (BAD: Code bloat)
- ...
What can I do?
EDIT: Clarification: I have identified a bad singleton in my code. Now I want to refactor my code to remove it. I'm asking for tips and general ideas on how to achive this.