Lädt...


🔧 Singleton design pattern


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

This is part of the design pattern series in Typescript.
Code for this and other patterns can be found here: https://github.com/abhidatta0/Essential-design-patterns-in-Typescript

Why singleton is needed ?
There are some cases where we need to ensure only one instance of a class exists.
e.g: Thread pools, Caches, Settings.

Singleton Pattern ensures a class has only one instance, and provides a global point of access to it

Example
We know, every school has only one headmaster(or principal).
Irrespective of the situation , where the headmaster is called it should return the same person (or in code "instance").
Code

  class Headmaster{
    static uniqueInstance: Headmaster;
    public readonly name = "Albert Einstein";

    private constructor(){}

    static getInstance(): Headmaster{
      if(!Headmaster.uniqueInstance){
        Headmaster.uniqueInstance = new Headmaster();
      }
      return Headmaster.uniqueInstance;
    }
}

Let's break it down and explain.

  1. static uniqueInstance: Headmaster -> This is a static variable to hold our one instance.
  2. public readonly name = "Albert Einstein" -> This is a property of the instance. For singleton, every object will have same property values. readonly so it cannot be modified from outside.
  3. private constructor -> Our constructor is made private ; only class itself can instantiate this class!
  4. static getInstance(): Headmaster-> This is a static method which returns a instance of Headmaster class.We first check if the uniqueInstance is null or not.
    • If uniqueInstance is null, we first create it via the private constructor and assign it to the uniqueInstance.
    • If uniqueInstance isn't null , then it was previously created. We just fall through to the return statement.

Testing

const hm1 = Headmaster.getInstance(); //  instance 1
const hm2 = Headmaster.getInstance(); //  instance 2

console.log(hm1 === hm2); // will print true

Output:

Output

Eager vs Lazy creation
If your application always creates and uses an instance of the Singleton class or the overhead of instance creation is not much, you may want to create the Singleton eagerly.

"Eager" means the instance will be created at the time of class creation in runtime.
This will require slight modification of above code.

class Headmaster_Eager{
      static uniqueInstance: Headmaster_Eager = new Headmaster_Eager();   

      ...
      ...

    static getInstance(): Headmaster_Eager{
      return Headmaster_Eager.uniqueInstance;
    }
}

Here , no need to check if uniqueInstance is null or not in getInstance() method.

However, the eager way should be followed only if the object is always needed in your creation, otherwise it will waste memory.

That’s it 😅

Code for this and other patterns can be found here: https://github.com/abhidatta0/Essential-design-patterns-in-Typescript

...

🔧 Singleton-Pattern | Javascript Design Pattern Simplified | Part 1


📈 52.82 Punkte
🔧 Programmierung

🔧 What is a singleton class in Java? And How to implement a singleton class?


📈 43.05 Punkte
🔧 Programmierung

🔧 Singleton Design Pattern


📈 40.15 Punkte
🔧 Programmierung

🔧 The Singleton Design Pattern: Ensuring a Single Instance in Java


📈 40.15 Punkte
🔧 Programmierung

🔧 Singleton design pattern


📈 40.15 Punkte
🔧 Programmierung

🔧 Singleton design pattern


📈 40.15 Punkte
🔧 Programmierung

🔧 Elevating Laravel Development with the Singleton Design Pattern


📈 40.15 Punkte
🔧 Programmierung

🔧 Singleton Design Pattern: When Not to Use It


📈 40.15 Punkte
🔧 Programmierung

🔧 Getting a Grip on the Singleton Pattern in Software Design: Ensuring a Single Instance


📈 40.15 Punkte
🔧 Programmierung

🔧 Singleton Design Pattern


📈 40.15 Punkte
🔧 Programmierung

🔧 Singleton Design Pattern, TypeScript example


📈 40.15 Punkte
🔧 Programmierung

🔧 Design Pattern #1 - Singleton for frontend developers


📈 40.15 Punkte
🔧 Programmierung

🔧 Design Pattern - Singleton


📈 40.15 Punkte
🔧 Programmierung

🔧 Singleton Design Pattern


📈 40.15 Punkte
🔧 Programmierung

🔧 Solving Logs Woes: A Small Dive into Singleton Design Pattern


📈 40.15 Punkte
🔧 Programmierung

🔧 Golang - Singleton Design pattern


📈 40.15 Punkte
🔧 Programmierung

🔧 Redis cache clé / valeur ou comment éviter l'anti-pattern Singleton


📈 34.2 Punkte
🔧 Programmierung

🔧 Implement the Singleton pattern


📈 34.2 Punkte
🔧 Programmierung

🐧 Das Singleton Pattern – eine Klasse für sich


📈 34.2 Punkte
🐧 Server

📰 heise+ | Wie man ein Singleton-Pattern threadsicher initialisiert


📈 34.2 Punkte
📰 IT Nachrichten

🔧 The Singleton Pattern in C#: Why It’s a Classic Choice


📈 34.2 Punkte
🔧 Programmierung

🔧 Singleton Pattern, Backend State management and Pub Subs


📈 34.2 Punkte
🔧 Programmierung

🔧 Singleton Pattern in Flutter


📈 34.2 Punkte
🔧 Programmierung

🔧 Understanding the Singleton Pattern with PHP Example


📈 34.2 Punkte
🔧 Programmierung

🔧 Mastering Singleton Pattern in Kotlin: Challenges & Calling Strategies


📈 34.2 Punkte
🔧 Programmierung

🔧 Understanding the Singleton Pattern in TypeScript


📈 34.2 Punkte
🔧 Programmierung

🔧 Singleton pattern in PHP and IOC (Inversion Of Control) container in laravel


📈 34.2 Punkte
🔧 Programmierung

🔧 Design Pattern #5 - Adapter Pattern


📈 31.3 Punkte
🔧 Programmierung

🔧 Module-Pattern | Javascript Design Pattern Simplified | Part 4


📈 31.3 Punkte
🔧 Programmierung

🔧 Decorator-Pattern | Javascript Design Pattern Simplified | Part 5


📈 31.3 Punkte
🔧 Programmierung

🔧 Factory-Pattern | Javascript Design Pattern Simplified | Part 2


📈 31.3 Punkte
🔧 Programmierung

🔧 Observer-Pattern | Javascript Design Pattern Simplified | Part 3


📈 31.3 Punkte
🔧 Programmierung

🔧 Design Pattern #4 - Publisher/Subscriber Pattern


📈 31.3 Punkte
🔧 Programmierung

🔧 Design Pattern #3 - Observer Pattern


📈 31.3 Punkte
🔧 Programmierung

🔧 Design Pattern #2 - Facade Pattern


📈 31.3 Punkte
🔧 Programmierung

matomo