Software Design Patterns
Tags: systemdesign, designpatterns
Published: 2024-07-17 12:58:09

Becoming a good software engineer isn't just about memorizing syntax or code snippets, it's about solving problems effectively with the tools at your disposal. By the end of this article, you'll understand some key design patterns that can help you tackle common challenges in software development. 

## What are Design Patterns?
Design patterns are reusable solutions to common problems in software design. They represent best practices refined through repeated application in various contexts. The **Gang of Four ** categorized design patterns into three main types: Creational, Structural, and Behavioral patterns. Each category addresses different aspects of software design:

- Creational Patterns: How objects are created.

- Structural Patterns: How objects relate to each other.

- Behavioral Patterns: How objects communicate with each other.

Let's dive in!

## Creational Patterns

**1. Singleton Pattern:**
The first in this category is called a singleton. This just means that class can only have one instance of itself at any given time. Think of a database connection in an application. You only want one instance of the connection to avoid conflicts and ensure consistency.
The Singleton pattern ensures that a class has only one instance and provides a global point of access to it. 

**2. Prototype Pattern:**
Inheritance is a term in Object Oriented Programming where a class can be extended with a subclass. One problem with inheritance is that it can lead to a complex hierarchy of code. The prototype pattern is an alternative way to implement inheritance. Instead of inheriting functionality from a class, the functionality comes from an object that has already been instantiated. The Prototype pattern involves creating new objects by copying an existing object, known as the prototype. This creates a simple prototype chain that makes it easier to share functionality between objects. 
In JavaScript, you might be familiar with prototype chains. When you create a new object by cloning an existing one, you are using the Prototype pattern. This can be more flexible than traditional class inheritance.

**3. Builder Pattern:**
The Builder pattern separates the construction of a complex object from its representation, allowing you to create different representations using the same construction process.
Imagine you are a worker at a fast-food restaurant, and a customer orders a combo meal. It is somewhat hard to keep track of all the items the customer ordered at once. Instead of preparing the meal all at once, you build it step-by-step: first the burger, then the fries, and finally the drink.
With the builder pattern, we can create the order systematically with methods rather than once and we can even delegate the building logic to an entirely different class. The Builder pattern lets you handle complex object creation in a similar systematic manner.

**4. Factory Method Pattern:**
Instead of instantiating an object directly, we use a method to do it for us. The Factory Method pattern defines an interface for creating an object, but allows subclasses to alter the type of objects that will be instantiated. 
A real life use case is when developing applications for different operating systems. You can use a factory method to instantiate UI components specific to each OS. This way, the main application code does not need to know the specifics of each OS.

## Structural Patterns

**1. Façade Pattern:**
In reality, a façade is the face of a building. Inside the building are many things that make it work, like plumbing and electricity, that residents of the building interact with but don't know in detail about. 
The façade pattern is a simplified method used to hide low-level details in a system.
In a building, there could be a pluming class and an electricity class. The residents of the house don't need to know about all these system in detail. We create a façade class called "house" that contains the systems as dependencies but simplifies their operation. we could mix the functionalities of these systems in a way residents can interact with.

**2. Bridge Pattern:**
The Bridge pattern is a structural design pattern that separates an object’s interface from its implementation, allowing both to vary independently. This is particularly useful when you want to avoid a complex inheritance hierarchy and prefer a more flexible and scalable solution.
Imagine you have a drawing application that supports different shapes like circles and squares, as well as different rendering methods such as vector and raster. Without the Bridge pattern, you might end up with a convoluted class hierarchy like: "VectorCircle", "RasterCircle", "VectorSquare", "RasterSquare", and so on. This can quickly become unmanageable as the number of shapes and rendering methods increases.

##Behavioral patterns 

**1. Iterator Pattern:**
This pattern allow for traversing through a collection of objects. high level languages like python and c have abstractions for this pattern with "for" and "while" loops. It is a pull-based approach.

**2. Observer Pattern:**
This pattern allows many objects subscribe to events that are broadcasted by other objects. The Observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified. This is used heavily in today’s development world where data needs to be updated on arrival. A use case of this pattern is on social media app apps where users can follow others. When a user posts an update, all his followers are notified. This pattern offers a push-based approach.

## Which is the best?
There is no best design pattern! The best solution depends on the pattern that works in solving your design problems. There are more design patterns that were not covered in this article. Dive into the world of design patterns, explore their applications, and see how they can transform your software development practices!