Singleton Pattern - Design Patterns

  Рет қаралды 162,909

Web Dev Simplified

Web Dev Simplified

Күн бұрын

Пікірлер: 110
@KallyanBRoy
@KallyanBRoy 4 жыл бұрын
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
@fabriziotofanelli
@fabriziotofanelli 5 жыл бұрын
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 =)
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
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.
@fabriziotofanelli
@fabriziotofanelli 5 жыл бұрын
@@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 =)
@julescsv7012
@julescsv7012 3 жыл бұрын
Sometimes I wonder where I would be without you. 🙏🙏🙏
@omarabid3932
@omarabid3932 3 жыл бұрын
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.
@Gabriel-V
@Gabriel-V 2 жыл бұрын
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
@MT-pp9eh
@MT-pp9eh 10 ай бұрын
I didn't undrestand you point here, can you please explaine it more. Thanks
@Matter743
@Matter743 4 ай бұрын
Better use typescript instead of js and make the constructor private
@thehardyboiz1
@thehardyboiz1 5 жыл бұрын
This was pretty useful, i hope that your channel grows a lot in the next few months, you totally deserve it :)
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
Thank you. My channel has already grown a lot the last few months. I can't imagine even more growth.
@davidfonseca698
@davidfonseca698 5 жыл бұрын
And now your words became real. Just last week 20 thousands people subscribe to this channel!
@ehSamurai3483
@ehSamurai3483 Ай бұрын
In Js we can just use object const logger = { logs:[], log : function(){}, printLog: function(){} } this will be passed by reference hence a singelton.
@frheidi8067
@frheidi8067 2 ай бұрын
Extremely clear explanation. Thank you so much!
@nabiisakhanov3522
@nabiisakhanov3522 4 жыл бұрын
Angular services are singleton by default, so I guess this pattern does have its niche
@alejandroperezrivas2397
@alejandroperezrivas2397 7 ай бұрын
damn this has to be one of the best explanations i had with this pattern.
@ardhiirfansyabani4921
@ardhiirfansyabani4921 3 жыл бұрын
to the point and really understandable! love it! keep up those great contents!
@handlethissonny
@handlethissonny 21 күн бұрын
chill color scheme.
@karthik-ex4dm
@karthik-ex4dm 4 жыл бұрын
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...
@klauspetersen8593
@klauspetersen8593 5 жыл бұрын
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
@WaldoTheWombat
@WaldoTheWombat 7 ай бұрын
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?
@Mobin92
@Mobin92 5 жыл бұрын
Singleton pattern is basically a glorified global variable. Change my mind. :^)
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
You are completely right. I generally avoid this pattern, but there are cases where it can be useful.
@MT-pp9eh
@MT-pp9eh 10 ай бұрын
Thank you, waht are the other use cases of singelton pattern in the real world beside loggers and Data base connections ?
@Deretvir
@Deretvir Жыл бұрын
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!
@tlev615
@tlev615 3 жыл бұрын
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?
@someone761
@someone761 3 жыл бұрын
Same question!
@wiczus6102
@wiczus6102 3 жыл бұрын
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.
@kavindudilshan8818
@kavindudilshan8818 Ай бұрын
great video it help me to understand singleton design pattern
@penggao2615
@penggao2615 2 жыл бұрын
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 ?
@areebtariq6755
@areebtariq6755 Жыл бұрын
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.
@areebtariq6755
@areebtariq6755 Жыл бұрын
Here is a sample code for Java Singleton Class , which may clear the difference between Singleton and Static Member
@areebtariq6755
@areebtariq6755 Жыл бұрын
public class Singleton { private static Singleton instance; private int value; private Singleton(int value) { this.value = value; } public static Singleton getInstance(int value) { if (instance == null) { instance = new Singleton(value); } return instance; } public int getValue() { return value; } }
@samislam2746
@samislam2746 3 жыл бұрын
*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?
@tyafizi
@tyafizi 3 жыл бұрын
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
@Pankecal-v4k
@Pankecal-v4k 4 ай бұрын
Thanks kyle. Very well explained.
@vuejs1
@vuejs1 2 жыл бұрын
Can we use this instead of FancyLogger inside the constructor of FancyLogger?
@WaldoTheWombat
@WaldoTheWombat 7 ай бұрын
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?
@nalditrinmartinho1781
@nalditrinmartinho1781 Жыл бұрын
Can we say that some parts of state managers like Redux use the Singleton Pattern? Just to make sure I understood the concept here...
@marcosantonioreyesmedina2364
@marcosantonioreyesmedina2364 5 жыл бұрын
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?
@nonnodacciaio704
@nonnodacciaio704 2 жыл бұрын
Yeah, that way you only have one connection and it's easier to manage
@mycommentmyopinion
@mycommentmyopinion Жыл бұрын
Depends on the use case / how often you make calls to your db. Look into database connection pooling
@jenstornell
@jenstornell 3 жыл бұрын
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.
@ayabochman5453
@ayabochman5453 3 жыл бұрын
Such a good example!! Thank you!
@aryarifqipratama
@aryarifqipratama Жыл бұрын
Singleton adalah 1 instance object sifatnya shared reaource sehingga bisa di pakai oleh object lain
@Carl-yu6uw
@Carl-yu6uw 5 жыл бұрын
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?
@skittlznt2611
@skittlznt2611 2 жыл бұрын
Very well explained. Thank you 🙏🏾
@johnnyxp64
@johnnyxp64 5 жыл бұрын
my favorite pattern! :)
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
Nice. What makes it your favorite. For my the command pattern is one of my favorite.
@johnnyxp64
@johnnyxp64 5 жыл бұрын
@@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 🤣🤣🤣
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
@@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.
@johnnyxp64
@johnnyxp64 5 жыл бұрын
@@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. 😎
@rahimco-su3sc
@rahimco-su3sc Жыл бұрын
thanks a lot for your efforts , i really appreciate that
@aryankumar87771
@aryankumar87771 2 жыл бұрын
is there a video on Factory Design Pattern ?
@briefcasemanx
@briefcasemanx 2 жыл бұрын
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?
@passby8070
@passby8070 2 жыл бұрын
Love this explanation
@Colstonewall
@Colstonewall 5 жыл бұрын
I knew there was something I liked about You! Love the shirt. Any Zeppelin fan is a friend of mine. I'll shutup and sub, now.
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
Thanks! Zeppelin is an incredible band and always enjoyable to listen to.
@mohitbhatia1994h
@mohitbhatia1994h 5 жыл бұрын
The best explanation !
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
Thanks!
@mahdiwebdevelopment2338
@mahdiwebdevelopment2338 4 жыл бұрын
What is strategy pattern? Make a video on this. Thanks
@yahelabraham9821
@yahelabraham9821 2 жыл бұрын
great explanation, thank you
@Crazould
@Crazould 4 жыл бұрын
your videos are awesome! thank you so much!!
@AcousticBruce
@AcousticBruce 3 жыл бұрын
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.
@karthikkumaresan1091
@karthikkumaresan1091 4 жыл бұрын
awesome videos!! Keep them coming
@anngular2227
@anngular2227 2 жыл бұрын
Error: "To load an ES module, set "type": "module" in the package.json or use the .mjs extension."
@AbhishekKumar-vl3cb
@AbhishekKumar-vl3cb 2 жыл бұрын
1). npm init (under folder singleton) 2). Add .js (import logger from './FancyLogger.js')
@vinayyadav-qr7rb
@vinayyadav-qr7rb 5 жыл бұрын
thank'u
@shivaganga9148
@shivaganga9148 5 жыл бұрын
Could you do a video on repository pattern
@WaldoTheWombat
@WaldoTheWombat 5 ай бұрын
Hi Kyle!
@juanyang838
@juanyang838 5 жыл бұрын
Are there any differences between singleton pattern and database?
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
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.
@juanyang838
@juanyang838 5 жыл бұрын
​@@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.
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
@@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.
@audiodrocher
@audiodrocher 4 жыл бұрын
Ohshhhh, this is llifechanging. Thanks a lot.
@aneesahmad168
@aneesahmad168 2 жыл бұрын
O hooo 1K to Million sub 😻
@mangotree9629
@mangotree9629 5 жыл бұрын
Nice explain by u
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
Thanks!
@mahdiwebdevelopment2338
@mahdiwebdevelopment2338 4 жыл бұрын
Just wow! these days i was just looking for how to share class modules with other user objects. Just wow!
@louaykhammar7268
@louaykhammar7268 3 жыл бұрын
thanks
@JonesDTaylor
@JonesDTaylor 5 жыл бұрын
great job.
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
Thanks!
@peterzeng1243
@peterzeng1243 Жыл бұрын
You are so handsome and smart. Clear explanation, thanks!
@hugofilipeseleiro
@hugofilipeseleiro 4 жыл бұрын
Thank y
@aissa.bouguern
@aissa.bouguern 3 жыл бұрын
We can also create a singleton via a factory function instead of doing it inside class constructor. That's much cleaner imo.
@TheBatags
@TheBatags 5 жыл бұрын
Cool Shirt!
@WebDevSimplified
@WebDevSimplified 5 жыл бұрын
Thanks! Led Zepplin is such a great band.
@StuntmanJo
@StuntmanJo 2 жыл бұрын
Dope!
@___dh__dh__
@___dh__dh__ 5 ай бұрын
Why no ;
@pipertripp
@pipertripp 5 жыл бұрын
Zeppelin, nice.
@samms7922
@samms7922 4 жыл бұрын
Now I know how mongoose works behind the scenes.
@augischadiegils.5109
@augischadiegils.5109 3 жыл бұрын
@awekeningbro1207
@awekeningbro1207 3 ай бұрын
This is not correct. Returning a value from a constructor is ignored and can lead to unexpected behavior.
@zum4342
@zum4342 4 жыл бұрын
holy shit 0 dislikes. imma dislike it ahahhaahah jk bro ur awesome
@Rebel101
@Rebel101 Жыл бұрын
Incident implementation. It should just be a module, not a class.
@knackofabhinav
@knackofabhinav 3 жыл бұрын
so redux is singleton 🥲
@ZikoMario
@ZikoMario 3 жыл бұрын
You have got 0.1% dislikes on your video, that is insane.
@greydiamondplayz2078
@greydiamondplayz2078 5 жыл бұрын
Singleton is my last name
@ManuelMontoyaRdz
@ManuelMontoyaRdz 3 ай бұрын
Another reason why OOP sucks.
@seanzoso
@seanzoso 4 жыл бұрын
Led Zeppelin
@yiminzhou7935
@yiminzhou7935 3 жыл бұрын
you are so handsome
@ahmedboutaraa8771
@ahmedboutaraa8771 4 жыл бұрын
.....
@Incognito-ig7ld
@Incognito-ig7ld 3 жыл бұрын
thanks, good, but not professional))
@akhilm4867
@akhilm4867 5 жыл бұрын
Second comment
@akhilmaganti3533
@akhilmaganti3533 5 жыл бұрын
Hi dude im also Akhil M
@akhilmaganti3533
@akhilmaganti3533 5 жыл бұрын
Have a great day!
Facade Pattern - Design Patterns
8:50
Web Dev Simplified
Рет қаралды 107 М.
Builder Pattern - Design Patterns
10:49
Web Dev Simplified
Рет қаралды 138 М.
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 48 МЛН
Don't underestimate anyone
00:47
奇軒Tricking
Рет қаралды 23 МЛН
Why no RONALDO?! 🤔⚽️
00:28
Celine Dept
Рет қаралды 92 МЛН
Singleton Pattern - Design Patterns (ep 6)
20:09
Christopher Okhravi
Рет қаралды 252 М.
Singleton Design Pattern in C# - Do it THAT way
13:15
tutorialsEU - C#
Рет қаралды 29 М.
5 Design Patterns Every Engineer Should Know
11:51
Traversy Media
Рет қаралды 942 М.
Null Object Pattern - Design Patterns
10:24
Web Dev Simplified
Рет қаралды 82 М.
Dependency Inversion Principle Explained - SOLID Design Principles
13:00
Web Dev Simplified
Рет қаралды 163 М.
Composition Vs Inheritance - Why You Should Stop Using Inheritance
10:16
Web Dev Simplified
Рет қаралды 178 М.
Design Patterns in Plain English | Mosh Hamedani
1:20:01
Programming with Mosh
Рет қаралды 1,4 МЛН
5 Design Patterns That Are ACTUALLY Used By Developers
9:27
Alex Hyett
Рет қаралды 308 М.
This Folder Structure Makes Me 100% More Productive
24:36
Web Dev Simplified
Рет қаралды 98 М.
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 48 МЛН