Rethinking Design Patterns

The GOF book was the first pattern book I read, and I found it to be a terrible introduction to the world of patterns. I can (now) appreciate the patterns it contains and it’s role in creating a wider awareness of patterns in software, but it presents a very narrow perspective of what patterns are all about.

I agree with Parveen about The Timeless Way of Building by Christopher Alexander. It the book that gave me an “aha!” moment about the nature of patterns and is great if you want to understand the theory behind what makes them work patterns.

“A Pattern Language” is a catalog of architectural patterns based on the method described in Timeless Way i.e. it’s a demonstration of how to apply the theory in practice. It’s a great example of patterns and a pattern language in practice, but Timeless Way contains the real nuggets.

For a broader perspective, I found the Pattern Languages of program design series (PLoP) from hillside.net to be quite informative - they really opened my eyes to the different types of problems to which patterns can be applied, not just software design architecture.

Patterns aren’t things you implement (in the solution space). They are things you recognize (in the problem space).

I would certainly agree with everyone about the misuse of patterns. Which is why I think that reading “Object-Oriented Design Heuristics” by Arthur Riel is so helpful. While patterns tell you what to do, heuristics tell you why - or at least make you weight the factors for that decision.

Sorry, but I too have to disagree

When designing, patterns may seem cumbersome and overkill, but once your applications have grown considerably, patterns make your application more maintainable as they allow you to get a “Feel” of how they work, and before looking at the code, you often know what library and what chunk of code is broken, without even having to dive in or debug. I’ve even experienced opportunities where patterns, combined with naming conventions allow you to say: “I’m looking for this method name, in this class file, in this library or namespace”, or “This method is called this or the other, so it does exactly this, and nothing else”.

Granted, patterns aren’t the silver bullet. If you know beforehand to what extent your application or library will grow, then you should consider more of a rapid development approach, if you don’t, put in the extra effort.

For this one time I have to slightly disagree with you Jeff

Funny. This is the one time I find myself in total agreement with Jeff. Devotion to design patterns seems to be a sure indicator of programming mediocrity. The worst part of this is that an unthinking pattern applier often misses the one requirement detail that makes their chosen pattern totally inappropriate (without some kind of modification). Worse are the ones who like to rectify this issue by layering on yet another pattern…

The fact of the matter is that software development is hard and requires real thought. You can’t solve all problems with a finite set of some “n” cookie cutters.

Lest I come off as a total luddite, I rather like the refactoring book. There’s a fun book on “anti-patterns” that I liked a lot too.

Don’t apply patterns as dogma! Consider the patterns, understand the patterns and apply as necessary to solve a problem.

There is way much prose about this topic.

Patterns are a map, a guideline.
Code is the terrain, sometimes it’s rocky.
It’s nice to have a map.
Don’t always trust the map, trust the terrain instead.

That’s my haiku for design patterns

I think Alexander’s newest work “Nature of Order” not only sheds light on “A pattern language”, but also has the potential to have a better impact in the way we build software than “Design Patterns” has in general. If you have not read the series, I suggest you read it as it is a much more mature collection of ideas than “A pattern language” was. How and if it will impact the way we right software is yet to be seen. But fun to think about.

I know 2 things for sure:

  1. The more I learn the less I know. I become ever more cognizant of things I should know that I don’t have time to learn.
  2. This must be the most confusing time for programmers. OO is the proper way to develop systems. This is established by years of practice… no wait, maybe OO can encourage bloat and complexity, FP is way better… wait, who the heck knows? One things for sure, design patterns result in better code… wait, maybe they really only expose an underlying problem. Well, I do know that I should make code flexible and extensible… wait, that has been the source of many bloated, complex systems too.

Janitorial arts - it’s the only way to go.

T.E.D., what book do you mean? Is it this book?

http://www.amazon.com/AntiPatterns-Refactoring-Software-Architectures-Projects/dp/0471197130/

Design Patterns is, like a number of other books are, more like a combination dictionary and thesaurus, and largely ought to be used like one. I’d never try to write a novel without a thesaurus and/or dictionary handy, but I certainly wouldn’t expect them to instruct me on writing great prose.

