Boss: My wife is cheating on me Programmer: One man's constant is another man's variable....
@ChristopherOkhravi4 жыл бұрын
🤣🤣
@ionichi4 жыл бұрын
'The Variable Man' - by Philip K. Dick
@sourabhkhandelwal6894 жыл бұрын
Is this lose?
@bazejtez66354 жыл бұрын
once a variable, always a variable :D
@renarsdilevka65734 жыл бұрын
@@bazejtez6635 Unless you garbage collected :D
@mohammadnaseri50457 жыл бұрын
One of the best series I have ever watched. Keep going on dude!
@ChristopherOkhravi7 жыл бұрын
mohammad naseri thank you for the kind words. I'm glad it's useful :)
@mikhailbisserov80176 жыл бұрын
That's a good video. Especially the code smell part. I would add though that to ensure single instance you also need to wrap instance creation with lock (Monitor), because nowadays threads are used left and right. In fact, "double-checked locking" (that's a pattern) should be used if (instance == null) { lock (_lockObject) { if (instance == null) { instance = new Singleton(); } } }
@GohersWay4 жыл бұрын
Sorry for asking such a newb question but what is purpose of locking here.
@mikhailbisserov80174 жыл бұрын
@@GohersWay Locking prevents execution of the enclosed code by multiple threads. We want "instance = new Singleton()" execute only ONCE in entire process lifetime and for that we need to make sure that inner "if (instance == null)" is never evaluated SIMULTANEOUSLY (but theoretically it can happen more than once, if two threads happen to access the singleton instance for the FIRST time simultaneously). Now, why do we need outer "if (instance == null)" - because it's much cheaper than lock in terms of CPU. If a web application runs for 30 minutes before process recycling, then 99.9% of instance access cases by different threads will go no further than outer if, 0.09% will create monitor, wait on that monitor, and evaluate inner if, and 0.01% will actually execute inner if's enclosed code (instance = new Singleton).
@good_life_videos2 жыл бұрын
@@mikhailbisserov8017 but even if two threads access that part of creating the object, why should it be a concern ? considering, first instantiation call is just necessary (but state will always be random, so hardly matters who creates it finally or been changed by some other threds at same time)
@mikhailbisserov80172 жыл бұрын
@@good_life_videos if two threads create instances of the object, it's not Singleton by definition. WHY we want to create it only once is an out-of-scope question. It's a given task, purpose of Singeton. Usually it's for performance and memory conservation, maybe even acquiring lock to some unique underlying resource, but again, it's out of scope.
@Tapuck6 жыл бұрын
Your editing is absolutely fantastic. The stream of knowledge never stops or slows down.
@IamCagedAnimal6 жыл бұрын
U, my good Sir are born for teaching, really nice and simple explanation for everyone to understand. Keep up the good job! cheers
@vineetherenj31433 жыл бұрын
Me: Let me learn about Singleton pattern. Christopher: never use singleton pattern. Me: -_-
@eugeneganshin29343 жыл бұрын
You can use it tho. In Typescript Nestjs Framework alsmost every class is a singleton and its fine.
@PeterFlemmingNielsen3 жыл бұрын
You should never use it, except when you should. (eg. it is good to prevent database connection leaks)
@AlonGvili2 жыл бұрын
@@eugeneganshin2934 so like redux store is a singleton in a javascript/react world ?
@dalitsobotha79324 жыл бұрын
I love the way you teach. Your explanations are thorough and the way you speak is very captivating. Straight to the point, you are not a time waster.
@timothyjohns87756 жыл бұрын
Not sure about other languages, but in C++, the preferred approach to singletons is known as the Meyers Singleton (named after Scott Meyers who included it in his book Effective C++). The idea is to declare the variable within a method rather than as a member variable. This is thread safe, and it also allows you to skip the check to see whether or not the singleton exists. The code looks like this: class Singleton { private: Singleton() {} public: static Singleton& getInstance() { static Singleton instance; return instance; } };
@yoavmor9002 Жыл бұрын
I don't think intra-method static variable magic exists in non C languages, I think it's mostly a C thing. By the way, I'm assuming you meant static Singleton instance = new Singleton()
@Remist0 Жыл бұрын
@@yoavmor9002 It's not a pointer, so they did not mean that.
@hoelefouk4 жыл бұрын
If there is anything comparable to your teaching skills, it's your skills to have amazing intros. Intro for this particular video is simply genius. Your intros are so underrated man!
@SaurabhGuptaicecool4 жыл бұрын
This series is greatest I've ever encountered on youtube on any topic, period.
@coorfang4264 жыл бұрын
Same here
@ruvishkarathnayake91255 жыл бұрын
The way you teach these lessons is perfect. This is a good example for my lecturers how to teach these stuff.
@aniketb20105 жыл бұрын
“One man’s constant is another man’s variable”... Woow!!
@betterculture7 жыл бұрын
Great video! However, the argument is not that Singletons are necessarily bad except that they are misunderstood, often abused and can introduce unwanted side effects when used without proper thought -- a lot of frameworks use Singletons :D. For instance, I have this piece of (framework) code that manages all of my configurations (and of subsystems) and the construction of this object is really heavy and doesn't even lend itself to a prototype or flyweight. It is important that these bits of configuration happen only once at application startup. Instead, I hide my singleton configuration instance behind an abstraction and delegate the construction and life cycle to a DI / IOC container. In other words, users only have to depend on that abstraction e.g. IConfigurationManager etc. In addition, the one thing (or two) I think you're missing is the issue of potential race conditions or thread-safety in concurrent systems when handling stuff like this manually -- why it's important to use a good DI framework and instead depend on abstractions. Plus, it is your responsibility to ensure that your object is IMMUTABLE upon construction and member access is read-only.
@jaycelila62587 жыл бұрын
don't understand why this video is not popular than it has to be. You are amazingly well explaining abstract concepts so dumb like me can understand. Thanks.
@karzo2223 жыл бұрын
You are criminally underrated. Deserves more followers. Top notch.
@ckmedia61205 жыл бұрын
This is why I love the internet, good videos with good info. My man!
@ariaien86374 жыл бұрын
Singleton design pattern is simple, however, complicated! every line of the code was explained thoroughly and beautifully.. well done! One of the best videos ever for Singleton!
@rusiraliyanage66433 жыл бұрын
Hey SIr , I am Sri Lankan IT Undergraduate , you do explain these design patterns very well and Thank You So much. It was really helpful me to figure out the answers for the design patterns questions in our university :)
@sammclaren6965 Жыл бұрын
That's what I call 'in-depth explanation'! Awesome work.
@CognitiveSurge7 жыл бұрын
Love these videos, thanks Christopher! The Head First series is great.
@ChristopherOkhravi7 жыл бұрын
Karl Hadwen thanks! I'm very glad they're useful. Thanks for watching :)
@gurituna7 жыл бұрын
Excellent i have a java architecture certificate exam within 3 months. You made me clear with the design pattern. Many thanks. Bless you
@ChristopherOkhravi7 жыл бұрын
+gokhan tuncel I'm glad to hear. Thank you very much for sharing and best of luck on the exam :)
@sibinM113 жыл бұрын
this guy and explanation is pure gem
@prapulkrishna4 жыл бұрын
You are one of the best teachers on the planet. Thanks for the videos.
@kensearle48927 ай бұрын
Thank you! Good points. The only time I have used a Singleton is when I was developing my own small website, wanted a quick cache, and only needed to load the database once at application startup to save on Cloud Database DTU hits. The webpage did load faster using the Singleton as a cache. A static class also worked for this scenario and is a little more straightforward. Either was ok for this scenario because I only changed the data about once per month, could restart the application when I wanted, and didn't have sessioned users on the site yet. However, once I want to add/change data in the database and want to reset the cache without an app restart, it is a dead-end; so it is not practical for many business production scenarios. I was just looking at 5-6 ways to cache and those were the quickest but most limited.
@clementtytube3 жыл бұрын
First time seeing your videos... I give a 10000 likes for your cuts and edits. Your videos are full of info (no lags and time waste). That itself proves you value your trade.. Kudos
@oussamaml41616 жыл бұрын
i got a UML exam tomorrow , and i've been watching your video , your videos are saving my life right now
@N1ghtR1der6664 жыл бұрын
definitely a very simple to understand explanation, thank you. I would as "a novice" suggest there are legitimate uses for singletons in game development.
@PatrickMagToast2 жыл бұрын
Could you provide an example, now that you've grown another year? :D I mean ofc there are things like "auto-saves" you need only once, but would there be a need to access them globally? It prevents people from doing stupid things but at the same time allows them to do other stupid things.
@kirubababu71273 жыл бұрын
The way you teach is fantastic, you are my inspiration.
@vyaasshenoy18845 жыл бұрын
Your explanation is clear and precise. I enjoyed the video and was able to understand the nuances of this pattern very well.
@luli8297 жыл бұрын
Already learned this in university and now planning to review again, my professor who teaches that course is very good, and your lecture is as good as the course I learned before ! Thank you so much ! Waiting for more
@sumitkapadia15097 жыл бұрын
The way you jump entered, I expect to hear "Hey Vsauce" :P . Awesome video.
@m41kdevelops415 жыл бұрын
Great videos. I just have an opinion and feel free to correct me; I believe that the Singleton pattern could be used for things that we only need one of such as database connections for example. So throughout the application's run-time, I don't think we'll need to change or re-establish the connection to the database we're using ( unless it is necessary for some reason of course ) so I think it's best to only allow one instance of the db connection class and each time we request an instance ( potentially a new connection ) we just send the existing connection back. please feel free to correct me if i'm wrong.
@houidimohamedamin47472 жыл бұрын
but don't you use different databases for dev and testing ?
@lyusienlozanov16327 жыл бұрын
Everything makes sense with that kind of explanation! Thanks, Chris!
@ChristopherOkhravi7 жыл бұрын
Thank you for the very kind words. Much appreciated and I'm glad to hear it. Thanks for watching :)
@seguncodes31363 жыл бұрын
The best explanation of Singleton pattern i have come across
@sushantkumar18453 жыл бұрын
The best playlist (on IT/Sw) I have ever watched on KZbin
@DAS-jk3mw Жыл бұрын
This is the best explanation of a Singleton pattern ever !!
@jvsnyc2 жыл бұрын
The Clean Code links are all great! Now, back to your videos! I wound up learning as much from this as any of your others in this playlist, thanks to the excellent links provided in the description. Singletons seem very convenient, until test-time...
@emskiee53305 жыл бұрын
I love Singleton in Unity. Very handy for Manager objects.
@TheLucausi4 жыл бұрын
It could become your worse nightmare xD
@emskiee53304 жыл бұрын
@@TheLucausi I've actually seen it unfold before me when the project I am working on became large. The Manager became this class with lots of states. Singleton, despite being great for Manager objects, should still be used sparingly :D
@IntelliAbb7 жыл бұрын
As much as we try to avoid Singletons, we do run into scenarios where we have to use one. Specially when working with native Android using Java where for creating Fragments, we use a public Instance property to get access to it. These Instances are then held in memory for various operations done by the OS. Great series, keep it up :)
@glenndify17 жыл бұрын
you have really simplified design patterns ... classic explaination ... thank you very much
@ChristopherOkhravi7 жыл бұрын
+Glenn Dsilva Thanks for letting me know! :) And thanks for watching!
@rustemduran42482 жыл бұрын
NIce man, totally grasped for the first time, thank you so much.
@user-sh9bj8qz3j7 ай бұрын
A clear video on single ton design pattern. Thank you.
@johnyjohnjohny80147 жыл бұрын
This is quality man. Keep the good work, youtube definitly needs that. Well done
@ChristopherOkhravi7 жыл бұрын
+JohnyJohn Johny Thanks! And thanks for watching :)
@abhisheksaxena5183 жыл бұрын
In some cases it is actually good to use singleton pattern, infact kotlin has a class type "object" just to make it easier for developers to work around singleton pattern. The use cases where I have used it are db calls and api calls, in a way they do make sense. Think about like this, there could be different threads making calls to the db for different crud operations, and by using singleton pattern we can make sure that the information that we get at any instance is correct until modified by the single owner(modifier), this also helps with locking mechanism that dbs have to prevent inconsistent data and conflicts. I agree with you that forcing a single instance may not be the viable approach but it can be seen as a preventive approach in this use case. Please correct me if I am wrong or incorrectly using this pattern.
@debaratimaiti2570 Жыл бұрын
I'm thinking using dependency injection is good for your case. Check out springboot or guice
@jphvnet Жыл бұрын
A factory could do the job, thus it is transparent how the instance is created (only one, decorated, etc.). Using DI of course.
@slobodanmikaric21806 жыл бұрын
Singleton can be used in notification servers for mobile devices or others, when you always have one and only one instance to call to add or remove observers. :) thx Christopher Okhravi for clearing this out!
@gavinmcgregor32197 жыл бұрын
Just wanted to let you know that you are amazing for making this series!
@ChristopherOkhravi7 жыл бұрын
Thank you. You peeps are amazing for watching it :D
@uzil245 жыл бұрын
Thank you for this video and all the rest. I really hope to find every single Design pattern explained by you in this play list.
@shleym5 жыл бұрын
Thank you. One note: singleton should be created in synchronized method, otherwise you could ended with more then one different instance of your singleton in different threads.
@ChristopherOkhravi5 жыл бұрын
Or it should simply not be used at all 😋😉 Thanks for watching!
@blasttrash4 жыл бұрын
@@ChristopherOkhravi What about a Database class? Why need two instances? We can be 100% sure that our app will only need single instance in this case right?
@ChristopherOkhravi4 жыл бұрын
blasttrash There’s always a second case: the testing environment 😊 Id recommend Misko Heverys clean code talks on KZbin if you want to dig into this a bit more. But ofc its not all black and white and a database is as you say a pretty good candidate if you only interact with the DB singleton in predictable places such as controllers. If you call the database from lots of places in the app then we’re back to the testing problem :) And yes ofc there are solutions to test mocking singletons but that’s another story 😊
@PedroCouto19822 жыл бұрын
@@blasttrash, I'm working professionally with several databases in a single REST server application. They're different databases for optimization reasons. If your program might grow and you force it to have a single database, your program might not be scalable.
@blasttrash2 жыл бұрын
@@PedroCouto1982 that makes sense. But I think several databases in a single Rest server is an antipattern afaik. Not sure what optimization you are talking but most of blogs online say to have single database for most of typical rest apis. Only when you are doing some complex routing, sharding or dealing with different kinds of data(in which case might be better to have separate microservices anyway) do you need multiple databases.
@gargkapil7 жыл бұрын
Hey Christopher it was a nice video but i have couple of question please try to answer them whenever you get time.. 1. what is the main difference between singleton and static class about which developer usually don't know. 2. Under which scenario we should use static class and when should we use singleton pattern. 3. if i have a abstract class having all abstract method and I have an interface, then which one i should choose and why?? I have couple of more questions which i ask you once i will get the clean and deepest answer of these question.
@taki5baj4 жыл бұрын
As I saw your video I get the feeling that I never ever understood clearly the Singleton design pattern. Your video catched me like 'Yeah, this stuff doesn't do much more than a static class.' Then my brain went on fire, I started thinking about this. Then I came up with a 'reason': A static class's constructor (type initializer) can't have any parameters in it's constructor. Singleton pattern can have any parameters, that the constructor might need in the getInstance() method. 1: Simply initialize an object, with the given parameters 2: Simply override the fields to the parameters. The only use-case with a Singleton pattern I can think of is like focus on a UI element (or GameObject if we are talking about a game). Just pass the object as a parameter and the singleton might change the color of the passed object to red, so only the element will be in focus. Sorry if this is wrong, I'm still a beginner in programming. C# example: public interface IFocusable { Color ObjectColor{get;set}; } public class Button : IFocusable { public Color ObjectColor{get;set;} public Button(){.....} } public class FocusSingleton { public static FocusSingleton instance; private IFocusable focusObject; private Color oldColor; private FocusSingleton(IFocusable objectToBeInFocus) { focusObject = objectToBeInFocus; oldColor = objectToBeInFocus.ObjectColor; objectToBeInFocus.ObjectColor = Color.Red; } public static FocusSingleton getInstance(IFocusable newObject){ if(instance == null) instance = new FocusSingleton(newObject); else{ focusObject.Color = oldColor; oldColor = newObject.ObjectColor; newObject.ObjectColor = Color.Red; } return instance; } } If this would be done with a static class, then we should check @ every object dependent method if the focusObject is null. With Singleton the checkings are eliminated.
@stefanoforgiarini3396 жыл бұрын
Excellent explanation Cristopher! Only one thing to note: what happens in a multi-threaded environment? It may happen that two different threads call the getInstance() method and because of this, more than one instance of the Singleton class is created. The only way to ensure a single instance is to add the synchronized in the getInstance method signature or (it would be better), synchronize the block of code in which we check if the instance is null (the if statement). Congrats for your videos!
@Death8ball7 жыл бұрын
Fantastic series. When I get my first dev job I will do what I can to support you.
@heshamabood4723 жыл бұрын
Singleton pattern is useful in a situations like when need create a logger for the application, this case required only one instance to be instantiated and used by multiples modules
@EngSamieh3 жыл бұрын
In general, most HW devices don’t allow concurrent access, Singleton pattern is essential to provide the SW interface that encapsulates the protection for the HW device.
@mehakjain53847 жыл бұрын
One of the best video series I have ever seen. Superb explanation. Quite like the way you do these videos in your own style. Keep up the good work. P.s. loved ur writing and honesty :P
@ChristopherOkhravi7 жыл бұрын
Thanks! I'm glad to hear that the style works :) Thank you for the encouragement and for watching the series :)
@abhishekpawar84586 ай бұрын
Thanks for this amazing video. One correction I would suggest is to make the static getInstance method synchronized. In the world of multi-threading, if two threads access the getInstance method, they will end up with 2 different instances
@liveyounganastasia Жыл бұрын
I love how you explained this. Very clear. 👏
@VaibhavJain4 жыл бұрын
Liked the way you describe different aspects of decision making and assumptions related to pattern
@AVBFANS7 жыл бұрын
great video. finally I found explanation in a style that I can understand clearly. Thanks for the lesson. I am now a subscriber.
@ChristopherOkhravi7 жыл бұрын
+AVBFANS Awesome! Welcome to the channel :) And thank you for sharing your thoughts :)
@naffiahanger93162 жыл бұрын
Your explanation of concepts are easy to understand 👏👏👏👏👏
@thomas29575 жыл бұрын
You're views must really go up every December/January. Exams time and your explanations are great, thanks man
@biswarup0776 жыл бұрын
Great video. One suggestion: if you would have included multi threaded environment that would have been great. In many interviews this is asked.
@pervanadurdyyeva19203 жыл бұрын
Simplest and clear explanation I've come across. Thanks
@gg4eva7632 жыл бұрын
Oh my god, you are a good explainer! I'm actually understanding what's happening
@RmDIrSudoSu3 жыл бұрын
Singleton are useful for some stuff. I agree that most application singleton are not good. But for example, on rendering engines, we usually wants to open only one graphic context for rendering, so this makes sens to make a singleton for it. On windowed applications we would also make a singleton on the main window to avoid to be able to have multiple instance of the same main window in the same program. So here we either use functional programming or OOP with singleton. There are couple of things that I don't really understand, for example you say new to create an object. This is not to create the object but to instantiate a space into the heap for the object. Static methods are also basically doing some functional programming in OOP language like java. And I don't completely agree with that statement that it should be avoided. For a lot of application where you don't care about optimization yes, but if you're aiming at performance when you check what the compiler does with the OOP design, there are times where it is not well optimized at all, so functional programming makes sense to optimize part of a program, or a whole program. Sometime it is still useful to do some part in assembly due to some space limit or to lower the CPU usage or the number of instruction per cycle, but this is embedded systems and low level system programming only. There is that weird thing that in C/C++ a lot of really well know programmer will prefer functional programming over OOP at all cost, because you've more control over what and how the compiler will optimize it. Personally I use both OOP and functional together, there are time where OOP makes complete sense and able me to still have the same performances. Usually when you check the compiled code afterward, the compiler just took your object strip them of everything useless and inline nearly everything, that's when OOP is nice, but once it start not doing those inlining and you can get quite a messy and unoptimized code. But it is probably me who is too deep into the low level programming... I know that most application don't need that level of optimization, but I still disagree that static or functional programming is bad, because it is still far better for optimization than OOP, using OOP when it is needed and it adds something nice without crippling the performance is good but I don't like using it everywhere.
@sachinkainth95083 жыл бұрын
Very great video. Thank you for this. One thing that is missing is a discussion about a thread-safe version of the Singleton pattern using double check locking or some other mechanism to achieve thread-safety.
@xavmanisdabestest3 жыл бұрын
I think in a game manager would be the perfect time to use this pattern as you only ever have one instance of a video game so it could hold useful functionality there, but just like you said one mans constant is another mans variable.
@mark71663 жыл бұрын
One valid reason you might want to use a singleton would be in an app where a lot of lookup data is shared throughout. It makes sense to have a single instance of a class which loads the data once the first time it's needed, and then makes it available in all subsequent places without having to make another call to the database. Other than that, there certainly doesn't seem to be many reasons you'd want something like this.
@cunami27 жыл бұрын
Amazing work on patterns, reading book is fine, but all those extra explanations are just making everithing perfectly fit in place. Wish you could finish rest of book till monday when i have test out of all patterns from book ^.^ Any way, keep it up (sharing videos with rest of Uni, everyone is gratefull)
@ChristopherOkhravi7 жыл бұрын
+cunami2 thank you very very much for sharing the video :) Increasing views and comments like yours are of course things that keep me going :) :)
@sarahlee6976 Жыл бұрын
BEST VIDEO EVER!!!!! So much better than university lectures
@Wwise_sounds2 жыл бұрын
SNHU is using this video as material reference. you should feel proud that an university is using your videos to teach new students :)
@rahulkmail3 жыл бұрын
Explained the matter nicely.
@radekp98517 жыл бұрын
if one is a religious fanatic and assembles a God class, singleton pattern seems like an obvious choice :)
@ChristopherOkhravi7 жыл бұрын
+Radek P This comment is quite simply hilarious :D :D
@pradeeph20007 жыл бұрын
Radek P LOL!!! For Indian Hindu fanatic it's probably a fly weight pattern with millions of fly weights
@timmyers97986 жыл бұрын
Unless you have more than one god, then you have to refactor the whole universe.
@Quynn-Oneal5 жыл бұрын
have you ever heard about Polytheism?
@uzil245 жыл бұрын
don't you mean polymorphism? ;-)
@priyankmungra293 жыл бұрын
Thank you for making this series ❤❤❤
@DominikRoszkowski7 жыл бұрын
Thanks, your explanation was clear and convincing :)
@ChristopherOkhravi7 жыл бұрын
Dominik Roszkowski thanks for sharing and for watching :)
@zolisawelani93383 жыл бұрын
Super awesome mate. Clear explanation.
@abdullahkhaled61623 жыл бұрын
I'm a beginner so take my words with a grain of salt, I think singletons are very convenient in the case of database connection instances, you always want to have a single instance of that thing per database, to put it in a more abstracted way in my opinion singletons are useful whenever context needs to be shared. But again I may be completely wrong so feel free to correct me and discuss the topic
@monjasonsteng78615 ай бұрын
This is perfect. Thank you so much.
@rajeshrathod93297 жыл бұрын
Very nice and detailed explanation of design pattern concepts.... I am waiting for other remaining design patterns videos ..... Thank you very much.
@always90s7 жыл бұрын
Thanks Christopher! I will definitely check out those links. On to the next pattern :)
@ChristopherOkhravi7 жыл бұрын
Hitesh Rana cool! They're worth the time. Misko seems like a very smart guy. Thanks for watching!
@edudestroer4 ай бұрын
Thank's for all the Knowledge🤜
@abeplus73523 жыл бұрын
I use Singleton's in facade patterns . Ie if you want to package your service layer . Or if you have to orchestrate alot of services talking to each other . Singleton helps bring the memory footprint down . However I don't leave the constructor as private. I just add a static mehod that says getinstance and basically document that for live applications we should use this.
@yasienaboelkheer96305 жыл бұрын
it was previously recommended to use a static class (singleton) for some scenarios eg. "having a helper public class which provides data access functionality to the whole application". and of course new practices have already deprecated such approach . in my opinion i still find it very productive for some contexts
@exoticcoder53653 жыл бұрын
Gooooooooood one !!!! Very clear ! Clear all my concepts !
@linux_lectures2263 жыл бұрын
I will give an example when I used singleton I was working on on arm m0 processor The adc measure external voltage I created adc as singleton where I configurations for adc with dma and this ensure only one time configuration is done Only one instance of adc is important more instance will create problem because adc is a single hardware with multiple channel So i use adc function this way Adc::getInstance() ->getAdcValue(channelNumber)
@KartikSharma16077 жыл бұрын
I was waiting for this pattern. Already knew the basics. Wanted to know the threading aspect here and how to implement using Enum :(
@ChristopherOkhravi7 жыл бұрын
Sorry about my being able to accommodate :/ I guess my opinion on how to make it thread safe is to simple not use it, since it’s inherently unparallelizable. Yes there are workarounds but it would be better to use an abstraction that supports parallelization, i.e. rethink the problem :) Thanks for watching and for your feedback though! :)
@houssemzaier7 жыл бұрын
Good explenation. Singleton should be used in its place it's helpful... not for as a globalState as it is had to test, difficult in concurrency . can be avoided by IoC containers that can provide single instance of an object.
@sshanzel4 жыл бұрын
This is actually useful on my end. Using .Net Core with its "appsettings.json" for Configuration, it is intuitive that it will never change throughout its life since you have to hard-code the changes on the file itself first. So i don't think it hurts to identify those items.
@alexgarces14007 жыл бұрын
I think there are exceptions where the Singleton Pattern is actually very handy. Just look at Redux, one of the most popular libraries right now, uses the Singleton Pattern in React.
@ChristopherOkhravi7 жыл бұрын
Very interesting point! Thank you very much for sharing. I assume you refer to the store? If I'm not mistaken, Redux asks you to only create one store and argues that when using Redux it simply only makes sense to have a single store. They however do not make it impossible for you to create a second store. See: github.com/reactjs/redux/blob/master/docs/api/createStore.md. The wording is "there should only be", not "there can only be". The redux store thus falls under the category "it's ok to only ever have a single instance of something but it's not ok to make it impossible to create a second one" (paraphrasing Misko Hevery). I.e. not a Singleton. Rather it's what Misko calls a singleton with a lower case s. A single instance. Again, very interesting point though. And please do say so if I misunderstood what you meant. When trying to determine whether something is a singleton or not in a particular code base, you can look at whether it is "passed around" or whether it is "requested" by running some static method. If it's the former it's probably not a singleton, but if it's the latter it most likely is. On a different note. Since JavaScript could be considered a functional language we have to be careful when applying OO rules. I need to think about this a bit more but Singleton-pattern-like implementations are probably not as problematic in FP since in FP you could actually put the method call getInstance in a variable and pass that around (functions are first class citizens). Which means that we can actually replace the "static" call to getInstance to a call to another getInstance2 method at runtime, which means that we're not "forced" into using that single instance. This is not the case for OOP. In OOP we achieve dynamic dispatch through polymorphism, and that's the problem of e.g. Singleton pattern (but more generally static methods). See: kzbin.info/www/bejne/oJXSqZuYfruAeLM Pardon me for the long answer but thank you for the interesting comment :D
@matthewsmeets5 жыл бұрын
@@ChristopherOkhravi So going by that, a "service container" (a list of service instances, created by constructing services, passing services as arguments (depinjection) to other services) would not conform the the Singleton principle because 1. it does not enforce a single initialization 2. it is an instance being passed around i.e. via dependency injection
@nathalievanbellegem5313 Жыл бұрын
thank you sir, my teacher is making my class create a random number generator between 0 and 1000 without repeats using a singleton and i'm going to be honest his explenation was just confusing. The assignement is still making me want to cry but now it's because i'm pretty sure he just hates us but at least i kind off understand singleton now.
@romanworker9954 жыл бұрын
Totally agree with you. Single Responsibility is broken with Singleton Pattern. Why? There are three responsibilities here: 1. Create only one object. 2. Provide global access for it. 3. Provide own Behavior for this class.
@mhshbht127 жыл бұрын
very good explanation of Singleton. Thanks Christopher
@piyush92664 жыл бұрын
Thanku so much sir... what a beautiful explanation.sir if possible make builder design pattern.i want this.plz i shall be grateful for this kind act.
@ramiroblanco77895 жыл бұрын
I write automated integration tests using Selenium WebDriver. Usually in my tests I want to make sure that I am always using the same instance of driver (e.g. WebDriver driver = new ChromeDriver()) Wouldn't this be a good use of the singleton pattern? What would be a drawback to this?
@asifnisarr2 жыл бұрын
Use Factory method to give you object of each browser😊 Use singleton for global configurations (url, environment, browser name, etc…)
@praveennampelly35592 жыл бұрын
Superb man, you explained very well. I really got lot of interest on design patterns. Thank you so much I leant a lot from your video's.
@Mari99Hamida Жыл бұрын
Clearly explained ! thank you .
@irshuck7 жыл бұрын
Woooow, Thanks christopher for this wonderful series. Your teaching style is awesome. waiting for other design patterns :)
@ChristopherOkhravi7 жыл бұрын
+Irshad ck Thank you. Makes me glad to hear. Next one is coming very soon. Thanks for watching :)
@Dotto193 жыл бұрын
For once I have understood the concept of singleton pattern. Thank you dude!! My question is, in what circumstances do we actually need to use singleton pattern?
@redz59203 жыл бұрын
I saw that you have to use it for implement services that have an identical working on the application. For instance you juste need one instance for handle configuration page of an application and you can call it from everywhere. But I'm not sure at 100%, I'm learning too.
@karthikkeyan44603 жыл бұрын
Just so we are clear on all levels, the demonstrated example isn't thread safe, which mean we can have multiple instances of the Singleton class by using different threads. You need the "lock"ing object to secure the thread
@chukwujiobicanon961 Жыл бұрын
The Singleton pattern has use cases just like every other pattern. It’s best to choose according to use case. Python Flask Micro framework and C++ Crow Micro framework make use of this pattern to create an app object and have one and only one app object. Again, use what suits best.
@sakshisharma70977 жыл бұрын
Dude ! am waiting for command design pattern :) you are awesome , i just love the way u make everything easier and understandable :)
@ChristopherOkhravi7 жыл бұрын
+sakshi sharma thanks! :) I'm glad you're following the series. Thanks for watching and for the comment.