Javascript Design Patterns #2 - Singleton Pattern

preview_player
Показать описание
What is the Singleton design pattern?
The Singleton pattern allows you to limit the number of instances of a particular object to one. This single instance is called the singleton. Singletons reduce the need for global variables which is particularly important in JavaScript because it limits namespace pollution and associated risk of name collisions.

📚Materials/References:

🌎 Find Me Here:

Рекомендации по теме
Комментарии
Автор

Honestly I used to get confused with this 'design pattern' thing, but after watching your series I'm like "Oh, so this is it!" ...Thanks to you bro and stay blessed

stevekingu
Автор

The is "single" video I need to watch to undertand the "Singleton" pattern :)

neuodev
Автор

Nice video... I think that most of design patterns needs to be repaginated when we are in the javascript context. Most of the cases the language can solve some sort of problems in a more simpler way. In the singleton example, you can just export a object with all the required methods in it, and then you can import at any level, on any component because the js module system will not load it twice, so you won't have 2 instances of the same module.

If you don't want that kind of behavior, you can use a factory, so instead of exporting an object with methods within, you can export a function so you have to call it in order to get a new instance of something.

O_Eduardo
Автор

Wow sir.. you truly showed the power of IIFE. Never seen such a beautiful example on IIFE and on Singleton as well.. Kudos!

ayush
Автор

Should probably update this... if you are using either commonjs (node-style) or esm/mjs (module) patterns, you can simply have your variable, constructor/class and export the method to retrieve or instantiate the singleton...

aztracker
Автор

Very good example and simplified explanation.

bhanuprathap
Автор

You should use parantheses even for a single line if statement, at least in videos like these. Because the readability is low in videos so its easy to think that the return belongs to the if.

rutwickgangurde
Автор

Great way of explaining, thank you so much!

ram-gbxg
Автор

Reminds me a lazy loading approach, but with instance initialization instead of actual loading

dmytromahas
Автор

In this example where you have used Process constructor ? Just created and not used ? I got confused due to that.

RahulKumar-fzcg
Автор

Quick Question: Are you dropping down the first bracket intentionally or is this because you are using JSbin. Also, I have noticed that you are not closing out your statements with a semi-colon. Overall, I enjoy your videos because you cover more advanced topcis. Great Job!!!

StaceyWilsonlosangeles
Автор

First of all I appreciate the work you've done, the explanation is great! Thx!
But you've written useless lines of code.
As you reassign pManager twice.
It's simply should be:

const Singleton = (function(){
let pManager

function ProcessManager() { /*...*/ }

return {
getProcessManager: () =>
{
if(!pManager)
pManager = new ProcessManager()
return pManager
}
}
})()

P.S. Adding line numeratorion in your code editor would help to discuss example as well

faultfinderx
Автор

Again, Great video! But a quick question, in "getProcessManager()" isn't reassigning "pManager = createProcessManager()" redundant, and simply calling "createProcessManager()" will do the job?

tahasaleh
Автор

function Process(state) not used ? is there any value for just defining it here ?

javascriptduniya
Автор

Not a critical question, just wondering - why are you using PascalCase here?

Erliortmejurur
Автор

I'm working with a "black box" of APIs and want everyone's opinion.
Should I should use this pattern instead of using global variables? I have limited options.

mattsmith
Автор

I'm finding it difficult to understand Singleton. But from my understanding, the same below could already served the purpose.

const x = {}

function Singleton(){
return x;
}

module.exports = Singleton;


* invoking new or calling the Singleton() itself will just return the same object. So singleton, isn't that right? After all, why bother with al the complexity when we could all understand it this way

ultimatewarriorfrieza
Автор

Sage.. I feel like I'm using Singleton way too much.. Basicly everytime I have a class with some kind of "state" I always make it a Singleton because I want to access the same data instance everywhere.

Does this sound like bad design/arquitecture or is this common to happen?

joaomendoncayt
Автор

why did we use an IIFE for the Singleton Function?

samdimahmood
Автор

Why does the if statement returns true for the 2nd time, how does javascript knows pManager exists on the second invocation?

kachunchan