The Singleton Design Pattern - Part of the Gang of Four

Поделиться
HTML-код
  • Опубликовано: 7 сен 2024

Комментарии • 303

  • @mwoe4035
    @mwoe4035 4 года назад +69

    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 года назад +6

      I appreciate the kind words.

    • @Berlm
      @Berlm 3 года назад +1

      You're completely right. Most videos of this quality are locked behind paywalls.

  • @DylanSterling
    @DylanSterling 4 года назад +41

    "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.

  • @officialspock
    @officialspock 4 года назад +46

    Thanks for this, please discuss all the GOF Design Patterns :)

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +12

      I will add it to the list. Thanks for the suggestion.

    • @JOHNSONLOBOlobojony
      @JOHNSONLOBOlobojony 4 года назад +2

      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 года назад +6

      @@IAmTimCorey Yes Tim, we want more design patterns :)

    • @saifilali7221
      @saifilali7221 2 года назад +1

      @@IAmTimCorey is this out there or on your list of things? You are the best instructor.

  • @gmt8336
    @gmt8336 4 года назад +5

    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 года назад +2

      lol, I'm sure you knew more than that but I am glad my videos are helping.

  • @jt.633
    @jt.633 Год назад +2

    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!

  • @mac19999x
    @mac19999x 4 года назад +9

    Going to go through some more patterns? Awesome.
    Damn you're good at explaining these things.

  • @gp6763
    @gp6763 4 года назад +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.

  • @bitsbytesandlife
    @bitsbytesandlife 4 года назад +1

    That great a explanation of the Singleton Design Pattern. I hope that you are going to do more Design Pattern videos.

  • @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!

  • @jonatancordoba7984
    @jonatancordoba7984 4 года назад +5

    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 года назад +2

      I will add it to the list. Thanks for the suggestion.

  • @UmarAdeeb
    @UmarAdeeb 4 года назад +1

    A great example of Singleton is the Application object in an MVC application.

  • @Osman-eo1lx
    @Osman-eo1lx 4 года назад +10

    Thanks for the video. I would expect to hear some stuff about thread safety in your code.

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +8

      I'll be covering thread-safety specifically in an upcoming video.

    • @gazar37
      @gazar37 4 года назад

      @@IAmTimCorey I will be waiting for that :)

  • @bartekbratus7415
    @bartekbratus7415 4 года назад +2

    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!

  • @torrvic1156
    @torrvic1156 2 дня назад

    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.

  • @Xpert599
    @Xpert599 6 месяцев назад

    All is well when I see my search result returns with a Tim Corey explanation!

  • @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..

  • @gami195
    @gami195 4 года назад +1

    Nice video.
    Looking forward to other design patterns on Gang of Four.

  • @gazar37
    @gazar37 4 года назад

    Great example. Your way is the correct way of teaching in my opinion.

  • @labeh3750
    @labeh3750 4 года назад

    Have been struggling with the GOF (obsever ) Design Patterns but your teaching on Singleton has helped my understanding... Thanks Tim

  • @stevensmith5626
    @stevensmith5626 2 года назад

    Thank you for your teaching. You are a blessing to us all 👍

  • @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

  • @refactorear
    @refactorear 4 года назад +1

    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.

  • @alimustafa2088
    @alimustafa2088 4 года назад +2

    thanks sir, i hope to complete all Gang four and please record Repository Pattern with unit of work

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +3

      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.

  • @devgenesis6436
    @devgenesis6436 4 года назад +1

    Cool video..i love these design pattern series..what bout some microservices and gateway videos for future

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +1

      I will add it to the list. Thanks for the suggestion.

  • @farizmamad
    @farizmamad 3 года назад

    Thanks for showing this kind of tutorial in concious way

  • @steve-wright-uk
    @steve-wright-uk 4 года назад +1

    A nice clear explanation. Looking forward to any other design patterns that you might do.
    My on criticism is that I probably would have mentioned about two methods calling the GetNextServer at the same time and getting the same server. I would then mention locking strategies that can be used to avoid this.

    • @IAmTimCorey
      @IAmTimCorey  4 года назад

      Yes, that is something to consider. I will be covering locking in a future video.

  • @Yanivbd1981
    @Yanivbd1981 3 года назад +1

    Brilliantly explained.

  • @saeedehbabaiyan8747
    @saeedehbabaiyan8747 2 года назад +1

    Hello Mr Corey. Thank you so much for this video. Could you please make a video about Abstract Factory Design Pattern?

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +1

      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/

  • @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.

  • @ppcgnamda
    @ppcgnamda 4 года назад +1

    Excellent as always. Please do the rest of the GoF Design Patterns please!

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +1

      I am planning on doing more.

  • @donybg
    @donybg 4 года назад +2

    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 года назад +2

      I will add it to the list. Thanks for the suggestion.

  • @ignaciodelaparra
    @ignaciodelaparra 3 года назад +5

    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!

  • @saintinel
    @saintinel 4 года назад

    This is the best explanation i have ever seen about singletons, thnx!

  • @HowDidThisHappenNow
    @HowDidThisHappenNow 4 года назад

    Nice to see a good Singelton lecture ;)
    Tip: at 4:00 ish you used Ctrl+C and then Ctrl+V, as i recall, you can just place the marker in that line of code and just press Ctrl+D to make a duplicate of that line.

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +2

      I forget that one because I don't use it often enough.

  • @thesazarconfidential
    @thesazarconfidential 2 года назад +1

    Thank you Tim

  • @deepanshukumar3943
    @deepanshukumar3943 3 года назад +1

    I m ready to buy also. But please add more patterns.. awesome..

  • @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.

  • @AlThePal78
    @AlThePal78 2 года назад

    I was going to ask too so I am glad you know I actually asked lol

  • @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.

  • @farizmamad
    @farizmamad 3 года назад

    Thanks for showing this kind of tutorial obviously

  • @bigs2469
    @bigs2469 4 года назад +2

    My co-workers need to watch this. "Globals are golden!"

  • @vssrikanthgarnepudi5952
    @vssrikanthgarnepudi5952 4 года назад +1

    Finally, you did it.

  • @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  Год назад +1

      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!

  • @kajalmishra7367
    @kajalmishra7367 4 года назад

    I have been looking forward to this video. Big thanks, Tim!

  • @martink4964
    @martink4964 4 года назад +1

    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 года назад +1

      Yep, you need to be careful of threading issues.

  • @jfbarbosaboro
    @jfbarbosaboro 4 года назад +1

    Awesome lecture! Thanks for this!

  • @theElectrASiAN
    @theElectrASiAN 4 года назад

    It was very useful to understand Singleton pattern! Thanks Tim.

  • @S3Kglitches
    @S3Kglitches 3 года назад

    You should have mentioned what is the difference between using a static class and the singleton patterned class. From the video it isn't clearly visible but it gets clear when you think about dependency inversion. With static classes, it's not possible to pass instances as interfaces (static classes cannot implement interfaces or participate in inheritance). And that becomes important when doing Dependency Injection.

  • @apoleyta9760
    @apoleyta9760 2 года назад

    Awesome tutorial. Really helped a lot. I'm gonna watch your dependcy injection tutorial next.

  • @joshuajaworowski4258
    @joshuajaworowski4258 4 года назад +2

    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 года назад +1

    Very helpful Tim. Will this playlist has the facade pattern also?

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +1

      That is on the suggestion list.

  • @lynndemarest1902
    @lynndemarest1902 3 года назад

    Simply excellent. Thank you.

  • @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 года назад +1

      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).

  • @Fergobirck
    @Fergobirck 4 года назад +10

    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 года назад +5

      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 года назад +3

      @@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 года назад +3

      @@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 года назад +1

      @@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.

  • @intellisound
    @intellisound 4 года назад +1

    I’ve been looking forward to this!

  • @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.

  • @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.

  • @srinivastanneru1927
    @srinivastanneru1927 3 года назад

    Great Explanation Tim. Thank you!

  • @mihaimyh
    @mihaimyh 4 года назад +1

    Hey Tim, will you make a video/series about data structures and algorithms?

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +1

      Probably. It is on the suggestion list.

  • @AM-rl8lv
    @AM-rl8lv 4 года назад

    Great video as always!! Keep it up Tim!!

  • @kitsurubami
    @kitsurubami 4 года назад +1

    "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 года назад +1

      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 года назад +1

      @@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.

  • @JacobDuenke
    @JacobDuenke 4 года назад

    So excellent! Thanks for another great video, Tim! Turns out I’ve done this myself, and didn’t even realize what I was doing...crazy. Please more design pattern videos!

  • @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 года назад +1

      Yes, you would need to add a locking pattern or something similar to make this thread-safe.

  • @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!

  • @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.

  • @zulhishamtan4263
    @zulhishamtan4263 3 года назад +1

    Thank you very much for the valuable information.
    By the way, what is the difference between static class and singleton.

  • @cirezno5960
    @cirezno5960 3 года назад +1

    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 года назад +1

      That is correct.

    • @cirezno5960
      @cirezno5960 3 года назад +1

      @@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🇫🇷

  • @jasrobsny
    @jasrobsny 2 года назад

    please do an Angular example project to showcase what's important.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +1

      I haven't covered Angular up to this point. I've got a ways to go before I consider it.

  • @luismendez9436
    @luismendez9436 4 года назад

    excelent tool indeed. Thank you for this.

  • @artsofsenthu
    @artsofsenthu 3 года назад

    Fantastic, thanks for sharing

  • @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 года назад +1

      Singleton does have its uses but I did caution against overuse in this video.

  • @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.

  • @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.

  • @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..

  • @Laiteux
    @Laiteux 4 года назад +1

    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 года назад +2

      I do what I can to eliminate background noise.

  • @tondar2127
    @tondar2127 4 года назад

    Wonderfully explained thanks !

  • @eslamalshiki2650
    @eslamalshiki2650 4 года назад

    Thanks for this, please discuss CLR & managed code and protect the dll to be deassembly

  • @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..

  • @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.

  • @aimanalnami7655
    @aimanalnami7655 Год назад

    hi tim ' Is the viewmodel the same Builder design pattern thanks

  • @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.

  • @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?

  • @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.

  • @SyuSept
    @SyuSept 3 года назад +1

    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.

  • @yop6356
    @yop6356 4 года назад +1

    Tim used the Dolorean and uploaded the video like in 2008 ^^
    360p style

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +2

      That's just RUclips 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)

  • @saiashok28
    @saiashok28 2 года назад

    I wish I watched your videos at least last few years ago :( .
    Could you also please suggest good tool for design document? What are the essential applications other than VS a .Net or any developer should have? Please advise.

    • @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/

  • @pylvr8021
    @pylvr8021 4 года назад +1

    thanks tim , can we expect a xamarin tutorial this month ?

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +2

      Not this month but it is high on my priority list.

  • @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.

  • @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 года назад +1

      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.

  • @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).

  • @romedov882
    @romedov882 4 года назад +1

    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 года назад +1

      No, that probably isn't a good idea.

    • @yop6356
      @yop6356 4 года назад

      @@IAmTimCorey why?

  • @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.

  • @janne_kekalainen
    @janne_kekalainen 4 года назад +10

    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 года назад +2

      The new "First" comment lol

    • @IAmTimCorey
      @IAmTimCorey  4 года назад +1

      That's just RUclips 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.

  • @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 года назад +2

      I prefer to use the interface so that it can be more easily replaced.

  • @a99rocker
    @a99rocker 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.

  • @user-mn8yu4sx2h
    @user-mn8yu4sx2h 2 года назад

    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  2 года назад

      Send an email to help@iamtimcorey.com and Tom will get you helped out.

    • @user-mn8yu4sx2h
      @user-mn8yu4sx2h 2 года назад

      @@IAmTimCorey I will! Thank you for your quick feedback, Tim:)

  • @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!

  • @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".