 Let's talk about the Singleton design pattern. So this has kind of been phased out and something better has come up and we're gonna talk about that so What is a Singleton? Well ensures a class has only one instance and provide a global point of access to it Okay, sounds great in theory. Now. How do you actually implement it? And there's tons of ways of doing a Singleton design pattern Most of them and I'm guilty of this myself Most of those implementations are horribly wrong. Why? Think of it in terms of threading You have one instance Global point of access and all these threads are gonna come smashing into this instance and do things with it Well, threads have their own problems. You're gonna have deadlocks race conditions concurrency issues and so on and so forth And we can sit here all day and talk about it So Singleton's been kind of phased out, but definitely feel free to read up on it There's some great examples and Singleton for other languages that are not I Want to say as in-depth or as powerful as C++ Really don't have the same issues that C++ has so for C++ We're gonna use the C++ 11 Magic static and I cannot take credit for this This is Mark Gregory's class and it is the Singleton class and I think it's on my singles, right? Nuonsoft.com. I'm gonna put a link to their blog here This thing is just dead simple. I mean, it's straightforward and we're gonna run it and use it So we're gonna change it around a little bit as you would expect So what are we talking about a Singleton pattern or a C++ magic static? Basic premise here is only one instance of the class. Why sometimes you want to in you know really limit How many instances you have so for example you wanted like a global timer or something like that? Now think of it like a logger You wanted to be able to write to a log, but you have 15 threads Well, obviously you can't open the file in write mode 15 times You'll get locks and race conditions and things like that We're gonna dial it down very very simply make it very simple to understand. We just have a Simple little Q object nothing special in here whatsoever Just created destroyed and then displays a message And we're gonna use this Singleton class now. Where's the magic? It's right here Static T get instance. So it's a templated class and we're saying get instance T static What does that really mean? Static means there's only one instance of this. So it's just going to return that instance. It's incredibly powerful So we have a constructor a deconstructor and then we've got a bunch of operators that are removed This is where the other part of the magic comes in. We make sure that we can't accidentally goof this up When in doubt leave those alone All right, let's jump in here. We're going to Make two functions scope test and Probably help actually made that a function And let's call this void loop test. So what I really have here three different scopes So Let's go down here and let's call these we're gonna say scope test And then I'm going to say loop test and let's go to scope test first Let's say singleton And we're going to give it the my class because this is templated remember We're going to get instance and notice how this is going to return Reference to the my class instance And I'm going to just say set Object name. Let's call this I Need like a really really strong name like Godzilla. I don't know I just I struggled coming up with names anyways, so we can take this global instance here and we can say display From scope test and we can actually just take this. Let's go Starting loop my horrible spelling mistakes. I get too excited and then I type too fast and then everything stops working So let's say finished Loop and I'll just do something really stupid like for it I equal zero eyes less than 10 Let's go ahead and increment. I I mean just something ridiculously mind-numbingly stupid just to show that it's working and Because I just feel like it we're gonna actually Grab this and I'm gonna say hello from Maine and Let's go ahead and go cute info just to show that we can actually work with the actual class not just some weird C++ thing I'm gonna get that object name back that we set in an entirely different scope right up here This is incredibly cool save and run sure enough We have created my class and we set the name to Godzilla and it's from scope test starting and finishing a loop and Then hello from Maine and there is only one Godzilla. So My nickname for C++ magic statics is of course Godzilla because there's one and only one this is extremely cool and extremely powerful and unlike the Singleton design pattern that we've talked about over here. This does not have any of the Baked-in issues with threading because it's gonna return one and only one and a static Voila now comes with the disclaimer that just because It's a magic static doesn't mean it's purely thread safe if you start doing things like Manipulating a value or working with IO or anything like that. You may have some threading issues So you'll have to work with that appropriately based off your specific situation