Ever built a desktop tower PC from components? Or ever upgraded a tower PC?
What’s the easiest component to change?
You were thinking “RAM” right? Man you can unclip one of those little silicon buddies and snap a beefier one in place in less than a minute. Once the case is open.
No, not RAM.
How about your USB memory stick. That’s pretty easy to change. If you don’t use one, how about your mouse? Or headphones?
Okay we’re thinking outside the box here. What about software? Ehhh not as easy as headphones. Oh how about the user! Well unless your password is “123456”…
I’m going with headphones.
Right so now you’re ready for the real question. What’s the hardest component to change?
You’ll be looking for trick answers now, this is good.
The desk? Heh, nice outside the box thinking – yeh that’s hard to change. Pretty much have to unhook and move everything to get that thing shifted.
Motherboard? Getting warmer. But come on, that’s obvious.
No. What I’m thinking about is
The case is an interesting little beast. It does one job and does it well. It keeps things that should be inside the box inside it; things that are outside outside. But while doing that it also provides external ports for headphones, mice, keyboards and monitors; it provides internal fixings for the motherboard, dvd drive, power supply, sound card and so on.
But the really nice thing about the case, is that if I go and sit at your computer with my headphones, I don’t need to give a damn about what is inside your case. I just plug my headphones in, put on your music and I start changing your desktop background.
And the other really nice thing about the case is that if you want to start swapping out components – your hard drive could stand to be a bit bigger – you can just swap that out. And no-one needs to care what is outside your case.
Your case makes things easy to change.
Except the case.
Its hard to change the case, because its busy making everything else easy to change.
I’m not just talking about swapping your case for another case. Could you actually swap your case for something that wasn’t a tower case but solved the same general problem?
Well sure you could but you might need to redesign the bits that fitted inside it. Laptop cases have many of the same features. Server racks solve a similar problem. Once you’ve decided on a tower PC case, you can swap any of the bits inside and out but changing it into a laptop is a tall order.
The case is a real world example of a design pattern at work.
Software design patterns are common solutions to common problems.
Hey I need to be able to get an instance of an object… but I don’t really care if that object is a derived type or not – I shouldn’t be the one making that decision… and I don’t want to think about how that object is constructed. Maybe you want a factory pattern?
I need an action performed for me. I know what kind of inputs I’ll have every time, but I really don’t know what the content or algorithm of the action will be. I just need to know I can invoke it and it’ll behave consistently as far as I’m concerned whatever other stuff is really going on inside. Sounds like a strategy pattern.
The thing about design patterns in software is that they make /some/ things easy to change, both in terms of implementation behind the pattern, and the consumers in front of the pattern. But once you’ve decided on a design pattern, the pattern itself becomes hard to change.
So every use of a pattern is a trade off. You say “I’m going to sacrifice being able to change this later – because I want to be able to change that instead.”
In the GoF Design Patterns book, each pattern is described alongside what it makes easy to change. The trade off is the pattern itself.
So what am I saying – are patterns bad?
They’re not good either. Design patterns are a tool. Effective programmers may be more effective because they use design patterns effectively, in context, and with knowledge of what is being traded.
Ineffective programmers may be ineffective because they don’t know about design patterns and so don’t apply them.
Seriously ineffective programmers – well they might know some patterns and apply them blindly and inappropriately – like a confused plasterer in a new house, with every floor, ceiling and wall tiled painstakingly carefully in mellow blue and green colours. “I worried it might get wet,” they say, “so I copied this thing I saw in a swimming pool once.”