It’s unfortunate that so few people understand the patterns (indeed, Patterns) that they’re working with, but taking away their “instruction manual” will not force them to rethink their working habits; They’ll just find another manual.

What I find strange is that Jeff and others like him will work to convince us that we live in an age of infinte monkeys, but will then turn around and blame Shakespeare for their existance.

what book do you mean? Is it this book?

a href="http://www.amazon.com/AntiPatterns-Refactoring-Software-Architectures-Projects/dp/0471197130/"http://www.amazon.com/AntiPatterns-Refactoring-Software-Architectures-Projects/dp/0471197130//a

Yup, that’s the one.

Misuse of patterns - and posters here using words like “dogma” and “commandments” - is not the fault of the patterns but the practitioners.

The GoF and other pattern books present a handful of solutions to a handful of problems. To appreciate them and use them well, or to make up your own solutions to new problems, you really need to understand the principals and values that led somebody to think there was a problem and why they think the pattern solution is good. If you’re focused on separation of concerns, isolating sites of change, managing dependencies and so on, the patterns give some solid hints and examples. You can judge whether the problem is likely to cause you so much pain that you need the solution … or not. GoF goes down much better with something like Robert Martin’s Patterns, Principals and Practices. Without that understanding, one can wind up pushing “dogma” and “commandments” without reason.

I use design patterns to 1) solve particular problems in the code “trenches” and 2) help myself design code by the concept of orthogonality DRY Principle (refer to The Pragmatic Programmer). I don’t do it as an exercise of faith. Nor do I do it without trying other solutions.

Also, the I agree with the comment about junior programmers using design patterns. Let the JPs code JP stuff.

Another thought: Perhaps the term “Patterns” is incorrect here. Perhaps “Constructs” would be better, because “patterns” has the connotation of being mindlessly, guilelessly repetitive and reactive. Maybe “Templates” would be even better.

I read a Pattern language cover to cover. It is a very worthwhile read.

However, more generally: I find it is always worthwhile to go to the source inspiration for any ‘classic’ text. Norman’s The Design of Everyday Things has a fantasic bibiography in the back.

Jeff and others like him will work to convince us that we live in an age of infinte monkeys

We do, in fact, live in an age of infinite monkeys:
http://www.codinghorror.com/blog/archives/000864.html

But not all monkeys are created equal.

I find it is always worthwhile to go to the source inspiration for any ‘classic’ text. Norman’s The Design of Everyday Things has a fantasic bibiography in the back

Shh! You’re giving away all my blogging secrets! :slight_smile:

Jeff wrote:
But I have two specific issues with the book: 1. Design patterns are a form of complexity. As with all complexity, I’d rather see developers focus on simpler solutions before going straight to a complex recipe of design patterns.

Design patterns are only a form of complexity when viewed at a code level. When viewed in the abstract, they are a source of simplicity because they provide the ability to understand the forest without knowing the details of every tree. The problem with “simpler [code] solutions” is that the code is the only way to understand what is going on and that breaks communication of ideas because those ideas can only be communicated at the code level. This makes it harder for the rest of the team, QA, Program/Product Management, Other Development Teams, … to understand what is going on inside the box.

It is true that rabid adherence to every gory detail of the patterns in Design Patterns can lead to overly complex code but, as others have said, these patterns are to be adjusted to the needs of the task at hand. If the full power of the Abstract Factory or Factory Method is not needed then simplify them down to the core idea which is “place construction code in a separate method so it doesn’t clutter up the mainline logic”. If you aren’t bridging two different inheritance hierarchies, don’t use the bridge. If the target API is close to the needs of your project, use a facade to wrap it - don’t bother with the adapter.

In arguing for his point, Jeff shows that his argument wears no clothes (to paraphrase an earlier point). Jeff and another individual shared these thoughts:
Who are these architects that I always hear about that blindly apply the Gospel of the Design Patterns instead of thinking? I’ve not seen them, and the GoF book doesn’t seem to advocate it as far as I can tell.
Indeed. Ever read “The Bible”, or “The Quran”? Strangely, those books don’t advocate a lot of the things you’ll see people doing in the name of them, either.

