DEV Community

Cover image for Singleton Pattern
Francesco Ciulla
Francesco Ciulla

Posted on • Updated on

Singleton Pattern

Singleton Pattern

Single object instance

GitHub code : https://github.com/FrancescoXX/Design-Patterns-Singleton-Javascript


Creational pattern

⚡️Problem (we need)

  • One configuration file, from different components
  • Always the same file
  • Guaranteed that there is only one file

💡Intent

  • Ensure a class only has one instance.
  • Provide a global point of access to it.

🔧 Apply

  • One instance of a class is required
  • It must be one access point
  • Need to manage object instances

✅Pro

  • Single point of access to the instance
  • Avoid global variables
  • Ability to subclass the singleton class
  • Configure the number of instances

⚠️Cons

  • State of the singleton must be shareable between program executions

🏆 Great for

  • Manage number of instances at runtime
  • Provide one single unique access

Structure
Alt Text


GitHub code : https://github.com/FrancescoXX/Design-Patterns-Singleton-Javascript

Latest comments (0)