DEV Community

Cover image for Design Patterns: What They Are and Why They Matter
Pavan Sai
Pavan Sai

Posted on • Originally published at Medium

Design Patterns: What They Are and Why They Matter

I got asked about OOP in a conversation with a senior dev. I answered okay-ish. Then they followed up - "Do you know design patterns?"

I didn't. Not really.

That one question sent me down a rabbit hole that genuinely changed how I think about writing code. This article is what I wish I had before I started.


So what even are design patterns?

They're not libraries. They're not frameworks. You can't npm install them.

Design patterns are reusable solutions to problems that keep showing up in software design. Think of them as blueprints - you don't copy-paste them into your code, you adapt the idea to fit your situation.

Here's a distinction that helped me early on:

Algorithm = a set of steps to achieve something. Step by step, concrete, follow this and you get that output.

Pattern = a higher-level description of a solution. It tells you what to do, not exactly how.

That's an important difference. Patterns operate at the design level, not the implementation level.


What's actually inside a pattern?

Every pattern comes with four things - Intent (the problem it solves), Motivation (deeper explanation of why this solution works), Structure (a diagram showing classes and their relations), and a Code example so you can see it in action.

Refactoring.guru does this really well if you want to go deeper on any specific one.

The three types - and what they're actually for

Patterns are grouped by what they do. Once you understand the grouping, the whole thing becomes way less overwhelming.

Creational Patterns

These are about how objects are created.

The default way - just calling new SomeClass() everywhere - works fine until it doesn't. When your codebase grows, hardcoding object creation becomes a nightmare to maintain and extend. Creational patterns give you more control and flexibility over that process.

The main ones: Factory Method, Abstract Factory, Builder, Prototype, Singleton

Structural Patterns

These are about how objects and classes fit together.

You're assembling things into larger structures. The goal is flexibility and efficiency - making sure that when you add something new or change something, the whole thing doesn't fall apart.

The main ones: Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy

Behavioral Patterns

These are about how objects communicate and share responsibility.

Who does what? Who tells who? When something happens, which object handles it and which one delegates? Behavioral patterns give you clean answers to these questions.

The main ones: Chain of Responsibility, Command, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor


Why should you actually care?

Here's the real answer - not the textbook one.

When you're early in your career, you write code that works. That's the goal. Ship it, it runs, done. But as you start reading other people's code, joining bigger codebases, doing code reviews - you start noticing something. Some code is just easier to read, change, and extend than other code. And a big part of why is that experienced devs are - sometimes without even naming it - applying these patterns.

Design patterns are essentially shared vocabulary. When a senior dev says "let's use an Observer here" or "this smells like it needs a Factory" - they're communicating a whole solution in two words. You either get it or you don't.
Now you will.

Beyond communication, patterns also save you from reinventing solutions to problems that have already been solved. Someone smarter than both of us already figured out the clean way to handle object creation across a large system, or how to make two incompatible interfaces work together. That knowledge is sitting there in these patterns. Use it.

One last thing

There's also a classification by scale - Idioms at the low end (language-specific, only applicable to one programming language) and Architectural patterns at the high end (the most universal, applying across entire systems). Most of what gets discussed day-to-day falls in between - the 23 GoF patterns across those three categories above.

Learning design patterns won't make you write perfect code overnight. But it will make you think differently about the code you write. And that shift - from "does it work" to "is this well-designed" - is honestly what separates good developers from great ones.

Start with the big picture. Understand why they exist. Then pick one pattern, read it properly, and find a place in your code where it fits.

That's how it clicked for me.

References: Refactoring.Guru

Top comments (1)

Collapse
 
thepavansai profile image
Pavan Sai

Happy to Answer any questions on design patterns in comments-what pattern are you most confused about?