NFJS Day 2 Session 1: Design Paterns Reconsidered

Today I decided to be a bit pickier with the sessions I attended. I was going to go to Scott Davis’ presentation on Real World JSON, but decided against it since I already use JSON extensively and know how to serve it up from the serverside (as well as tools to nicely serialize/unserialize domain objects as JSON). Ditto on Ted Neward’s presentation on annotations… we’ve been using them a bit for runtime level behavior. So I decided to attend Alex Miller‘s session on Design Patterns Reconsidered. Good choice.

Basically he covered four well known patterns: Singleton, Template Method, Visitor, and Proxy, looking at their pitfalls and possible ways to avoid those pitfalls. The first pattern explored was singleton.

Singleton in my opinion was a good pattern to start with… its the easiest and most widely used, which why its an easy target for abuse. The biggest problem? Singleton introduces a hidden coupling in the way it’s used. How am I supposed to test this class if it uses a Singleton somewhere that hits the database? ARGHHHHH!!!!!

The solution Alex presented was interesting… extract an interface from your Singleton and specify the user of the Singleton as a “Victim” and provide access to the Singleton via Dependency Injection using the interface. He tied this up by using Guice to show how effortless it can be. But his final comment on it? Just don’t use Singleton. Might have been a joke, but that’s something I agree with… 90% of the time when you use Singleton it’s only because you cant figure out a better way to get access to a resource throughout your application and you soon find yourself bitten by the choice of using it later.

I’ll post more on Template Method and Visitor later… but overall it was a very good presentation and drives the point that we really need to question how we use Design Patterns… we need to identify and get around the pitfalls that can arise when we use them. ;)

You can leave a response, or trackback from your own site.

Facebook comments:

One Response to “NFJS Day 2 Session 1: Design Paterns Reconsidered”

  1. Alex Miller says:

    Thanks for the comments! Nice to meet you, too. I’d certainly welcome any other feedback you have on the talk (good or bad, in public or private).

    And that was no joke about the singleton. :)

Leave a Reply

Subscribe to RSS Feed Follow me on Twitter!