views:

141

answers:

8

I was just going through Design Patterns Questions on Stackoverflow and everyone mentioned about GoF- Design Pattern book and Head First Design Patterns book to learn them.

My questions is how can we learn where and when can we apply particular Design Pattern.

Are there any books or online resources which imparts this knowledge or what steps we need to follow in making our decision of where and when we should use particular Design Patterns ?

+5  A: 

Refactor to patterns, rather than code to patterns is the general advice.

Finglas
+3  A: 

Experience is the best guide you'll have for this. But in general:

  • Don't apply a design pattern just for the sake of applying it; make sure that the problem really is what you think it is, and that you understand it at a level of depth necessary to apply a design pattern.

  • Code to solve the problem, not to apply a design pattern.

Although they're useful in the long run, it can often be very dangerous for beginners to read books like these, because they go from an empty toolbox to now having a single, enormous hammer -- and when all you have is a hammer, everything looks like a nail.

John Feminella
+1  A: 

I've found FluffyCat.com to be a very helpful source of design pattern examples. It provides both Java and PHP implementation examples of all the GoF patterns, as well as a description of how each pattern is supposed to work.

Kaleb Brasee
+1  A: 

The head first book is pretty good at explaining where and when to use patterns.

This is an article with one of the GoF writers:

http://www.artima.com/lejava/articles/gammadp.html

Don't try and force them

Scobal
A: 

Refactoring: Improving the design of existing code by Martin Fowler is a really good book. It shows existing conventional code and then shows why and how to refactor it. Many examples of how to identify code that should be refactored and how to take small incremental steps to do so.

Michael Valenty
+1  A: 

It's like knowing to fish with the right tools the first time... pretty hard if you never experienced before. Given some time and lectures, it will come by itself.

If you make a mistake by implementing a design pattern not suited for the job, you'll have a hard time doing it. But you'll be learning 2 of them (the one you should have took, and the one you taken) ;)

Mike

Mike Gleason jr Couturier
A: 

In my experience, having a good understanding of the design patterns will be enough to turn on a light in your head whenever you're facing a particular problem to which a a design pattern might be applicable. And as John said, with experience you will learn to apply the correct one.

Often times there is more than one pattern that will work fine. But you shouldn't have to work too hard to make a pattern fit a particular solution. It should kinda just be "obvious" after a while.

Also there are times when you can combine patterns or adapt them slightly to a particular problem. And it's not uncommon to see a new pattern and think "well I've kinda been doing that all along." Design patterns weren't invented per se, they were identified and named. That's not to say they are a strict recipe for building software.

Josh Einstein
A: 

I would recommend sourcemaking.com. It provides a general discussion on how and when to use the GoF design patterns. And how you can combine them for the best result.

Also Joshua Kerievsky's book Refactoring to patterns might be a valuable resource.

mrydengren