Most of the NPM modules (major ones) uses this pattern, e.g: Mongoose. This pattern is one of the best if anyone is creating a library or something like that as you might need to output some debug information for the module. Great video thanks a lot....BTW thanks for changing your uniform
its kind weird to see this smooth explanation regarding this subject because, most of times, some teachers don't really grasp this concept and makes this even harder... great content man... keep it up =)
Thanks! I really appreciate it. I always put in extra time researching and working with any technology/topic before I record a video on it even if I already know it really well, since I want to make sure I understand all the intricate details so I know which parts are the important parts I need to explain in a way that makes sense.
@@WebDevSimplified And I really appreciate your efforts to bring us the best way to absorbe, sometimes, complex subjects and break them into tiny pieces easily to understand and digest.... hope to see much more great content from you =)
Hi, The implementation looks good but In order to verify that the singleton implementation is working correctly you need to instantiate the FancyLogger twice, in your current implementation the constructor is called only once.
Agreed. And the export is the class itself, because whenever you run new FancyLogger(), you will get back the same FancyLogger instance. and that is handled inside the class constructor itself. If you export the logger instance created, as shown in the video, the constructor doesn't need to be changed this way
The only video I found on youtube with 10K+ views and 0 dislikes... For a long time, I was thinking guys at youtube randomly add dislikes to videos coz there are many good videos that got dislikes for no reason...
Nice explenation but... All the design patterns should be presented in TypeScript. The key words like private, public, static, protected are crucial in my opinion. On the other hand, Everyone who has the TS knowledge should handle to add TS to your's code :) Thank You!
Great explanation, and thanks for the video, just have 1 question, is it really necessary to do this manipulations inside constructor, if we import instance of logger it will be same instance for any file because of modules that are singletones anyway? Just wondering why we really need that checks here?
If you work in a team, you want other people to use it correctly. Let's say it's the company directive to get the log count. If a person would by accident instantiate FancyLogger, they'd make a silent error. People will get the wrong log count and won't know that. If you make a class, you should make sure that others will use it correctly.
Nice explanation, and use case, cheers. Do such singleton running on client side js commonly then persist their log array to db to allow after the fact debugging from one central place?
Why export an instance variable? Wouldn't it be the same if first and second file just call new, which would return the same instance and then move Object.freeze into the constructor? Thinking it would be a cleaner solution
Yeah I also didn't get that, Kyle already solved it in the constructor, so why can't the other files just the keep using the instantiating with the class?
Hi Kyle, thanks for the video. I have a question regarding to this singleton pattern: what's the difference between using an instance 'logger' and using a 'static' class attribute in the class 'FancyLogger' to record the logs ?
Don't Know about JS but as for Java a class cannot be static , only its members can be static. A static member can be access by all instances of that class.
Hi, very good video. We have always seen the "Singleton" pattern applied to a DB Connection class, what do you think ... do you think it is correct to apply it in this case?
@@WebDevSimplified is just that since i started coding with gwbasic back in the 90s there was no patterns like today and without any special reading when i was 10 and discovered i could share data info among different "screens" by using a common "module"(even in today's vb.net is still been used) that was so excited for me as a kid. so is my favorite just for nostalgia reasons 🤣🤣🤣
@@johnnyxp64 That makes a lot of sense. Nostalgia is pretty powerful. For me the command pattern just really opened up how powerful clean code and design patterns can be which was really exciting.
@@WebDevSimplified makes sense. is one of the pillars in OOP (command pattern) in my opinion and everything that uses encapsulation is helping the coder to focus on what matters and keeps indeed a clean and more readable code. 😎
*If somebody knows please help*, What will happen when you write: class X { constructor(){ X.instance = 8; // Here } } 1- What does X.instance mean? It's mentioned in the video nearly at 6:00 2- Where the X.instance is going to be? 3- What is the difference approach and using a static property?
1. It's mean that you set static property called 'instance' for your class X. 2. In the class like any other static fields. 3. Difference is that if you use this approach method you can't define an instance property without creating an instance of this class. class MyClass { constructor() { if (!MyClass.instance) { MyClass.instance = this; } return MyClass.instance; } field = 42; } console.log(MyClass.instance); //undefined const myClass = new MyClass(); console.log(MyClass.instance); //MyClass {field:42} (Or in your case 8) So all next instances will be known that you've already created some instance and instead of returning new X they will return that instance const newClass = new MyClass(); console.log(myClass === newClass); //true
Is it a good idea to use singleton to simplify plugin methods, something like field.setValue(34); Then it's like a namespaced function where the plugin maker does not need to add new instances of the class.
Isn't a database just a singleton? Its a shared resource that can be used by many other processes for different purposes, instead of each of those processes having their own separate set of data. Am I wrong, or do people that don't like singletons not like databases either?
But fi you already modified the constructor to always return the first instance that was ever created, then why can't you just keep exporting the class instead of that object like you did in the first place?
I paused video and tried something first and then I finished your video and saw it was much different.. Does anyone know the pros and cons of my version? let L; class Logger { ... } if (!L) { L = new Logger(); } module.exports = L; Is there something wrong with this version? I did this in Node and because Node caches all uses of require, I figured this was okay.
The singleton design pattern and databases are two completely different things. A database is a place to store information in an efficient long term place while the singleton design pattern is just a way to structure code so that you only ever have one global version of an object.
@@WebDevSimplified Then using a database to store an objects is waste and misused? I'm having a plan to create a small database for re-merging every properties on the same column and sending them back to the each instances just like how singleton does.
@@juanyang838 I am not sure what you mean. A database is for permanent storage of things you need to access later. I do not really know what you are trying to do.