Explain Design Patterns Like I'm Five

Did you find this post useful? Show some love!

Hmmm, it's bit tough to come up with a truly eli5 example for this, but I think the basic idea is reasonably simple: A design pattern is a structure of code that you can apply to many different problems.

I don't think it's generally thought of that way, but to me, even something as simple as a humble for loop is a kind of design pattern. It's used to loop over some kind of iterable and then inside the loop you execute some piece of logic. The for loop is a re-usable structure of code: It can be used for all kinds of different applications. Therefore it's a simple design pattern!

In the 90s, a book called Design Patterns, Elements of Reusable Object-Oriented Software was published. This book became quite influential in the object-oriented programming community. In this book, each pattern is basically a piece of boilerplate code that can be used to reduce duplication and increase flexibility when solving a type of problem. You can think of this pattern code as a kind of harness or scaffolding: You start with this pattern code and then you fill in the actual logic for your specific situation. The Design Patterns book has ideas that are quite common in programming today. For example it describes the proxy/decorator pattern that's used quite a bit in many contexts these days.

Often when someone refers to design patterns, they're either talking specifically about one of the patterns in this book, or they could be talking about patterns that other authors defined later on. For example in 2002, Martin Fowler published another influential book of design patterns around enterprise programming called Patterns of Enterprise Application Architecture. Among other things, this book spends quite a bit of time on design patterns for mapping an object-oriented application to a relational database. You can see some of these patterns mentioned specifically in some ORMs (object-relational mappers), like active record in the Rails framework, or data mapper in SQLAlchemy. That means design patterns can also be useful as a shorthand for communication among developers.

On the negative side, design patterns can become a temptation to over-design one's code, so that's always something to keep in mind. The basic trade-off of design patterns is that they generally give you more flexibility at the cost of increasing the level of abstraction/indirection. It's like anything else: Having a tool in your toolbox is always a good thing, but it doesn't mean you have to apply that tool to every problem you encounter.

When humanity first started making buildings, they were usually made out of sticks, grasses, mud, etc. This worked well for a long time, it gave people some kind of protection from their environment; helps keep out bugs and predators, gives protection from rain, provides a feeling of general security, etc. In fact, in many places around the world, indigenous peoples still build their houses this way, and that's perfectly OK.

Fast-forward to when larger civilizations started to emerge, and you'll find that the old mud huts and tents just aren't going to work. You've got too many people living in too small an area, and while it may be easy to build or rebuild hut for a single family, it's much more difficult to keep an entire city of huts in good order. And since huts are built right on the ground, a city of huts would grow to cover a very large area, making for an inefficient use of the available area, or limiting the population that could live in a given area. The answer, of course, is to build upward rather than outward, using timbers and large stones to construct many-storied buildings that last a longer time. It may be more difficult to build initially, but the benefits in the long-term are definitely worth it.

And going even further, today's skyscrapers are made out of steel and concrete, not wood and stone, which makes them even stronger and more resilient, able to climb higher and support more people than ever before. While it may have taken thousands of years of engineering technology to get to this point, now that we're here it is really easy to go and build another skyscraper, because all the hard stuff has already been figured out by someone else. Now that we have the patterns figured out for how to build a good skyscraper, we just need to go build it.

And this is the purpose of design patterns, like MVC, MVVM, MVP, Clean/Onion Architecture, and so many others. They are by no-means necessary for small projects, and when working with a small codebase, attempting to follow a design pattern can be quite cumbersome. You probably don't need to build a skyscraper for a village of 20 people, and even a house of stone and timbers may be too much. However, if you are expecting your village (project) to grow significantly, it might be worth it to put a design pattern in place, because it is a whole lot easier to build a skyscraper from a clean foundation than it is having to start with a patch of dirt littered with mud huts. Usually, in this case, it is actually easier to just throw away all the old huts, level the ground as flat as possible, and start over with the intention of strictly following a design pattern.

Design patterns are standard recpies by programming chefs which help you writing tasty applications.


Hey there, we see you aren't signed in. (Yes you, the reader. This is a fake comment.)

Please consider creating an account on dev.to. It literally takes a few seconds and we'd appreciate the support so much. ❤️

Plus, no fake comments when you're signed in. 🙃

A design pattern is the description of the same solution multiple persons used to solve the same problem, so that further persons don't have to come up with this solution on their own.

Classic DEV Post from Apr 29

The (Im)Practical Continuous Delivery

Don't let the anti-patterns grind you down.

Follow @nikoheikkila to see more of their posts in your feed.
dev.to is now open source!
View Announcement Post View GitHub Repo
Member since Jun 12, 2018
Trending on dev.to
Timsort: Fastest sorting algorithm for real world problems.
#python #java #programming #beginners
Code management with Git and Github
#git #beginners #learn #tips
Explain Hashing + salting Like I'm Five
#explainlikeimfive #webdev
How Web APIs work?
#beginners #webapi #javascript #html
Exposing localhost server to the internet in one minute
#webdev #beginners #testing #devtips
How to Build Your Own Html editor⁉
#html #javascript #css #beginners
To Use Google Cloud Function As Google Chrome
#beginners #javascript #node #testing
Understanding microservices 101. A frontend developers view.
#devops #developing #beginners #webdev