
Deciding on a Design Pattern
One of the most difficult questions to answer is also the most often asked: How do you know what design pattern to use? My standard response to that query is,
Ask, “What varies?”
Of course, it seems that everything varies just when you need a nice clear answer to that question. The Gang of Four provides a list of design patterns and what varies in each pattern—such as states of an object, an algorithm, responsibilities of an object without subclassing and so on. However, determining the nature of the variation can be as difficult as deciding the design pattern to use as looking at class diagrams. (Download the AIR version of the Variation Table—also known as The Magic Table.)
Go To The Original Source
Chapter 1 in the Gang of Four’s book is the gateway to design pattern knowledge. I must have re-read it 50 times. Further, every time I re-read it, I get something new. You can download Chapter 1 of Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides—just click the download button to bring up the PDF file and then save it:

Find the section, How to Select a Design Pattern and read it. Even if you don’t get it all the first time through, you’ll have a better idea of design pattern selection. Now you’re all set to continue with the rest of this post. Read on!
How Do You Get to Carnegie Hall?
There’s an old joke about a young violinist trying to get directions to Carnegie Hall. He stops and asks an old man, and the old man responds,
Practice, young man, practice!
That may well be the same answer to the question, Which design pattern should I use? Also, I think that’s what Louis Pasteur meant in his oft-repeated quote,
Fortune favors the prepared mind.
(For you purists, his exact quote is, In the fields of observation chance favors only the prepared mind and our French-speaking readers know he actually said, Dans les champs de l’observation le hasard ne favorise que les esprits préparés). The extent to which you play around with design patterns, the better you will understand how to use them. Which one to use is not going to jump out at you unless you’re very lucky–keeping in mind that luck favors those who are prepared. Everyone who has programmed for any amount of time knows his or her algorithms. However, they would be hard-pressed to answer the question,
Which algorithm is the best one to use?
Besides setting off arguments about which is the best, most programmers would point to very specific ones, like a sort algorithm. The more time you spend with algorithms, you begin to know which to use without really thinking about it. The same is true with design patterns, but design patterns are a bit more difficult. As you work with them, the sheer experience will nudge you toward a better understanding. However, they’re not going to clarify themselves.
So how are we supposed to know which design pattern is appropriate? (Don’t even think about which one is best—to get started, start thinking which is appropriate.) Begin with the examples. Concrete examples are good, but you need to be able to see them in a broader scope. In other words, you have to think in terms of applications like this or processes like this. The more you play around with them, the more likely you are to come to the right one.
Begin in Your Own Back Yard
The best place to start thinking about which design pattern is the most appropriate is with the kinds of applications you’re most likely to be called upon to produce. For example, if you’re into streaming media like I am, you want to think about design patterns and the processes involved in video applications. Other areas where Flash/Flex are involved include animation, games, RIAs, education, photography and graphics, communication and e-business of all kinds.
If you begin experimenting with different design patterns for the kind of work you do (or would like to do), then you can see what “fits” in the sense that the design provides a workable solution that allows for easy reuse and update.
Practice, Practice, Practice
To get started, let me throw another Louis Pasteur gem your way and help you understand the mysteries of design patterns.
I am on the edge of mysteries and the veil is getting thinner and thinner.
With that encouragement, I’d like to look at a typical application that might show up on our doorsteps. Further, I’ll keep streaming video out of it because I’ve done so many examples using streaming video already. (Okay, I’ll even omit progressive download videos.) So here it is:
Corporate Trainer Application: This application will be used to train corporate personnel how to adopt and implement new policies including use of new technologies, procedures, and operations. Included features are:
- Text
- Graphics
- Interactive learning
- Ability to be used for wide variety of cases
Those features are off the top of my head, and if you want to tweak it, that’s fine with me. (You can even add video.) Next, look at a summary Gang of Four’s steps (you can find the full version in Chapter 1 of their book that you can download—see download button above.)
- Consider how design patterns solve design problems.
- Scan Intent sections
- Study how patterns interrelate
- Study patterns of like purpose.
- Examine a cause of redesign.
- Consider what should be variable in your design
There ought to be enough examples using ActionScript 3.0 between this blog and our book that you can compare the different solutions and work through GoF’s 6-Step process. Also, remember to download the handy variation table for Step 6.
We’ll carry on the discussion in the Comments section. The purpose is not to get the one correct answer but instead to learn how to use the process. We’re really interested in seeing what you come up with and put the Comments to work.

