9

Wikipedia has a good summary about the various design patterns. Which ones have you used at most in practice and what are your experiences? When should one go for a pattern and when not ?

Edit: A cool C# based link with practical DP examples ( tnx to Mladen Mihajlovic)

11 Answers 11

18

You should definitely avoid "Pattern Driven Design" where you try to cram as many patterns as possible into your classes. I find it useful to know the most important design patterns and when I come across a new design problem I ask myself the question "Do I know a pattern that can be useful here?". If I do then I try to use it; if not then I don't (I also sometimes look them up for I don't know every pattern by heart).

Design patterns can really help you to improve your code but used wrongly they can also really mess things up.

1
  • 2
    +1 - See a design problem then see what patterns could help solve it. Easier to see problem areas if you are familiar with what different patterns try to solve. Apr 23, 2009 at 6:22
6

Re that people who may not have experience with all the patterns will be able to understand what your code is doing

I think it is the other way around. Patterns is a good way of communicating a design so if people don't understand factor, observer, decorators then they have a problem...

alt text
(source: headfirstlabs.com)
Check out "Head First: Design Patterns" they also have some forums regarding the book and a design meditation.

Good site for patterns http://www.dofactory.com/Patterns/PatternObserver.aspx

1
  • +1 for Head First: Design Patterns reference
    – Ikke
    Apr 23, 2009 at 7:21
4

My experience is that most programmers who talk about DP do not understand one bit of the forces etc. around it! To quote Yegge in his great Singleton Considered Stupid :

If they claim expertise at Design Patterns, and they can ONLY name the Singleton pattern, then they will ONLY work at some other company.

4

I think the best rule to try to follow, is KISS. Patterns are useful in helping your software be more maintainable and understandable, but ultimately KISS to ensure that people who may not have experience with all the patterns will be able to understand what your code is doing.

2

Well the one pattern which I use for most of my projects is MVC. It had proven to be perfect for my type of programming. I've also used a combination of most of the patterns that are available considering the amount of programming I actually do. Don't forget that patterns are "solutions to common problems" that we experience in programming.

Here's a cool link for reading about patterns with implementations (in C# and VB.NET).

2

As Benedikt already wrote, as a rule of thumb, only use patterns to solve problems, don't change your problem to make it fit to a pattern. ;-)

I use factories and builders a lot. Command, Observer and Visitor patterns on the behavioural side.

They naturally fit in most problems I am encountering in my daily development life.

2

Creational patterns are straight forward and we can easily conclude when to use a specific pattern. I have used all 5 patterns in my projects depending on the requirement.

Strategy pattern should be used when you have different implementations for a certain feature, which can be changed at run time very easily - Like changing the configuration to modify existing behaviour and move to different implementation

Decorator pattern is also straight forward - you can add behaviour at run time without touching existing class and add new classes

Chain of responsibility, State, Adapter patterns are also very useful in real time.

Bridge,Proxy & Facade - I have seen very few scenarios in my projects.

Among remaining patterns: Memento, Visitor, Mediator - I did not find any use cases so far and I do not like them compared to other patterns

1

Factories and Decorators are used a lot in both Java and .Net frameworks.

1

We try to identify where well known patterns can be applied. Normally we do not write code for them, we just take it from different places. Tipically we use existing code for Singleton, Proxy, State, etc.

take a look at Gamma's Book "Design Pattern"

The "Java Camp Code" has also code for common patterns (http://www.javacamp.org/designPattern/)

also the Wikipedia has code for patterns

Luis

1

Answering the title: "What practical experiences...?", I'd say that trying out DP's has taught me how OO really works, especially the power of interfaces.

Answering the rest of the posts:

My favourite patterns are:

  • MVP (Passive View for testing)
  • Strategy -- in combination with TDD,
  • Template method -- for extracting common behaviour into superclasses.

After having used a variety of patterns, I notice that I see them emerge "spontaneously" more and more as answers to some problems.

I agree with others that you should not use patterns for the sake of using patterns -- you'll get stuck trying to shape the problem to fit the solution, which is often problematic.

1
  • Hmm. Make that messages, instead of interfaces. Take a look at smalltalk to see how OO really works :) Apr 23, 2009 at 8:57
1

From my experience, some design patterns are more suitable for some type of programming than others e.g, In games programming, the State Machine pattern is what I use alot. MVC and the Template design patterns are also very useful.

Not the answer you're looking for? Browse other questions tagged or ask your own question.