The Bible and The Quran are often used as justification for strange arguments - that doesn’t make those arguments right. Those who apply the gospel of Design Patterns instead of thinking are not right either. In condemning the book because it is a source of unnecessary complexity, Jeff seems to be saying that because some zealots misinterpret its intent, the book is bad. But his counterargument above seems to imply that, despite being misinterpreted by some zealots, The Bible and The Quran are not bad. Was the cake tasty Jeff? Do you still have it around? You can’t have it both ways.

Now perhaps I am misinterpreting Jeff’s intent since he doesn’t seem to disagree with the idea of patterns, just the overreliance of some in the software industry on this one book instead of the ideas that were embraced by Mr. Alexander years ago.

My concern with Jeff’s post here is more that it will be used as ammunition for those who don’t want to design, who don’t want to worry about future changes that are known to be coming down the requirements pipe, those who will argue that a “simpler solution” is better. I worked with people like that. One very senior developer (senior in years, not skill in my humble opinion) argued that design patterns would do us no good - that you could not come up with a solution as elegant as the one he came up with without playing with the code for a while. It turns out that the elegant solution he came up with, after six months of uncontrolled coding because managers were afraid to interfere with “the great one” (eyes roll, sarcasm spews from mouth), was simply the interpreter pattern and I showed it to him in a book in 5 minutes once he finally was willing to discuss his grandiose project. All this work of his was justified by the argument of “simpler solutions”. Other work of his and his team that was justified by “simpler solutions” included a 40000 line class, some 2000 line methods in which the set of variables that tracked the state of the operation changed as you proceeded through the code, methods with cyclomatic complexity in excess of 60, etc. Because their code was “simple” it could not be discussed outside of the code - and therefore there were no sanity checks to stop these debacles.

Design patterns is an excellent book and should be on every developers shelf. It enhances team communication, it provides a source of inspiration when one is confronted by a tough problem, it allows developers to avoid six month ratholes because they are convinced nobody else can help them or they are too proud or arrogant to ask around. Because a few zealots overuse the book and religiously adhere to what it says does not mean the book does not provide a valuable service.

We all know actual implementation tends to deviate somewhat from the initial design generally because of exactly what Jeff is pointing out here - in a dynamic environment there’s no way one designer can anticipate all the requirements of a system.

Design patterns are useful when you need to communicate the design of something as opposed to its implementation. If I tell someone “xxx is supposed to act as a Facade here” or “yyy is a Factory”, we all have a more precise idea of the subject. Whether or not xxx actually is a Facade or yyy actually is a factory is a subjective matter.

Personally I’m looking forward to what I think is the next big thing here: a more expressive “Design Goal” methodology capable of evaluating the cost and benefit of applying different design patterns to a given problem.

If you are designing software in an OO way you are using at least some of the GoF Design Patterns. If you are using the GoF Design Patterns you are doing at least a little OO design.

The GoF Design Patterns were not an invention but rather a compilation of simple OO design practices which had been in use for years and a vocabulary to be used for them. Some patterns may be better than others but the book states pro’s and con’s so I don’t see that as a problem.

Jeff,

You know, when I first started learning design patterns, i was a little turned off for exactly the reasons you mentioned: over-complicated, cookie cutter solutions that like any piece of technology, they can and are frequently abused.

Collection and iterator patterns are almost useless now, because they are built into java and .net. The same is true of many “Enterprise Architecture” patterns, because they are built into one framework or another, if not the language itself. Truly, many times design patterns grow into a framework or even a language feature. This isn’t a bad thing at all, and i think it shows that the concepts behind patterns are inherently useful.

That being said, sometimes i really DO need something like a singleton pattern. It would be nice if the language had a built-in way to make one, although many times its a variation and not exactly what the GOF or “Head First” books show in their source code. For example, in asp.net, i need a singleton where the object is global to the current http request, but not global to the whole app domain. I can reuse the concept of the singleton with a slight variation. I can also tell another developer: “This is a variation the singleton pattern” and they’ll understand. Which is the whole point of design patterns. So I still think they are useful, but your points are definintely well taken.

I’m curious though, do you never use any design patterns yourself?

Craig