Mastering Design Patterns — 02: Categories & Singleton Pattern

Andrea Gernone 🫀🥷
tech bits pub
Published in
3 min readFeb 12, 2023

--

Boost Your Coding Productivity and Ace Your Interviews with the Singleton Pattern

The Design Pattern Space Dojo

Design Patterns are an essential component of the programming world.
They are a collection of tried-and-true solutions to common issues that arise during software development. They help to keep code organized and easy to maintain, and they can be used as a reference point in the future to solve similar problems ✌️
Last time we have seen where to start with some of the best books on design patterns and some history behind them all.
Today, we’re going to see how they are divided and dive into our first Design Pattern 🚀

DESIGN PATTERNS CATEGORIES

There are three types of design patterns: creational, structural, and behavioral.

1- Creational Design Patterns are concerned with the creation of objects and classes. They can be used to manage the creation of complex objects, such as instances of classes that rely on other classes.

2- Structural Design Patterns are concerned with how code is organized and how classes and objects interact with one another. They are useful for managing class relationships and designing scalable architectures.

3- Behavioral Design Patterns are concerned with how classes and objects interact with one another, as well as how to manage those interactions. They are useful for managing the logic of inter-class interactions and developing applications that respond quickly to user input.

Now that we’ve established what Design Patterns are and how they’re classified, let’s examine the first Design Pattern in the first category: 🥁 🥁🥁 the Singleton!

SINGLETON DESIGN PATTERN

The Singleton Design Pattern if it were a hero

The Singleton Design Pattern ensures that a class has only one instance and provides a global access point to this instance. This is useful when we only want one instance of a class, such as when managing database connections.
Here’s an example of a TypeScript Singleton implementation:

class Database {
private static instance: Database;

private constructor() {}

public static getInstance(): Database {
if (!Database.instance) {
Database.instance = new Database();
}

return Database.instance;
}

public query(query: string): void {
console.log(`Executing query: ${query}`);
}
}

const db = Database.getInstance();
db.query('SELECT * FROM users');

The Database class, as shown in the example, has a private constructor and a static getInstance() method that returns the class instance. This ensures that multiple instances of the class cannot be created and that the only instance is accessible from anywhere in the code.

To use the class, we simply call the getInstance() method and use the result to run a database query. This ensures that no matter how many times the getInstance() method is called, there is always a single instance of the class.

That’s all falks, this was the Singleton Pattern
That’s it!

I hope this example has given you an idea of how Design Patterns can be used to solve common problems during software development! Now this was just the first! Coming next is the FACTORY METHOD! 🚀

Cheers! 🍻

Andrea

--

--

Andrea Gernone 🫀🥷
tech bits pub

I am a full stack professional software developer at @apuliasoft. Based in Italy and passionate about anything that enriches my soul. Also I love to eat