HACKER Q&A
📣 null_bomb

How to Learn Design Patterns?


1. How do I learn different design patterns?

2. How do I know when to use one?

3. How to evaluate and use them when developing a feature?


  👤 austin-cheney Accepted Answer ✓
Don’t.

Memorizing patterns is so cringe. Instead just learn to program, which means writing original software that solves a given problem as efficiently as possible.

There is more to programming literacy than just syntax. Writing software is a form of writing. Nobody memorizes English patterns when tasked to write a paper, because it demonstrates just how bad and cliche they are as an author. The same is true for programming.


👤 brtv
IMHO the best book on design patterns is: https://gameprogrammingpatterns.com/ Examples are in C++ but easy enough to understand even if you're not familiar with C++.

👤 leke
I've tried to do this for years now and I think it's not possible. I've heard that the people that use design patterns correctly end up discovering the pattern after they've intuitively implemented it already.

👤 aristofun
Please don’t! Even if some smart looking egg head convince you… just don’t!

We have enough shitty over engineered software in the world already.


👤 dingosity
1. I'm not the biggest fan of the Gang of Four book, but it's a reasonable place to start, especially if you're doing C++, Java or C#. But IMHO, the best way to learn design patterns is to examine code which uses one of the patterns. In the mid-90s I was working on a big Java project and one of my co-workers had boarded the design patterns train. He went out of his way to code in a way that was very obviously pattern-oriented. I did have to do some reverse work to try to figure out how particular patterns were better than other patterns or better than no pattern at all, but it absolutely made it easier for us to talk about the code.

Sadly, I think there's no substitute for "examine other people's code." Software documentation has fallen out of style as the young kids now believe every line of code they generate is a "golden pearl of wisdom, exquisitely obvious to all but the most dense. Oh? You don't understand it? You must be dense."

2. How do you know when to use a loop? Or an if...then construction? It might be hard to remember, but there was a time when you didn't know what a for() loop was. Then maybe you watched some videos or read some books and it made sense and you wrote a bunch of code with loops and then it was second nature, subsumed into the grey matter behind your eyeballs. I think the same thing happens with design patterns, but they're "bigger" than simple control structures, so you need a bit more training.

Also, don't be afraid to use only a few of them. They're supposed to help you, not make you feel like you're an idiot 'cause you haven't encountered a situation where you can use them all. They're not Pokemon. You don't have to get them all.

3. Sadly, I think it's just experience. Again. Read other people's code. Read the comments and issues to see if using the pattern made it easier or harder for other people.

Also... I was just looking at this and I think Dick Gabriel, as usual, has cogent comments to offer: http://wiki.c2.com/?PatternsAreNotTheLesserOfTwoEvils


👤 in9
In the wagon of 'read other peoples code', do check out https://refactoring.guru/design-patterns

I like how they translate to many languages, and how they build the notion of a pattern with simple example and also have a slightly more in depth example as well.

But do try to stay away from those.


👤 revskill
Learn core design patterns only:

- Observer patterns => This is your AHA moments !

- Iterator patterns => So that you can use (for item of your_collection) { }

And the mother of all clean code: Monad.

That's it, you hardly need more than those.