views:

453

answers:

12

If, in future, you were only able to use a single one of the canonical patterns defined in the Gang of Four book, which one would you choose and for what reason?

+3  A: 

Abstract Factory Pattern or Template Method Pattern... but mostly Abstract Factory Pattern. They have just been extremely useful on a lot of situations.

I specially liked the way Abstract Factory allows you to prepare for situations where you know your program may change in the future by following the same rules in a different way.

Is mostly like the Abstract Class allows you to define specs "inside" the code, restrain the code to follow that specs while the factory allows you to "choose" among those specs. Simply beautiful.

Jorge Córdoba
+1  A: 

If I had to choose one, I'd go with the M.C.Escher picture on the front cover, to put on the wall. ;)

Peter Boughton
+19  A: 

Not a design pattern, but to me this principle was definitely the most influential on my programming style of the whole book:

Favor object composition over class inheritance.

Combined with the other principle "Program to an interface, not an implementation", most design patterns follow naturally.

Choosing one pattern as the most important is in my opinion impossible. They all have their purpose and complement each other. Its like asking if a hammer or a screwdriver is more useful. It depends on the problem, sometimes you have a nail and sometimes a screw.

martinus
This doesn't really answer the question, but it's an excellent point and I have to agree with you.
Stu Mackellar
+1  A: 

This is a difficult question because, I have found that when I was learning patterns, I was implementing a lot of systems in a very simillar manner to many of the design patterns (particulary the more fundamental ones such as the creational patterns, or state, and strategy and template).

Now that I know about the GoF, I cant seperate out in my mind what patterns I wouldnt have discovered naturaly, and to me that is what makes a good pattern. A good pattern is one that is so natural that you write code using the pattern because its a good solution.

Patterns just give us a way to talk about our strategies for composing solutions in a common dialect. I know I didn't answer your question so I applogize.

JoshBerke
A: 

I would have to say the Singleton Pattern. How many times you need that one instance of a class that you use over and over again! It may be considered an Anti-Pattern but it's one that I use frequently.

Again the Abstract Factory Pattern is also one that I use all the time and the up front development has saved me hours of re-factoring later on when we need to implement a new set of requirements.

Niels Hansen
singleton is actually an antipattern.
martinus
It's in all the design Pattern books I've read.http://en.wikipedia.org/wiki/Singleton_patternFirst sentenceIn software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object.
Niels Hansen
It's still an anti-pattern. I still have not found one instance where a Singleton has helped. Hindered? Sure all the time, helped? Not once.
Quibblesome
bart
+1  A: 

I like the Visitor pattern. More than once, I had a design problem and couldn't really solve it, until I finally found that the Visitor pattern was the answer.

You can add any functionality to a class without having to change it! Just put an accept() function in there.

+2  A: 

I like the state pattern. It's good for modeling, well, state. For a game project I'm writing, I use state hierarchies to keep track of game state. It's simple but very flexible.

Matt Olenik
+2  A: 

I'd say the Decorator pattern. Mainly because it's used so heavily in frameworks already.

For example, if you've ever written code like this:

FileStream file = new FileStream("file.txt");
GZipStream compression = new GZipStream(file);
TextReader reader = new TextReader(compression);
reader.ReadAll();

Then you've used the Decorator pattern.

Cameron MacFarland
I use the decorator pattern ALL the time. It really helps to separate out functionality while keeping everything "the same".
Quibblesome
I'm curious to know why someone voted this down...
Cameron MacFarland
+3  A: 

The observer pattern

Kire Haglin
A: 

The Behavioral Patterns:

  • State when my business logic is distribued;
  • Strategy when the context change the algorithm;
  • Iterator when I read a collection of objects;
alepuzio
+1  A: 

Listener or Observer pattern.

Can't imagine my life polling for stuff to check if it happened.

Others:

  1. Adaptor (this is very useful if you don't want to touch the code that works but you don't have the time to understand how, or you just don't know)
  2. State
  3. Strategy
artknish
A: 

One I can't live without is Iterator. I use it daily. I can hardly imagine life without it :-) My second most used is Decorator/Wrapper.

Peter Štibraný