Note: This is another pre-amble post before we actually look at implementing design patterns in our code. I mentioned some of these ideas in an earlier intro post to design patterns, but I wanted to take some time to dive deeper and draw out in more detail the points I was trying to make.
Sometimes design patterns can become buzz words developers will use. Maybe they think its vogue to be able to drop in terms like facade, proxy, adapter, observer, etc. in their discussions for the same reasons that some people like to use big words to make them sound smarter.
Doing so misses the point of what design patterns are supposed to provide though. Before we talk about design patterns, let's talk about patterns in general so we get our heads around the concept.
Patterns are a way of communicating a repeatable concept that has been tested and proven. They allow us to remove ambiguity in our requests by using a common language that the sender and receiver both understand. Let's consider some real world examples:
How often do you walk into a McDonalds and say "I'd like two all beef patties, special sauce, lettuce, cheese, pickles, onions, and all on a sesame seed bun."? You don't...you tell the person you want a "Big Mac". A "Big Mac" could be thought of as a burger pattern: it points to a certain way to create a burger. It also is a shared term that's part of the language used when in a McDonalds.
I was traveling with my wife in Quebec a year or so ago, and we drove from Montreal to Quebec City. On our way back we stopped in this small French town...and I mean FRENCH, as in English was *not* an equal opportunity language. We walked into McDonalds and I figured that my French was good enough that I should be able to order from the menu. After asking the girl at the counter if she knew English (which we got a hand motion to suggest she was shaky on it), I decided to wow both my wife and this counter girl with my command of the French language.
"Numero une si-vouz-plez" (as in Number 1 which is a Big Mac meal)
She looked at me like I had just spoken Swahili. I panicked for a second...this could only get worse...until my wife rescued me.
"He wants a Big Mac meal"
The counter girl smiled and nodded and realized what it was I tried to order in my horrible attempt at French. Even though neither of us understood each others language, we did understand the pattern of what "Big Mac" meant.
Design patterns are the same way. Design patterns allow us to communicate established designs using common terms who's representations across OO languages may change in implementation but not in basic design.
But here's the real important thing about design patterns, the one thing that you *must* understand about how to use them properly:
Each design pattern is a solution to specific problem in software development.
That's important, because although design patterns are there to help us, that doesn't mean we should or have to use them. Design patterns are in place to make our lives easier for when we encounter the situations they solve. Or in other words, what the context of the problem is.
You don't just use a dovetail joint in carpentry for every situation...only when its warranted. The same goes for design patterns. Have you ever heard someone say "Oh, we should implement a Facade layer here!" Well why? A facade is meant to make interaction with complex subsystem(s) easier. Do you have complex subsystems? Are you just interacting with a tier that you're creating as part of your current application? Does the true idea of what the facade pattern means really make sense?
As we go through the different design patterns that are in the wild, we'll talk about where you'd use them and what problems they solve.
D