Hi all,
ok so this is all about the singleton pattern. I find it pretty usefull, but I often read, that singletons are anti-patterns and they are no good use for OOP because they sprinkle dependencies all over the place in my programm. Still, I find myself using this pattern quite often. I wrap it around database connections, sessions, user-objects, Error-message-collectors, Input-Controllers and so on. So what do you think, is this good practice? Or what should I use instead?
cya
Question on Singleton
Moderator: General Moderators
-
SwizzCodes
- Forum Newbie
- Posts: 20
- Joined: Tue Dec 19, 2006 3:23 pm
-
SwizzCodes
- Forum Newbie
- Posts: 20
- Joined: Tue Dec 19, 2006 3:23 pm
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
You also might want to look through the long list you provided to see if it is:
1. Actually possible for you code to create multiple instances improperly. Often programmers implement Singletons to protect against use cases that don't exist.
2. Possible to inject those objects into the objects that use them rather than accessing them as a global. The injection could be directly or via a Registry.
1. Actually possible for you code to create multiple instances improperly. Often programmers implement Singletons to protect against use cases that don't exist.
2. Possible to inject those objects into the objects that use them rather than accessing them as a global. The injection could be directly or via a Registry.
(#10850)