samedi 24 décembre 2022

Singleton pattern. what can replace it? [closed]

I use a singleton in my project, as it is convenient for use in other classes.

But more and more I see that people do not like him

what is it connected with? And if it's so bad, what can replace it?

ok, I'll ask in a different way, what do you replace singleton with?

    class A {
        constructor() {
             this.context = new B().GetContext()
        }   
        ...
    }

    class B {
        constructor() {
             if(B.inst)
                return B.inst
             B.inst = this
             this.context = 'context'
        } 
        GetContext(){
          return this.context
        }
    }

    class C {
        constructor() {
             this.context = new B().GetContext()
        } 
       ...
    }

// and more class use B.GetContext()

Aucun commentaire:

Enregistrer un commentaire