Design Patterns: Singleton Pattern and Anti-Pattern in TypeScript

Tram Ho

How to use Singleton Pattern with TypeScript to solve real world problems in web projects makes it possible to Share a single Global Instance throughout the application.

Welcome to the Design Patterns in TypeScript series, this series will introduce some useful Design Patterns in web development using TypeScript.

Design Patterns are very important for web developers and we can code better by mastering them. In this article, I will use TypeScript to introduce the Singleton Pattern .

Singleton Pattern

Singleton Pattern is a common pattern and we usually only need one of several objects, such as public buffers, windown objects in Browsers, etc. Singleton Pattern is used to ensure that there is only one Instance of a Class and to provide a single global access point for it .

Let’s see how to use TypeScript to implement the Singleton Pattern.

When defining the Singleton class, there are three important steps:

  1. Define a private static Properties to hold the Instance object;
  2. Define a private constructor ;
  3. Provide a public static getInstance() to get an Instance object.

In VSCode, for private static Properties, VSCode’s IntelliSense will automatically filter out these Properties :

image.png

For private constructor , when we create an Instance via new Singleton() , TypeScript compiler will prompt the following error message:

With the Singleton class, try to check if its instances are really unique:

The easiest way to implement a singleton in JavaScript is to use an object literal

If you need to contain additional private properties or methods , you can use the following ways to create a singleton:

If you are using ES6, you can declare a singleton using ES Modules very easily:

Axios is widely used in various web projects and the provided axios object is also a Singleton object.

The example above axios uses the default configure object, of course Axios also allows us to use the axios.create function to create a new instance of axios with a custom configuration.

Use cases of Singleton Pattern:

  • Objects take too long or resources to create but are often reused over and over again.
  • Since Singleton Pattern has only one Instance Instance, memory usage is reduced especially when an object needs to be created and destroyed frequently => can’t optimize performance during creation or destruction, that is luc Singleton Pattern brings out its full power.

However, Singleton Pattern is not always useful, sometimes using it will also be an Anti-Pattern.

For example: If you code Nodejs + Postgres, you are familiar with this pg library, right? Connecting takes time and also takes resources. Wow, then the formula and then apply the formula.

pool is now a Singleton Instance ok to use.

Opps!!! not good at all in this case then using 1 Instance is counterproductive -> bug

According to node-postgres doc: To perform a transaction with node-postgres , you just need to execute BEGIN/COMMIT/ROLLBACK queries through an Instance which is fine. Note however: You must use the same Instance client for all statements in a transaction . PostgreSQL isolates a transaction to client . This means that if you initiate or use transaction using the pool.query method, you will get **LỖI** . It is strongly recommended not to use transaction with the pool.query method.

The correct way would be:

=> When you use a certain Design Pattern, even the formula is the same as the textbook, sometimes it’s a very normal thing. Vietnamese teachers also have a classic sentence: Math is very easy, you just need to apply the formula. It’s true that you just need to apply the formula… but leave the exam room early to submit.

Next time I will also write an article about Anti-Pattern and will dig into this issue. Remember to follow me to read.

Roundup

As always, I hope you enjoyed this article and learned something new.

Thank you and see you in the next posts!

If you find this blog interesting, please give me a like and subscribe to support me. Thank you.

Ref

Share the news now

Source : Viblo