When to use which design pattern?

Martijn picture Martijn · Feb 18, 2011 · Viewed 125.1k times · Source

I like design patterns very much, but I find it difficult to see when I can apply one. I have read a lot of websites where design patterns are explained. I do understand the most of them, but I find it difficult to recognize a pattern in my own situations.

So, that is why I ask this question. Are there any guidelines / alarm bells when to use which design pattern.

For example, if you are doing a switch statement to determine which object you need to create, you probably want to use the factory design pattern. So the switch statement in this case is a 'alarm bell' to use the Factory pattern.

So, do you know more 'alarm bells' to determine a design pattern?

Answer

Joep Killaars picture Joep Killaars · Feb 18, 2011

Usually the process is the other way around. Do not go looking for situations where to use design patterns, look for code that can be optimized. When you have code that you think is not structured correctly. try to find a design pattern that will solve the problem.

Design patterns are meant to help you solve structural problems, do not go design your application just to be able to use design patterns.