The Singleton Design Pattern - Part of the Gang of Four

  Рет қаралды 75,695

IAmTimCorey

IAmTimCorey

Күн бұрын

Пікірлер: 304
@mwoe4035
@mwoe4035 4 жыл бұрын
Hello Tim. I'm from Germany and programming in C# since 18 years now. I really enjoy your videos and learned much from you. I just want to say thank you for what you are doing here for us programmers. Videotutorials for free in this amazing quality are rare. So again: Thank you!
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
I appreciate the kind words.
@Berlm
@Berlm 3 жыл бұрын
You're completely right. Most videos of this quality are locked behind paywalls.
@DylanSterling
@DylanSterling 4 жыл бұрын
"My goal isn't to entertain you." Well, too bad. Great videos. Very educational AND highly entertaining. Thank you for the effort you put into them.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
lol thanks
@officialspock
@officialspock 4 жыл бұрын
Thanks for this, please discuss all the GOF Design Patterns :)
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
I will add it to the list. Thanks for the suggestion.
@JOHNSONLOBOlobojony
@JOHNSONLOBOlobojony 4 жыл бұрын
May be first cover frequently used design pattern's and then others. I am just loving your explanation on topics and encourage you to do more videos on such topics where i can write clean code
@Aprofox
@Aprofox 4 жыл бұрын
@@IAmTimCorey Yes Tim, we want more design patterns :)
@saifilali7221
@saifilali7221 2 жыл бұрын
@@IAmTimCorey is this out there or on your list of things? You are the best instructor.
@jt.633
@jt.633 Жыл бұрын
your videos are long and i am impatient but when I clicked on them you are explaining things so well and are truly tapped into the learner's mindset. Thank you!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@gmt8336
@gmt8336 4 жыл бұрын
Ive been writing production level code now for nearly two years now, wasn't until I started watching Tims videos that I realised that I didn't know nada.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
lol, I'm sure you knew more than that but I am glad my videos are helping.
@mac19999x
@mac19999x 4 жыл бұрын
Going to go through some more patterns? Awesome. Damn you're good at explaining these things.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Thank you!
@gp6763
@gp6763 4 жыл бұрын
I'll try to add some information about Singletons. I came from huge fan of Singletons using them everywhere I could to a moderate user. First I think it is interesting to point out Singletons, as design pattern, are one of the best source of discord and debate between programmers. So Tim, you explained very well why they are useful I'll try to explain why some programmers considers them as an anti-pattern (I disagree with that, but they have some interesting arguments). 1) Multi purpose : A Singleton will always have at least 2 purposes : manage his instantiation and the reason why you create it. So by using it you transgress the single responsibility principle. If you know what you're doing it is not a big deal, but still, at this point you already accept to not keep your code SOLID. 2) Most of the time not the best : This is more a problem for beginners which just learned this pattern That for me the biggest problem with singletons they are easy to use and once you now them you will easily want to use them where they really are not the best. They are able to do a bit to much jobs. 3) Unit testing : That a big one. Yes you can unit test with a Singleton in your workflows. But more the Singleton take importance and more the side effect will be hard to manage at some point testing will become impossible because of the complexity of the state management of your Singletons. Finally, Singleton have some unique behavior making them best pattern for some need. To know if I should use a Singleton I ask myself 3 questions : - Do I really need a Single instance class ? - Do I really need to be able to not instantiate it in run time ? - Does the creation of a factory will really be an overkill investment in my program ? Actually I see 2 feature that will very often have those needs : Loggers and seeders it is most of the time the reason why I'm making singletons.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Thanks for sharing.
@jaygarzon6709
@jaygarzon6709 3 жыл бұрын
I am here May 2021 This has to be one of the best real life example of singleton pattern that I saw! Thank you sir Tim!
@tomthelestaff-iamtimcorey7597
@tomthelestaff-iamtimcorey7597 3 жыл бұрын
Thanks for watching
@thesazarconfidential
@thesazarconfidential 2 жыл бұрын
Thank you Tim
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@bitsbytesandlife
@bitsbytesandlife 4 жыл бұрын
That great a explanation of the Singleton Design Pattern. I hope that you are going to do more Design Pattern videos.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Thanks!
@Yanivbd1981
@Yanivbd1981 3 жыл бұрын
Brilliantly explained.
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Thanks!
@UmarAdeeb
@UmarAdeeb 4 жыл бұрын
A great example of Singleton is the Application object in an MVC application.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Thanks for sharing.
@rajputanurag124
@rajputanurag124 2 жыл бұрын
Thanks for the detailed lecture. It is really helpful for me to understand why singleton design is followed in making the server based testing frameworks
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Glad it was helpful!
@stevensmith5626
@stevensmith5626 2 жыл бұрын
Thank you for your teaching. You are a blessing to us all 👍
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@gazar37
@gazar37 4 жыл бұрын
Great example. Your way is the correct way of teaching in my opinion.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Thank you!
@labeh3750
@labeh3750 4 жыл бұрын
Have been struggling with the GOF (obsever ) Design Patterns but your teaching on Singleton has helped my understanding... Thanks Tim
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
You are welcome.
@bartekbratus7415
@bartekbratus7415 4 жыл бұрын
Really enjoyed that one. I'm looking forward to seeing mode Design Patterns videos. As always I learned something new from your videos, thanks for the great content!
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Glad to hear it!
@torrvic1156
@torrvic1156 21 күн бұрын
Thank you for your valuable insight, Tim! You are an inspiration! And I also remember the phrase you quoted (about the cure which can be more dangerous than illness) from old and famous French book by De Balzac called Lost Illusions where Eva Seshar told it to her lawyer offering her some weird solutions.
@IAmTimCorey
@IAmTimCorey 18 күн бұрын
You are welcome.
@Xpert599
@Xpert599 7 ай бұрын
All is well when I see my search result returns with a Tim Corey explanation!
@IAmTimCorey
@IAmTimCorey 7 ай бұрын
👍🏻
@gami195
@gami195 4 жыл бұрын
Nice video. Looking forward to other design patterns on Gang of Four.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Thanks!
@seethablegalzoom
@seethablegalzoom 3 жыл бұрын
Great Explanation! I did the same thing in my career using Singleton on scoping globally, Clearly see those differentiation explained this video.
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Glad it was helpful!
@Osman-eo1lx
@Osman-eo1lx 4 жыл бұрын
Thanks for the video. I would expect to hear some stuff about thread safety in your code.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
I'll be covering thread-safety specifically in an upcoming video.
@gazar37
@gazar37 4 жыл бұрын
@@IAmTimCorey I will be waiting for that :)
@farizmamad
@farizmamad 3 жыл бұрын
Thanks for showing this kind of tutorial in concious way
@fcdarjan
@fcdarjan 2 жыл бұрын
Crazy how my teacher tried to explain this and i just couldnt manage to conprehend it. Now watching this, it makes al sense immedistely..
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Great!
@jonatancordoba7984
@jonatancordoba7984 4 жыл бұрын
19:34 This made me click in the sense that we never talk about how to recover the state of a program when it crashes due to, let's say, a power loss... when there's no chance to cancel tasks, close connections and so on. Would be a nice topic to cover.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
I will add it to the list. Thanks for the suggestion.
@mangesh4231
@mangesh4231 3 жыл бұрын
Hi Tim, I have gone through a couple of vedios you made on design principles. Found all of them more explanatory and very useful. Thanks a lot for this.
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
You are welcome.
@jfbarbosaboro
@jfbarbosaboro 4 жыл бұрын
Awesome lecture! Thanks for this!
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
You're very welcome!
@denizebassifigueiredo3706
@denizebassifigueiredo3706 Жыл бұрын
Hey! Nice video, you are a great teacher! After some search about the topic, yours were the best explanation! Thanks a lot!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@denizebassifigueiredo3706
@denizebassifigueiredo3706 Жыл бұрын
@@IAmTimCorey Let me tell you, thanks to your videos, I was able to pass in a job interview, I'm going to keep studying your content. I also intend to buy your courses as soon as I can. Again, THANKS! Please keep your great work!
@saintinel
@saintinel 4 жыл бұрын
This is the best explanation i have ever seen about singletons, thnx!
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Glad it was helpful!
@lynndemarest1902
@lynndemarest1902 3 жыл бұрын
Simply excellent. Thank you.
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
You're very welcome!
@devgenesis6436
@devgenesis6436 4 жыл бұрын
Cool video..i love these design pattern series..what bout some microservices and gateway videos for future
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
I will add it to the list. Thanks for the suggestion.
@vssrikanthgarnepudi5952
@vssrikanthgarnepudi5952 4 жыл бұрын
Finally, you did it.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Yep.
@apoleyta9760
@apoleyta9760 2 жыл бұрын
Awesome tutorial. Really helped a lot. I'm gonna watch your dependcy injection tutorial next.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Great!
@kajalmishra7367
@kajalmishra7367 4 жыл бұрын
I have been looking forward to this video. Big thanks, Tim!
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Hope you enjoyed it!
@farizmamad
@farizmamad 3 жыл бұрын
Thanks for showing this kind of tutorial obviously
@tomthelestaff-iamtimcorey7597
@tomthelestaff-iamtimcorey7597 3 жыл бұрын
Thanks for watching!
@ignaciodelaparra
@ignaciodelaparra 3 жыл бұрын
Hi Tim, Your videos are AWESOME! I have a question. @~24:00, you mention that turning the class to static would bring other complexities that we might not want. What could those be? Thanks!
@alimustafa2088
@alimustafa2088 4 жыл бұрын
thanks sir, i hope to complete all Gang four and please record Repository Pattern with unit of work
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
I will add it to the list. Thanks for the suggestion.
@saeedehbabaiyan8747
@saeedehbabaiyan8747 2 жыл бұрын
Hello Mr Corey. Thank you so much for this video. Could you please make a video about Abstract Factory Design Pattern?
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/
@srinivastanneru1927
@srinivastanneru1927 3 жыл бұрын
Great Explanation Tim. Thank you!
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
You are welcome.
@refactorear
@refactorear 4 жыл бұрын
Gamma and Johnson (two of the GoF) discussed about this pattern 20 years after the book was released and mentioned they should have reworded the singleton pattern. The way it was presented makes it easy for people to justify making something global which was the main drawback: why have it as a parameter when you can make it global and make the life easier for programmers calling that method. Another drawback is that it makes everything harder to test since you can't stub it directly, you need to wrap it before. Or you need to add a backdoor (like an "internal void overrideServers(List newServers)") to change the internal state of the singleton when testing. On the other side it's useful when dealing with legacy applications where globals are commonly used, you can convert them to singletons (which was the intended way of using it). Personally I rather err on the safe side, I prefer not using it at first but when I detect something is being called way too many times and might be impacting performance (typical example are configurations) I consider using it there.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Agreed.
@thethomasproject
@thethomasproject 4 жыл бұрын
Thanks for the explanation Tim. I initially had to think on the code after seeing it but it makes sense. Kudos on showing the singleton services instantiation in .net core. That's the way to do it there.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Glad to help
@intellisound
@intellisound 4 жыл бұрын
I’ve been looking forward to this!
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
I hope you enjoyed it.
@deepanshukumar3943
@deepanshukumar3943 3 жыл бұрын
I m ready to buy also. But please add more patterns.. awesome..
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
The full course is coming.
@donybg
@donybg 4 жыл бұрын
Another great video! Can we get Strategy or Repository, please? If you make a course on all major design patterns and some other more advanced topics, that would be awesome.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
I will add it to the list. Thanks for the suggestion.
@AlThePal78
@AlThePal78 2 жыл бұрын
I was going to ask too so I am glad you know I actually asked lol
@AM-rl8lv
@AM-rl8lv 4 жыл бұрын
Great video as always!! Keep it up Tim!!
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Thank you!
@artsofsenthu
@artsofsenthu 3 жыл бұрын
Fantastic, thanks for sharing
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
My pleasure!
@tondar2127
@tondar2127 4 жыл бұрын
Wonderfully explained thanks !
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
You are welcome.
@joshuajaworowski4258
@joshuajaworowski4258 4 жыл бұрын
Good tutorial on singletons. I have used them in game design to make inventory systems and never thought of using them for other applications. Will you be going over the observer pattern as well? Would love to see your take on that one
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
It is on the suggestion list.
@Time21
@Time21 4 жыл бұрын
Very helpful Tim. Will this playlist has the facade pattern also?
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
That is on the suggestion list.
@Fergobirck
@Fergobirck 4 жыл бұрын
Thanks! One question though: wouldn't simply changing 'nextServer' to static in the original code achieve the same result (so the counter is actually shared between all the instances)?
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
A static class cannot be instantiated, it cannot implement interfaces, it can't be passed as a parameter, and it cannot be lazy-loaded. You can't change just the method and access the instantiated value. You would need to make everything static to do that.
@Fergobirck
@Fergobirck 4 жыл бұрын
@@IAmTimCorey Thanks for the reply, but I'm not talking about making the whole class static, ONLY "nextServer" field static (i.e. take your code as shown at around 6:45 and only change "private int nextServer = 0;" to "private static int nextServer = 0;"). That way no matter how many TableServers you instantiate, the "nextServer" counter will be shared between all instances and you will always get the next "non-busy" server when calling GetNextServer(), no matter on which instance of TableServer (which is the same behavior of the singleton example).
@tailor9639
@tailor9639 4 жыл бұрын
@@Fergobirck Im just guessing here...in this case it will produce the same result but you will have two instance of Tableserver and then it will not be a Singleton.
@F4ir8or
@F4ir8or 4 жыл бұрын
@@Fergobirck the behavior seems to be the same, I agree. The upside of the singleton however is, that you only have one instance of that class loaded and thus it's a bit more resource friendly.
@rahulmathew8713
@rahulmathew8713 2 жыл бұрын
One might thing, if the intention is to simple create a single instance of a class why don't we just make the class static instead of implementing singleton pattern. First problem is, what if I want a derived class to have a single instance. A derived class cannot be made Static. Plus static class brings other restrictions in certain scenarios and it wont serve the purpose. So we need a different mechanism to create a single instance class, so we use Singleton pattern.
@luismendez9436
@luismendez9436 4 жыл бұрын
excelent tool indeed. Thank you for this.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
You are welcome.
@TheAngelOfDeath01
@TheAngelOfDeath01 2 жыл бұрын
I don't know... I find these videos both education AND entertaining.... but that's probably just the need inside of me, you know? ;-) And yes. Singletons can very much be one soft cushion too far -- you can easily go overboard very badly. As with EVERYTHING pertaining to programming it's all about picking the RIGHT TOOL for the RIGHT TASK and it's very easy to choose something wrong, and this isn't something you can just learn from reading... this is something you learn through experience, through being responsible for projects and following long term project development because you will rarely see the problems with too many Singletons until way down the road. And to use Singletons scarcely is really the best advice: don't use it unless you absolutely have-to.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I'm glad.
@bigs2469
@bigs2469 4 жыл бұрын
My co-workers need to watch this. "Globals are golden!"
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Feel free to share. 😉
@mihaimyh
@mihaimyh 4 жыл бұрын
Hey Tim, will you make a video/series about data structures and algorithms?
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Probably. It is on the suggestion list.
@martink4964
@martink4964 4 жыл бұрын
Personally, I find that the benefits of dependency injection (DI) are greater, than the benefit of real singletons. Real singletons can prevent more usage errors, but DI gives a lot more flexibility, and makes it far easier to switch around between singleton, scoped or transient modes for a given class or service. Another important thing to consider with singletons (both real and DI) is multi-threading. If you're using multiple threads, be sure your singleton is thread safe - otherwise you could easily run into errors, or worse, hard to find/debug race conditions.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Yep, you need to be careful of threading issues.
@godwinyoh3700
@godwinyoh3700 4 жыл бұрын
I see managing database connection information or File IO parameters would be a good use case for this. Thanks for the video, looking forward to the next.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
You are welcome.
@therealgamer8150
@therealgamer8150 2 жыл бұрын
Logging is the classic singleton use case. I think that would have been clearer than this somewhat odd restaurant situation. But still a good video
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Logging isn't a great singleton situation because then you cannot get class-specific information to log. Typically we don't use a singleton for logging. For instance, in .NET Core, logging is set up in dependency inject as transient (one instance per class).
@yorha2b278
@yorha2b278 9 ай бұрын
I use singleton design pattern for system that keeps a state machine for the entire app with additional efforts on thread safety for data access.
@IAmTimCorey
@IAmTimCorey 9 ай бұрын
Thanks for sharing!
@margosdesarian
@margosdesarian 4 жыл бұрын
Explaining how AddSingleton differs from the actual pattern is priceless. Does the runtime then manage the resources of the singleton created using AddSingleton efficiently then, do you know?
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
It does. When the first call comes in, it creates the instance. After that, it just hands out that same instance.
@SyuSept
@SyuSept 3 жыл бұрын
Thanks a lot for this precious video, and for your work overall. Your channel is extremely valuable and I've learnt a lot of things. 5:47 nextServer = (nextServer + 1) % servers.Count; would be, according to me, much more elegant. But I understand that such a demo must remain simple ;)
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Thanks for sharing. Good reminder that there is always more than one way to code something.
@cirezno5960
@cirezno5960 3 жыл бұрын
Thanks a lot Monsieur Tim Corey, I have one question : Suppose that this application is hosted on a IIS Web Server. This means that two users or more calling the server (means more than 2 user sessions) will be using the same TableServers instance ? Thank you.
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
That is correct.
@cirezno5960
@cirezno5960 3 жыл бұрын
@@IAmTimCorey I never seen good YT channel as yours. I wasted so much time those last 7 years... ..I am learning a lot You deserve to have my patreon. thank so much for your quick feedback. Hi from Fr🇫🇷
@Laiteux
@Laiteux 4 жыл бұрын
Hello Tim, thanks for this amazing video, I truly enjoyed it. However, I recently noticed some noises in the background when you're talking, instead of only having your voice clear. Could you please try looking into it? I find it pretty disturbing. Thank you!
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
I do what I can to eliminate background noise.
@bwcbiz
@bwcbiz 4 жыл бұрын
The singleton class also seems like it could be subject to race conditions in multi-threaded/multi-client environments. I assume a "real" implementation where state transition sequence is important would have to include a mutex implementation..
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Yes, you would need to add a locking pattern or something similar to make this thread-safe.
@kitsurubami
@kitsurubami 4 жыл бұрын
"will not get destroyed for the life of your application". What does the application life look like? Like if it were a web-app, does each user create an "application life" when they access the application, and it is killed upon them closing the application? Two application lives can exist concurrently? Sorry if I'm asking an off topic question, but I'm trying to better understand the possible negative side effects of using singleton design pattern. Thank you!
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
For a desktop application, it is as long as the application is open. For a web application, it is as long as the web server is running (technically the website, since you can restart the website without restarting the web server).
@kitsurubami
@kitsurubami 4 жыл бұрын
@@IAmTimCorey Thank you for that clarification Tim. This might be in part why in my organization restarting the web server is a common thing to try when weird issues pop up.
@Antmanzzz
@Antmanzzz 3 жыл бұрын
Whenever you talk about the SIngleton pattern for a mutable class that holds any data, I think it is also really important to mention thread-safe behaviour, such as implementing locking etc. This will definitely catch out someone naively trying to use this pattern in a web application or other highly multi-threaded environment. You could have quickly mentioned this issue and/or done a brief demonstration of lock in your GetNextServer() method (which is currently not thread-safe). Otherwise a great video!
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Good point.
@sunnyb7302
@sunnyb7302 2 жыл бұрын
15 mins in the video, you mentioned it's lazy initialisation. Isn't it eager instead of lazy? The static initialisation happens the moment the application is run..
@jasrobsny
@jasrobsny 2 жыл бұрын
please do an Angular example project to showcase what's important.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I haven't covered Angular up to this point. I've got a ways to go before I consider it.
@zulhishamtan4263
@zulhishamtan4263 3 жыл бұрын
Thank you very much for the valuable information. By the way, what is the difference between static class and singleton.
@MrLazyleader
@MrLazyleader 3 жыл бұрын
Hey Corey, I am looking for a way to update my Binding of a static property, if it changes. I read that singletons can help, but I don't understand in what way. I still have to tell the code when the property changes. If I create a normal property get the static property value, INotifyPropertyChanged doesn't work since this only works with SetProperty and there is not GetProperty.
@eslamalshiki2650
@eslamalshiki2650 4 жыл бұрын
Thanks for this, please discuss CLR & managed code and protect the dll to be deassembly
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
You are welcome.
@leahnjr
@leahnjr 2 жыл бұрын
Is it lazy loaded or is it loaded at runtime? I think to lazy load it you need to call it in your GetTablesServers method if it hasn't been instantiated.
@meirkr
@meirkr 4 жыл бұрын
Great video! I just think there should have been some statements about the disadvantages of Singleton. Like testability concerns, direct access from any/to any place which is a risk for spaghetti code, etc. I highly recommend not to use this old singleton pattern. Di really solves those things.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Singleton does have its uses but I did caution against overuse in this video.
@arkadiusz6688
@arkadiusz6688 4 жыл бұрын
MR. Tim, awesome as always, you'reb eing my programming mentor! My question now is, why in your HTTPClient video you haven't had used the singleton pattern? If you had used it, then we could have the only one instance of HTTPClient and we wouldn't have to have the static one.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
I didn't want one instance of the HttpClient. I wanted the HttpClientFactory to handle instances so that it would properly close them down and recycle them. This is safer and cleaner.
@ariesmontesa8435
@ariesmontesa8435 2 жыл бұрын
Very informative video. I just want to ask what is the better way on writing a report. I have a web API that going to write a report and save to disk when completed. This reporting ulility should be available anywhere so I need a single instance. My problem is what if another client request the service, will they use the same instance?
@pylvr8021
@pylvr8021 4 жыл бұрын
thanks tim , can we expect a xamarin tutorial this month ?
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Not this month but it is high on my priority list.
@lifeinloop
@lifeinloop 4 жыл бұрын
I have one doubt Tim. You said at 12:05 that if we have or need the list somewere else and make sure those list are sincronized that wouldn't be possible this way. Well, just make the list object declaration public and then call it like so: Program.hostlist.GetNextServer(); Is there any problem with that?
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Yes, you cannot pass that class around like an object in your application. That limits how you use this class.
@lifeinloop
@lifeinloop 4 жыл бұрын
@@IAmTimCorey i don't understand how it limits. I do that and it works. And now i discovered that i can use things like lock and Semaphore to control thread access, wich i think was causing me a crash in my application. To me, a static variable like this is the same as a thread safe singleton. Maybe that's because i'm new at this but i don't see the diference..
@jaymuraleedharan
@jaymuraleedharan 4 жыл бұрын
It's great that you are getting into the Gang of Four design patterns. Please keep going. But is the TableServers example a true "singleton" implementation? It doesn't prevent the users from still creating separate instances. I think a Singleton class should not be allowed to be used any other way. So you have to lock down the true instance by making the constructor private.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Yes, we have locked down the constructor. You cannot instantiate a new instance of TableServers.
@ArkamitDutta
@ArkamitDutta 4 жыл бұрын
Thanks for the lesson!!! Just a quick question to understand it better, would be useful to have Dispose() for the instance in the finally block to kill the instance everytime it finishes the execution?
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
You only need to implement IDisposable and/or Dispose of the object if you have something open that you need to close down properly (database connection, memory stream, etc.). In our case, we have nothing that needs to be closed down in a specific way. Objects themselves don't need to be disposed on their own.
@shaswatkumarparida6179
@shaswatkumarparida6179 3 ай бұрын
But why use static before the tableservers object creation in program class..is it necessary?
@IAmTimCorey
@IAmTimCorey 3 ай бұрын
Yes, it is necessary. The reason it is marked as static is because we want to have only one instance of TableServers. That instance gets created once, and then it will be just referenced from then on.
@verttix
@verttix 4 жыл бұрын
This is pretty cool. Thanks for sharing this. I would love to see something on SignalR, and more specifically how to handle SignalR in a load balanced environment.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
I will add it to the list. Thanks for the suggestion.
@fahadmohammad4482
@fahadmohammad4482 3 жыл бұрын
What is the benefit of using HttpClient as a singleton? I've seen that it is being used in many projects but didn't get the "WHY".
@daorsislami9080
@daorsislami9080 2 жыл бұрын
Hi Tim, hope you're doing ok, I wanted to ask you so by all this information is it safe to use SIngleton Design Pattern for seeding data for example city names so that we don't have to do a request in the database each time we want to get data? Thank you!
@bsarge
@bsarge 4 жыл бұрын
Thanks for the video! Singleton has given me a lot of freedom making a bunch of similar utility classes that can implement an interface (which can't be done with static classes, I learned). One question: Does the positioning of the static implementation line matter? Could it be at the bottom of the code block for instance?
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
I am not sure what you mean by static implementation line. If it is a method or constructor or property, no, position does not matter.
@hqcart1
@hqcart1 4 жыл бұрын
Can you explain what is the main difference advantage/disadvantage between singleton and static class?
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
I will add it to the list. Thanks for the suggestion.
@jacobphillips9235
@jacobphillips9235 4 жыл бұрын
Nice pattern! Great demonstration! Later on in the video, when you discuss Dependency Injection... would it make sense to load an app's configuration as a DI'd AddSingleton() or AddSingleton()?
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
I prefer to use the interface so that it can be more easily replaced.
@janne_kekalainen
@janne_kekalainen 4 жыл бұрын
Bother, Only 360p at the moment, guess I'll have to come back later.
@MCmotorsports12
@MCmotorsports12 4 жыл бұрын
If the video is in 360p it is likely youtube is still rendering the video. It is not the resolution the creator chose.
@LittleCD
@LittleCD 4 жыл бұрын
The new "First" comment lol
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
That's just KZbin not updating the video. I had it uploaded for weeks but evidently it waited until it was public to process the rest of the formats. It is 1080p now.
@wiepcorbier
@wiepcorbier 4 жыл бұрын
Thanks 👍
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
You are welcome.
@pilotboba
@pilotboba 4 жыл бұрын
Is there any need to make the nextServer field thread-safe. What happens if two threads call into GetNextServer at the same time?
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
You would implement a locking pattern (a future video).
@ibrahimicgili4254
@ibrahimicgili4254 3 жыл бұрын
Bro thank you...
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
You are welcome.
@danielheydt9864
@danielheydt9864 4 жыл бұрын
Great topic. Thanks for the video. I am wondering, aside from the "lazy loading" aspect of your solution what would prevent you from simply making the class static? A static class cannot be instantiated and there would only be one for the entire application. It seems to have the same result. In my case, I am thinking about configuration values that need to be loaded when the application starts and are used throughout the entire application.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
A static class cannot be instantiated, it cannot implement interfaces, it can't be passed as a parameter, and it cannot be lazy-loaded.
@romedov882
@romedov882 4 жыл бұрын
Thank you for the video! Would it be a good practice to use The Singleton pattern alongside Entity Framework for a database context class?
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
No, that probably isn't a good idea.
@yop6356
@yop6356 4 жыл бұрын
@@IAmTimCorey why?
@maelvathk
@maelvathk 4 жыл бұрын
Hi Tim. Is it good a idea to have a winform as a Singleton?. I need an instance per winform in my application so I was thinking on using this pattern.
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Typically no, because you want to interact with each instance of the WinForm as if it were unique. If you want them all to be the same, though, a Singleton would work.
@aimanalnami7655
@aimanalnami7655 Жыл бұрын
hi tim ' Is the viewmodel the same Builder design pattern thanks
@yop6356
@yop6356 4 жыл бұрын
Tim used the Dolorean and uploaded the video like in 2008 ^^ 360p style
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
That's just KZbin not updating the video. I had it uploaded for weeks but evidently it waited until it was public to process the rest of the formats. It is 1080p now.
@yop6356
@yop6356 4 жыл бұрын
@@IAmTimCorey thanks :) (for this answer and all of your videos)
@АлексейОрёл-к3у
@АлексейОрёл-к3у 3 жыл бұрын
Hi, Tim! Thank you so much for your efforts! You have a talent for teaching! However, I have problems with downloading the code from some of your video lessons, including this one. before I was able to download the code without any problems, but now an email just stopped sending and they are not in spam too! please take this into account! Best of luck! Sincerely, Alex
@IAmTimCorey
@IAmTimCorey 3 жыл бұрын
Send an email to help@iamtimcorey.com and Tom will get you helped out.
@АлексейОрёл-к3у
@АлексейОрёл-к3у 3 жыл бұрын
@@IAmTimCorey I will! Thank you for your quick feedback, Tim:)
@dyakobaram
@dyakobaram 4 жыл бұрын
hi Tim, I was trying to learn ASP.NET core but there isn't enough tutorial for version 3.1, so my question is, is it ok to watch tutorials below that version like 2.X ? do those version close enough or should I keep looking for version 3.1?
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
It depends on the video but you should be mostly fine with 2.1 videos.
@dyakobaram
@dyakobaram 4 жыл бұрын
@@IAmTimCorey thank you
@sergestevens6034
@sergestevens6034 4 жыл бұрын
Hey Tim, great video as always ! I'm just wondering whether this solution is a correct way of creating a Singleton : 1. Make the private static readonly field public and call it Instance as if it was a property. 2. Remove the method GetTableServers. Now you can call GetNextServer() by calling TableServers.Instance.GetNextServer() instead of TableServers.GetTableServers().GetNextServer() Is this still a valid singleton pattern ?
@sergestevens6034
@sergestevens6034 4 жыл бұрын
Correction : replace the field by a full property with a private set
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
It would technically work but it would not read as cleanly.
@sergestevens6034
@sergestevens6034 4 жыл бұрын
@@IAmTimCorey "Des gouts et des couleurs..." :-) Thanks for your answer, Tim !
@LindenMikus
@LindenMikus 4 жыл бұрын
if host1 and host2 each get the next server async, would you recommend a thread lock in the get next server method or would you prefer something else?
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
Yes, probably a thread lock.
@reodanoe-jankolar1410
@reodanoe-jankolar1410 4 жыл бұрын
Is the method to get the instance of the singleton necessary or would it also be ok to just make the instance public?
@IAmTimCorey
@IAmTimCorey 4 жыл бұрын
You want to control how the instance is returned so that you can, among other things, ensure that the user does not overwrite or change the instance.
@reodanoe-jankolar1410
@reodanoe-jankolar1410 4 жыл бұрын
@@IAmTimCorey How can the instance be overwritten if it's marked as readonly? Unless there's something I don't know about readonly fields which is possible...
Factory Pattern in C# with Dependency Injection
1:07:33
IAmTimCorey
Рет қаралды 107 М.
Остановили аттракцион из-за дочки!
00:42
Victoria Portfolio
Рет қаралды 3,5 МЛН
Самое неинтересное видео
00:32
Miracle
Рет қаралды 2,9 МЛН
Singleton Design Pattern in C# - Do it THAT way
13:15
tutorialsEU - C#
Рет қаралды 27 М.
Singleton Pattern - Design Patterns (ep 6)
20:09
Christopher Okhravi
Рет қаралды 249 М.
10 C# Libraries To Save You Time And Energy
33:59
IAmTimCorey
Рет қаралды 207 М.
"Stop Using Singletons in .NET!" | Code Cop #009
13:52
Nick Chapsas
Рет қаралды 76 М.
5 Design Patterns That Are ACTUALLY Used By Developers
9:27
Alex Hyett
Рет қаралды 276 М.
Inheritance vs Interfaces in C#: Object Oriented Programming
49:41
IAmTimCorey
Рет қаралды 103 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 822 М.
Остановили аттракцион из-за дочки!
00:42
Victoria Portfolio
Рет қаралды 3,5 МЛН