classical geek

Design Patterns and Real World Problems

Design Patterns can be seen as rather high-faluting or academic, and a bit far removed from on-the-ground problems such as dodgy network connections or debugging memory leaks. Java developers have a (sometimes deserved) reputation for building expansive elegant frameworks using all the right design patterns without ever maturing into working, reliable code. In a recent article at Artima, design patterns author Erich Gamma described the case of some poorcoder getting frustrated because he’d only managed to cram 21 of the 23 design patterns in the GoF book into his code, and couldn’t find a place for the other two.

I’ve just been writing up an experience I had where design patterns really helped me out with a nitty-gritty, real-world problem involving leaky web browser implementation of the DOM. Javascript being javascript, there were several ways of doing all the little things that my program did - creating DOM elements, attaching logic to them, removing the elements from view when i’d done with them - and suddenly, a previously well-behved ajax app started gobbling up MB of memory, a couple of weeks before it was due to go live. I stepped back to look at all the ways of doing the little things in te code, and assigned names to each pattern. With names for them all, it was easy to run through the code switching between each permutation of patterns, and run a simple benchmarked stress test on each permutation. The results were pretty clear, with the worst permutations generating memory footprints some 150-times that of better-behaved runs. Lo and behold, our current codebase was using a badly-behaved combination of patterns in one or two key areas. Switching to a different combination of patterns, the real-world app’s hunger for memory swiftly abated, and we had a trouble-free deploy at the customer’s site.

The point is that the simple act of naming the patterns made all the difference. I could look at my code and pull out the information, and express it in the right way - ‘here I’m using pattern X and here and here variant Y’, and quickly get all the different parts of the codebase singing to the same tune. I might have managed to do so without design patterns, but I’d be far less likely to know when I was done with the fine-tuning, even if i did achieve consistency along the way.

1 Comment

  1. Patterns in the real world are definately a valuable thing however, I see them abused so much. Its like learning anything new I suppose, you have to apply it to actually get it logged in your memory banks. So you learn a pattern and quickly try to apply it to your project. The fact someone crammed 21 patterns into an application is downright scary!

    Comment by Jim Plush — July 8, 2005 @ 9:05 pm

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.

Powered by WordPress, Supported by SaveOnRefinance.com