The How to Pick the Right Design Pattern: Fortune Favors the Prepared Mind by William B. Sanders, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.

Bill Sanders
great! i’ll study it this weekend.
i’m trying to create something like a media gallery framework, that should be a “base” for all sort of image/video galleries and slideshows, ranging from a single image slideshow with no navigation to a image/video gallery with thumbnails, tooltips, slideshow with all sort of navigation buttons and descriptive textfields, and it must be as customizable as possible.
i know there`s a lot of free image galleries out there but most of them are specific, and anyway, I think this might be a good and useful way for me to practice OOP :)
Hi Zak,
Be sure to let us know what you come up with. Your project sounds like one a lot of developers have to deal with.
Kindest regards,
Bill
Hey guys!
Nice post! I also keep reading the most common usage of design pattern to solve some sorts of problems but, as you stated really well, there is no best but the most appropriate! Some times, that old Abstract Factory is not the most appropriate for a specific problem/situation.
I am recently working with a cool Visitor pattern solution for a SQL parser. Quite elegant the solution IMHO.
Cheers guys!
Hi William,
I am very interested what you are working on for an SQL parser using the Visitor. It’s one of those patterns that I’d like to work with more and using it for SQL parsing is a great use. Please tell us more what you’re doing with it!
Kindest regards,
Bill
Hi Bill!
I’m working with the JSqlParser library (http://jsqlparser.sourceforge.net/) and as you can imagine, it is a Java library for parsing SQL statements.
“JSqlParser parses an SQL statement and translate it into a hierarchy of Java classes.
The generated hierarchy can be navigated using the Visitor Pattern”.
This was the first time I REALLY worked with Visitor pattern. It took me a while to get used to it but now I see how clever it is. My application got really flexible!
Prost!
bill_bsb
Hi William (or is it Bill or Memo?),
Is this a project in Java or one where you’re moving the library into AS3 and parsing it?
Anyway, it sounds like an interesting task. What are your different participants in the Visitor doing? Where do you use double-dipatch and how is your traverser set up, and most importantly, what does your ObjectStructure look like? Of all of the participants in the Visitor, the ObjectStructure was the most taxing.
Also, because the Visitor is one of the key design patterns for using composition and delegation, I’d be very interested to see what you’ve done there as well.
By the way, if this is a Java project, is there some way we could use your ideas in AS3?
Please tell us more!
Thanks,
Bill
Hi Bill!
Sorry for the delay for this reply but I don’t get much time for computers during weekends.
This project that I am working on is totally Java and there is no plans for adding any AS3. Unfortunately. =/ Also I don’t got the time to port the JSqlParserLibrary to AS3.
Basically, I’m extending/adding new functionalities to a very limited JDBC driver so I use the JSqlParser library to help me parse and work with the sql statements. I can’t go into much detail here but after the sql string is parsed into Objects by JSqlParser, I just call the accept method passing a Visitor (SelectVisitor, InsertVisitor, UpdateVisitor …). For every feature that it improves, I have a class that implements the Visitors with the specific code. This way the code is very decoupled and cohesive. The code flow is quite cool, thanks to the library!
As for porting the JSqlParser to AS3, well … it is definitely possible but you would have to implement the hardest part which is the actual parser. In JSqlParser they used another library to generate the code.
Cheers mate!
Hi William,
Many years ago I was very into working with Flash and PHP together. So the idea of a SQL parser (ported or otherwise) seemed like a great project.
Chandima and I have been über busy lately, and so we’re hoping to get more reader input. By May I’ll have a lot more time for DP and I’d like to see what could be done with SQL parsing and connecting PHP DP programs with ActionScript 3.0 DP programs.
So many projects–so little time.
Cheers,
Bill