Monday, October 29, 2018

Design Patterns Aren't the Drawings

An easy, and very forgivable mistake is to think that the drawings in the original book, Design Patterns, are the essence of the patterns.

One reason someone might make the mistake is that the drawings proliferate like dandelions.

Yet, the drawings are just vehicles.

A drawing may help me communicate to someone the essence of a design pattern. In fact, several drawings are almost always required to do so.

That doesn't mean we should let a drawing confine us to a particular design, just because it was used to explain an example of that kind of design at some point in the past.

I'll discuss design patterns more in future entries but I'll leave you with a puzzle.

The image at the bottom of this entry depicts three the implementation of three patterns, with all the names removed. There's a State, Strategy, and Template method pattern in it. The Template method is implemented the way I prefer.

Can you guess which drawing matches which pattern?

Three identical class diagrams, each of which have one class holding a reference to another and the dependency having two inheritors.
Well... which is which?