February 16, 2023
This is a immense topic, but on this post I will try to summarize the core concepts and explain and the most common design patterns in software (or at least those I've personally run into during my career).
I'll not pretend and say that I've read back to back Design Patterns by the GoF ✌️, this will most likely be a "cheatsheet"-like article with examples and some opinions.
You can say these patterns are some kind of elegant solutions to repeating problems in software development. In a nutshell a design pattern shows you how you should structure your classes and how these classes should communicate with each other.
My main takeaway from reading about this topic is that once your learn a couple (the most common ones) you will recognize them everywhere, so learning a new framework for example will become much more easy.
It also helps us 'communicate' with other developers and coworkers in a more abstract way; like when working on a new feature... like: "let's implement a singleton pattern for our database connection"... instead of: 'lets have some kind of global variable somewhere to keep the reference'.
Also helps when you hire someone; if your project follows some of these patterns and they know the basic concepts seeing something like:
let connection = ResultsServer.getInstance();
// or ...
let car = CarBuilder()
.addEngine("v10")
.addFuel(8.0)
.build();
// will become insta-recognizable
Not all is great, theoretically sure... but in practice i've ran into many projects that where a complete pile of ... patterns-on-top-of-patterns and the actual business-logic was hard to understand. Same thing with OOP programming... when it becomes a massive tree of class-hierarchy it actually becomes even harder to do any kind of structural modifications.
So be careful of not falling into that, specially when writing something from scratch like a library.
The book I mentioned before covers about 23 different design patterns but there are many more (and undocumented). One example is the Repository Pattern that became a thing years later after the book was released and currently is present in many ORMs and database-like libraries.
For now I will just focus on the following ones that in my experience are the most used ones. We split them in three categories:
Different ways to create objects. Instead of you instantiating all objects very specifically (and explicitly), these patterns give you more flexibility in how the objects are actually created.
Example here Github @ factory.ts
It separates the construction of a complex object from its representation. It allows you to build objects step-by-step, providing flexibility and control over the creation process.
Example here Github @ builder.ts
Ensures that a class has only one instance and provides a global point of access to it. It's often used for objects that need to be shared across the application, like a database connection or a logger.
Example here Github @ singleton.ts
Key Points:
When to Use Singleton:
Caution:
These help you with how inheritance and composition(and aggregation) can be used to provide extra functionality. Like the relationship between these objects.
Allows incompatible interfaces to work together. It provides a wrapper class that converts the interface of one class into an interface expected by clients.
Example here Github @ adapter.ts
Benefits of the Adapter Pattern
Use Cases
Provides a unified interface to a set of interfaces in a subsystem. It simplifies the interaction with the subsystem, making it easier to use.
Example here Github @ facade.ts
Use Cases:
Interaction or communication and assignment of responsibilities between our objects
Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Example here Github @ observer.ts
Use Cases:
It separates the traversal logic from the collection itself, promoting flexibility and encapsulation.
Example here Github @ iterator.ts
Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
Example here Github @ strategy.ts
I'm a 32-years old programmer and web developer currently living in Montevideo, Uruguay. I been programming since I was about 15 y.o, and for the past 12 actively working in the area.