Singleton Design Pattern TypeScript

The Singleton design pattern is a creational pattern that ensures a class has only one instance and provides a global point of access to that instance. This pattern is widely used in object-oriented programming to ensure only one instance of a class is created.

In TypeScript, the Singleton pattern can be implemented using several approaches, including using a static property, a private constructor, and a static method. In this article, we will explore each of these approaches and see how they can be used to implement the Singleton design pattern in TypeScript.

Singleton Design Pattern TypeScript

To implement the Singleton pattern, you need to ensure that your class has a private constructor and a static method that returns an instance of the class. The static method should check if an instance of the class already exists and, if not, create a new instance of the class.

Working on Redis integration in Node.Js TypeScript? Checkout here how to work with various Redis connection events.

Let’s start discussing how to implement design patterns with various approaches.

Static Property Approach

The first approach we will explore is the static property approach, where we declare a static property on the class to hold the Singleton instance. Here is an example of how this approach can be implemented in TypeScript:

In this example, we declare a static property called instance, which holds the Singleton instance. We then create a static method called getInstance() that checks if an instance of the Singleton class exists, and if not, creates a new instance of the class. Finally, we add a public method called doSomething() that can be used to perform some action.

In this example, we create two instances of the Singleton class using the getInstance() method. As the Singleton pattern ensures that only one instance of the class is created, the two instances are actually the same object, and the comparison returns true.

Private Constructor Approach

The second approach we will explore is the private constructor approach, where we declare a private constructor to prevent other parts of the program from creating instances of the Singleton class. Here is an example of how this approach can be implemented in TypeScript:

In this example, we declare a private constructor to prevent other parts of the program from creating instances of the Singleton class. We also declare a static property called instance, which holds the Singleton instance. We then create a static method called getInstance() that checks if an instance of the Singleton class exists, and if not, creates a new instance of the class. Finally, we add a public method called doSomething() that can be used to perform some action.

Check the output of code console.log('Are both instances same?', instance1 === instance2); below.

Private Constructor Singleton Approach

Static Method Approach

The third approach we will explore is the static method approach, where we use a static method to create and return a Singleton instance. Here is an example of how this approach can be implemented in TypeScript:

In this implementation, the Singleton class has a private constructor, which prevents it from being instantiated outside of the class. The getInstance static method checks whether an instance of the Singleton class already exists, and if not, creates one. The getInstance method always returns the same instance of the Singleton class, ensuring that there is only ever one instance of the class.

You can then use the getInstance method to get a reference to the singleton instance and call any methods on that instance as usual.

Note that this implementation assumes that the Singleton class is used within a single runtime. If you need to create a singleton that works across multiple runtimes or processes, you may need to use a different approach, such as using a database or other shared storage mechanism to ensure that only one instance of the class is created.

Static Method Approach to Implement Singleton

Read here how to prevent class instatiation in Typescript.

Conclusion

Singleton pattern can be a useful tool for ensuring that only one instance of a class is created, and can be particularly useful in situations where resources are limited or where multiple instances of a class could cause problems. By implementing the Singleton pattern in TypeScript, we can create more secure, maintainable, and efficient code.

Leave a Reply

Your email address will not be published. Required fields are marked *