The software engineering radio podcast has a very interesting interview with the Gang of Four members, from “Software Design Patterns” fame. It is roughly 75 minutes, but it is really nice seeing them comment on their book 20 years later.
Here is a transcript I did of a portion of the podcast that I found extremely interesting (validating of my point of view, actually), that I just had to write down and keep, so I could quickly use it as a way to avoid discussions about the recurring Singleton-as-global (anti?) pattern.
This took place at around minute 56 onward, when they were commenting on how the patterns in their book stood the test of time.
Ralph Johnson: (…) I would say that creational patterns, we have been making comments about Singleton, but in fact all the creational patterns are not used as much as they were back when we wrote the book because dependency injection is sort of an alternative pattern that …
Erich Gamma: Decouples things
Ralph Johnson: is sort of a better way of doing things. There are still cases where the other patterns make sense, but they just make sense a lot less frequently because dependency injection is often a better way of doing things.
Johannes: So you mentioned in the beginning, so I’m just saying the word of the pattern: Singleton.
Erich Gamma: So we added it, and it was an example everybody understood. In C++ how you do Singleton, have a static member and whatever, right? Everybody got that immediately, I think. That was a great way to tell someone what you talk about, but if you think about it doesn’t have a lot of coolness, Singleton, right? You something global which is kind of against objects, which are more about distributing things and not having a centralised thing which will at some point hurt you because you want to have multiple of these things, or whatever, right? Its often a design shortcut. To me Singleton is often a design shortcut. You have a well known place to get to another object, rather than reach into objects to get to this space, so thats why it often hurts [unintelligible] so that why its not on my top list of favorite patterns, right? To me its a list of the things that will be listed off of the island: Singleton.
Ralph Johnson: In Smalltalk Singleton has not been the problem that it is in C++. What people have used it for in Smalltalk is: you try to eliminate global state when you can, when you just can’t get rid of it, then you manage it, and that’s a good way of managing it. But the problem is, I think in C++ is a lot of people instead of using it as a way to manage global state you can’t get rid of, they instead use it as an excuse, or something that will make it ok, to have global state.
Erich Gamma: thats a justification for having global state.
Ralph Johnson: It’s a justification for having global state. “Its a pattern! Its ok because it is a pattern!” And global state is bad, you want to get rid of it as much as you can.
Erich Gamma: And my rule is it is very easy to add global state, but it is very hard to take it out.
Johannes Thönes: So you think in retrospect you shouldn’t have put it into the book ?
Erich Gamma: You see, I love all the patterns that we have in the book. So that is something … I made peace with them.
Ralph Johnson: So I’ve got a version of Singleton that I’ve rewritten. I wrote it in a way to make it really clear that global state is bad and this is only what you do when you can’t get rid of it. So we could have written it in a way that would have made that more clear. Its just that we didn’t realize how people were going to interpret it. We didn’t realize that people would have taken it as a justification for global state. You just put the book out there and see what people do with it.