3 ms·
ConnectionPool is an excellent example of a bad case for Singleton: what if you have two databases? What if you want to use different pools for different types
by sah 19y ago
ConnectionPool is an excellent example of a bad case for Singleton: what if you have two databases? What if you want to use different pools for different types of queries?
A global variable is different from a Singleton because if you write a Singleton class in some module that I want to use, I have to fork your module if I want to make it possible to use two of them. If you use a global variable, I can always make a second global variable.
- Tichy 19y agoOK, I am not even sure anymore what the singleton pattern is _(it's been a while). If it is the class constructor that makes sure you can only ever create one instance, it might be bad. I thought any mechanism for ensuring singleness would qualify for the pattern. Then you examples wouldn't invalidate the connection pool, you could just have to different pools, one for each database. I agree that with the "class instantiation implementation" you could probably not do that.
- bayareaguy 19y agohttp://c2.com/cgi-bin/wiki?SingletonPattern http://c2.com/cgi-bin/wiki?SingletonPattern has some good discussion on it.