Tuesday, October 30, 2018

Design Patterns Don't Live in the Code

Two stick figures are conversing between their desks. One says "I've invented a new way to use the Strategy pattern. Look: these all implement the same interface. Each one can just call the next Strategy if it doesn't know how to do its job!

In my previous post, I wrote that design patterns don't live in the drawings. You can't just replicate a picture someone drew a bunch of times and get a good design.

A natural next conclusion is that they must live in the code. This is also not the case.

We can take the same thought experiment from the aforementioned post and reapply it to code. If you strip out any hints as to what pattern is in place, it can be hard to tell whether you are looking at the code for a State, Strategy, or Template (method) pattern.

I know this because I've seen dozens, if not hundreds of cases of someone putting the word "strategy" at the end of the name of a variable that was not used in a strategy pattern.

We need to look elsewhere for the essence of design patterns. Somewhere less implementation-y. Stay tuned.