The Purpose of Design Patterns

This is kind of a response to “If I hear”design pattern” one more time, I’ll go mad”, but I confess I did not read the original deeply.

The purpose of Design Patterns (as I understand the book and the concept) is not to provide you with a bunch of ready-made and formulaic solutions to unthinkingly apply. It is to show you examples of how to approach problem-solving in an object-oriented manner, in the hopes that you will internalize the process of discovering abstractions. In almost every case, a pattern is not a certain object or interface. It is a style of interaction between two or more objects that the pattern embodies.

Let’s take the command pattern. If your commands are also mementos, you can use it to implement an event-sourced database, or write-ahead log. If your commands have an undo method, you can implement an undo/redo stack. Commands are reified; perhaps they can be passed around the network or stored, perhaps they can be added to queues to be executed in batch or in threads. The question is, what else do you need the command to do, besides be executable? If there isn’t anything, then a command really is no better than a function, and obviously you get no benefit from creating the abstraction.

Similarly, a singleton can be an improvement over global state because at least access to that state is mediated by the methods of the singleton itself. If I had some element in my system that was not threadsafe, but the rest of the program was organized using threads, I might make it a singleton just to ensure that locking and unlocking happen correctly. I don’t usually find I have to use singletons, but again, the pattern exists not to be blindly copied, but to help you think about how to approach a problem in an object-oriented fashion. And also, it is the interaction between other systems and the singleton that might provide the real value; for instance, being able to mock the singleton in testing is probably easier than mocking a tremendous amount of global state.

About abstract factories, I would say, much like monads and monoids and elements from category theory, it’s much easier to understand in terms of a few worked examples. Just because something is hard to understand in its abstract form doesn’t mean that it is hard to understand. But also, maybe more to the deeper problem, sometimes things that are hard to understand are still good and worth understanding.


Date
September 8, 2025