1

I learned that we should avoid Singleton. I was wondering if I develop a module in this fashion, am I doing a Singleton?

class MyClass{

}

export default new MyClass();

Since every time I load the module I will get the same instance, won't I?

Thank you

mplungjan
  • 169,008
  • 28
  • 173
  • 236
47ndr
  • 583
  • 5
  • 23
  • 1
    No, [it's not a proper singleton](https://stackoverflow.com/a/38741262/1048572). [Don't do this](https://stackoverflow.com/a/39079929/1048572). – Bergi Sep 29 '20 at 12:32
  • 1
    What does your `MyClass` actually do? If it does keep state, yes you should avoid exporting only a single instance. If it does not keep state, you should not use a `class`. – Bergi Sep 29 '20 at 12:34
  • Thanks for the links. I am just trying to figure out why I never see the "new" when I import third-party modules. They seems all Singleton, but if it should be avoided how they build them? For example Axios, Express, Lodash. You never import the lib and than "new Axios()" or "new Express()". Why? – 47ndr Sep 29 '20 at 12:40
  • Axios doesn't have global state afaik, it just exports a function. And express actually *does* export a "constructor" where you could [`const app = new Express()`](https://expressjs.com/en/5x/api.html#app), although this constructor is also a singleton instance since express is usually used as a framework with one global instance (and one global http server listening to a single port) and they want to simplify that usage. Not that it's a good practice, but in some use cases it's what you need. – Bergi Sep 29 '20 at 12:50
  • I was studying Axios source code, and I think Axios return an instance of its class. https://github.com/axios/axios/blob/master/lib/axios.js It exports the return of `createInstance()` function that will indeed return a new Axios instance. So not that different from what I am doing. Is this still bad? – 47ndr Oct 01 '20 at 11:01
  • Ah, I didn't know `Axios` was a class that's holding configuration. But notice that they *both* export the `Axios` class and export an instance with reasonable default configuration (which one should avoid to mutate). – Bergi Oct 01 '20 at 11:50

2 Answers2

0

it's not Singleton , look to example down

class SingletonClass {

    private static _instance:SingletonClass = new SingletonClass();

    constructor() {
        if(SingletonClass._instance){
            throw new Error("Error: Instantiation failed: Use SingletonClass.getInstance() instead of new.");
        }
        SingletonClass._instance = this;
    }

    public static getInstance():SingletonClass
    {
        return SingletonClass._instance;
    }

}

https://codebelt.github.io/blog/typescript/typescript-singleton-pattern/

Aian
  • 226
  • 3
  • 10
  • There are many singleton patterns. Yours is only one of them. What is your definition of "singleton" and why do you think the code in the question doesn't create one? – Bergi Oct 01 '20 at 11:51
  • Singleton should be all time "one instance", in his example is I have imported twice time with diff alias will be one instance ? @Bergi – Aian Oct 22 '20 at 12:20
  • Yes, no matter how often you import a module, you get the same exported values. The alias does not matter at all. – Bergi Oct 22 '20 at 12:24
0

Let's look at an example of a singleton.

Let us say I have a class named Accounting Department, and in real life scenario, there is only one accounting department. Hence, we need only one instance.

class AccountingDepartment {

 private static accountingDepartment: AccountingDepartment;
 private id: number;
 private name: string;

 static getInstance(): AccountingDepartment {
    if (this.accountingDepartment) {
      return this.accountingDepartment;
    } else {
      this.accountingDepartment = new AccountingDepartment(2, 'AD');
      return this.accountingDepartment;
    }
  }

  private constructor(id: number, name: string) {
    this.id = id;
    this.name = name;
  }
}
const accounting = AccountingDepartment.getInstance();
const accounting1 = AccountingDepartment.getInstance();

If you see, both accounting, as well as accounting1, will give you the same object in the console. So only one instance of a singleton is created.

Points to be noted here:

  1. A private constructor is being used here. So that no other class can access this constructor.
  2. The method is static as we need to access the method only through the class here.
  3. The getInstance method clearly states that if there is already an instance of the class, return that instance, only otherwise create a new instance and return it.

As only a Single Instance of the class is created, that is why it is a Singleton Pattern.

Bidisha Das
  • 252
  • 2
  • 14