views:

564

answers:

10

What are the design patterns that every developer must know?

I'm interested in the context of Java web developers working with Spring & Hibernate. I have often heard that good knowledge in design patterns is essential for working with those frameworks. Can anyone list the specifics?

For example, I know that understanding abstract factory & factory pattern, singleton pattern etc is absolutely essential. I'm looking for a comprehensive list.

A: 

Singleton - Singletons apparently can and should be used for everything

Martin Beckett
used for everything? - you're joking right?
Mitch Wheat
If only I was ;-)
Martin Beckett
Yes, irony is lost on some people.
Greg Hewgill
+6  A: 

Model–view–controller just has to be on the list, Spring has an MVC framework:

http://en.wikipedia.org/wiki/Model–view–controller

karim79
actually Spring has a MVC module (Spring MVC), but it's many many many other things besides that as well
matt b
@matt b - My mistake, I guess I just assumed so as so many of the question on SO refer to 'Spring MVC'
karim79
+1 my favorite design pattern.
Firstthumb
+3  A: 

Everybody should know about Singleton, but also when not to use it! It's currently the source of much pain for me on a project at work.

Singletons make the code hard to understand and follow, and make writing unit tests much more difficult. I like the blog post Singletons are Pathological Liars.

Greg Hewgill
+1 for the "Singletons are Pathological Liars"
Mitch Wheat
Greg - do you get the feeling that many people here don't get irony?
Martin Beckett
+4  A: 

Most design patterns are pretty obvious--you already know and use them if you've been programming a few years.

The biggest advantage I've found to design patterns is sharing a common set of names. If someone says "Callback" that can mean quite a few things, but if someone says "Listener Pattern" that means a more specific set of calls and implies a higher level relationship between objects.

So in essence, read through a good design patterns book, get an idea of the name of each pattern, spend some time understanding any you don't know, and you're good to go.

I wouldn't completely ignore any of them--they are all good to have seen. You should be able to recognize a situation that might benefit from a specific pattern and know where to look to find out more about it.

Bill K
+7  A: 

Inversion of Control

If you are ever going to design decoupled systems, you will need to know how to properly link dependencies between classes.

Command Pattern and Variants

In Java in particular, it is essential to learn how to pass a piece of functionality to another method as an object because of the lack of closures and function pointers in the language.

Factory Pattern

Factories are ubiquitous in Java frameworks and it is essential to learn why and when to use the factory pattern.

Singleton (pattern and anti-pattern)

Learning how to use the singleton pattern responsibly is very helpful for understanding the pitfalls in other people's code you may be reading.

Overall, learning the why with regards to patterns is much more important the the how. Knowing when not to apply a pattern is just as important as knowing when to.

Elijah
+2  A: 

I would recommend you get and read the Design Patterns book, since it gives you the vocabulary.

Thorbjørn Ravn Andersen
+2  A: 

I recommend you to read Head First Design Patterns book. This is a well written book about all commons and useful patterns.

stefano
+1  A: 

Hibernate? Then the Unit Of Work is a must http://martinfowler.com/eaaCatalog/unitOfWork.html

Composite, it's present in the JUnit framework. (Test-TestCase-TestSuite)

The Adapter, Builder, Command, Template Method and Strategy patterns are easy and often usable in practice.

The State pattern also helped me to clean up mess in inherited source codes.

+1  A: 

This would be a comment to Greg Hewgill's reference to "Singletons Are Pathological Liars", but I can't make comments yet.

That article makes a convincing case, but his ire is misdirected. As several commenters on his blog noted, his problem is really global state. His code fix could still be making use of singletons, and still gain the exact increase in clarity and testability.

Re-read the article. He's not bothered that OfflineQueue needs an initialized Database instance, nor that CreditCardProcessor needs an initialized OfflineQueue. He's bothered that those dependencies aren't visible, which causes issues with maintainability and testability.

His problem is with secret global state (does this make me sound like a conspiracy theorist?).

However, he's (imo) misinterpreting that secret global state as being the fault of singletons.

That doesn't mean I'm in favor of singletons where they're not necessary - certainly, they have drawbacks (including the obvious thread contention bottleneck possibility). But I prefer to be clear about what practices I'm eschewing.

Incidentally, I'd go further in my refactoring - based on the class names, I'd assert in a code review that CreditCardProcessor should, well, process the charges, so instead of his:

    card.charge(cardProcessor, 100);

I'd have this, instead:

    cardProcessor.chargeCard (card, 100);

(and yes, I replaced his variable names c and ccp with names I considered more readable)

CPerkins
+1  A: 

But don't forget the fundamentals :)

Interviewing Java Developers With Tears in My Eyes http://java.sys-con.com/node/1040135

programmernovice
Shaw