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

Top comments (0)

Top Heroku Alternatives (For Free!)

Recently Heroku shut down free Heroku Dynos, free Heroku Postgres, and free Heroku Data for Redis on November 28th, 2022. So Meshv Patel put together some free alternatives in this classic DEV post.