Singleton Pattern

-- by Vikram

Certain portions of your app you would want one and only one instance of something. Like you can't have multiple jQuery instances or at the least you don't need them until unless you love chaos.

Say you want a single API client and you want all your requests to pass through this particular object and it should only have a single instance because you are observing this object for some sort or performance monitoring needs and all. 

Whenever in your app you need one and only one instance of something like in video games you as a player is singleton, there won't be many you of course there are other players in multiplayer games. But as a main player it's always one instance that you need. 

To achieve this, all you need is a condition which checks if already an instance exists just return that and   do not create a new object. 

The only problem or difficulty with Singleton pattern is that you may not get the ready to use feature from the languages, but the key to solve this problem is to make the constructor private and as soon as you do that you fall into chicken and egg problem, don't worry you can always have a public static function that can call your private constructor and save the newly created instance in the static variable and always return the same object it called again.

Different languages may have different way to create private constructors, "Dart" language has _internal () and factory() constructor that can even eliminate the need of static public method.

Some sort of application/environment configuration object can also be a good use case for Singleton. The main advantage here is that you can have a globally accessible object that can be lazy initialized and yet you don't pollute the global environment. Also, the major point that it guarantees that there can never be more than one instance of it.