4 ms·
The major problem they cause IMHO is coupling. Some examples of how this has bitten me: - can't instantiate the singleton differently. - program does task A,
by mattfenwick 13y ago
The major problem they cause IMHO is coupling. Some examples of how this has bitten me:
- can't instantiate the singleton differently.
- program does task A, then task B, and both use the singleton; now you have to worry about the state that the singleton was left in after completing task A.
- can't create multiple, separate instances.
- can't easily mock for testing.
- can't easily swap out with a different implementation.
I've run into each of these problems, and I can never see any value provided by the singleton. All the pattern does is reduce the flexibility, adaptability, and usefulness of the code it's applied to (at least in my experience -- I'm not implying that it's always or necessarily so).
- jason_slack 13y agoI definitely agree that there are places where a singleton just is in no way the correct option. You have laid out some good examples here.