Design Patterns - Singleton Pattern | Explanation and Implementation in C++

  Рет қаралды 8,122

Mike Shah

Mike Shah

Күн бұрын

Пікірлер: 58
@devsutreja5053
@devsutreja5053 2 жыл бұрын
Best explanation of Singleton ever! Thanks Mike!!
@MikeShah
@MikeShah 2 жыл бұрын
Thank you for the kind words!
@user-ql7pw7ld1n
@user-ql7pw7ld1n 5 ай бұрын
Understood it fully today, after learning about static objects, everything is clear now.. :)
@MikeShah
@MikeShah 5 ай бұрын
Great!
@michaelswahla4927
@michaelswahla4927 7 ай бұрын
Beautiful explanation mike! I had trouble on understanding why people would use Singletons and how static memory worked! Keep up the great work, you are INSANELY underrated! One of the best! :)
@MikeShah
@MikeShah 7 ай бұрын
Cheers, thank you for the kind words!
@georgiosdoumas2446
@georgiosdoumas2446 Ай бұрын
18:50 in the for loop, it would be even better to have it as for(const auto &e : m_messages) since the element is accessed only for reading and not for modifying it!
@ByChris
@ByChris Ай бұрын
Excellent explanation!
@MikeShah
@MikeShah Ай бұрын
Cheers!
@robertstrickland9722
@robertstrickland9722 2 жыл бұрын
Thanks!
@MikeShah
@MikeShah 2 жыл бұрын
Amazing, thank you for the wonderful and generous donation!
@robertstrickland9722
@robertstrickland9722 2 жыл бұрын
@@MikeShah Generous donation for generous (and thorough) coursework!
@MikeShah
@MikeShah 2 жыл бұрын
@@robertstrickland9722 more to come!
@damondouglas
@damondouglas 6 ай бұрын
Thank you so much for this. I experimented with "what if I wanted to replace the Logger* with std::unique_ptr". What was interesting is that I had to declare "std::unique_ptr Logger::s_instance;" outside the class and then "Logger::s_instance = std::make_unique();" inside GetInstance(), returning *s_instance. However, when I removed the declaration outside the class and had "std::unique_ptr s_instance = std::make_unique();" inside the GetInstance(), it instantiated new Logger instances. In your implementation, would you have had to delete the *s_instance somewhere or is this taken care of for us because it's static?
@MikeShah
@MikeShah 5 ай бұрын
Because it is static, it would be cleaned up on program termination :)
@pradeepjagdale7768877650
@pradeepjagdale7768877650 3 ай бұрын
@@MikeShah I feel it will clean the pointer not the object of Logger() as we have used a new Logger()
@Cpp-q5j
@Cpp-q5j 6 күн бұрын
The destructor isn't called at the end, which seems to cause a memory leak. I'm not an expert, but since s_instance is dynamically allocated, shouldn't it be deleted somewhere, perhaps in the destructor? Please correct me if I'm mistaken.
@MikeShah
@MikeShah 4 күн бұрын
Indeed, should probably have a 'destroy' function that is manually called. If we have singletons they should probably be registered so that all of the 'destroy' member functions can be called to make sure they free resources.
@Altekameraden79
@Altekameraden79 10 ай бұрын
Holy smokes, three weeks into learning C++ with only two semester of MATLAB behind me 10 years ago and I guess correctly on static, sort of as I though static_cast would be answer.
@letslearn1703
@letslearn1703 11 ай бұрын
Please do a video on proxy and skeleton design pattern
@carlosrnardi
@carlosrnardi 2 жыл бұрын
Hello Mike, that is a very nice example of it! I have two questions, I understand we need to use mutex to be thread safe specially when we add new text to the log in this case, but why we should use mutex when GetInstance return the address of the object? since static will garantee we only have one copy of it. My second question is about the destructor, I notice it is never called, is this the standard behavior of singleton?, since the object needs to keep availble anytime if needed. Thanks Mike, awesome video!
@MikeShah
@MikeShah 2 жыл бұрын
Hi Carlos, Yes, correct on part one since we have static the variable will only be initialized once, and we'll only have one address returned so no need for a mutex. As for the destructor, static variables last the duration of the program, so the destruction is never called (it's kept available after the first allocation for the lifetime of the program). We could create a helper function to destroy any allocated memory if we truly wanted to, and sometimes folks will write a 'destroy' function, or a 'restart' type of function to reallocate.
@carlosrnardi
@carlosrnardi 2 жыл бұрын
Thanks@@MikeShah for the explanation, Your videos are awesome!!
@MikeShah
@MikeShah 2 жыл бұрын
@@carlosrnardi You are most welcome! Thank you for the kind words!
@_w62_
@_w62_ 9 ай бұрын
@@MikeShah Would shared_ptr be the suitable choices for the 'restart' functions? If not, what would be the recommended patterns for resoure management in the singleton pattern? Thanks.
@MikeShah
@MikeShah 9 ай бұрын
​@@_w62_Depends on what you are doing with the singleton -- in general we try to use unique_ptr, and in your restart() you could 'move' to a newly allocated pointer in 'restart'
@nagenHARP
@nagenHARP 6 ай бұрын
you are genius . 🙏
@MikeShah
@MikeShah 6 ай бұрын
Thank you for the kind words!
@k0185123
@k0185123 6 ай бұрын
amazing!!!
@MikeShah
@MikeShah 6 ай бұрын
Cheers!
@davidpinheiro5295
@davidpinheiro5295 7 ай бұрын
Alternatively you can use "inline" to initialize static member objects inside a class, think this makes it more intuitive!
@MikeShah
@MikeShah 7 ай бұрын
Yes! It actually makes things much more clean :)
@robertstrickland9722
@robertstrickland9722 2 жыл бұрын
I'm having a bit of trouble trying to understand how you can initialize the s_instance on a global scale even though it's a private member of a class.
@MikeShah
@MikeShah 2 жыл бұрын
Couple of videos coming up on 'static' here: kzbin.info/www/bejne/nnrWdmN-nrVke5o and here kzbin.info/www/bejne/f4GmdnV-jMyFqJo (to be released shortly) that I think will help clarify how 'static' variables in classes are shared amongst all instances of a class.
@avinashagarwal3625
@avinashagarwal3625 Ай бұрын
Good explanation. One doubt since "s_instance" is created with "new" keyword. How and where to use the "delete" in this case? Also since the destructor is private will it ever be used in this class?
@MikeShah
@MikeShah Ай бұрын
Once initialized it can effectively live forever an the operating system will reclaim the object. That said, you could add a member function (e.g. void DestroyInstance() or ResetSingleton()) if you wanted to reclaim the memory at some other time.
@bharatpatidar3693
@bharatpatidar3693 2 жыл бұрын
Just Loving it 🙏
@MikeShah
@MikeShah 2 жыл бұрын
Cheers!
@nagenHARP
@nagenHARP 5 ай бұрын
what if i like to use as below : #include #include #include using namespace std; class Singelton { private: vectormessage; Singelton(){ cout
@MikeShah
@MikeShah 5 ай бұрын
That is also fine -- there's a variety of ways to setup singletons with some subtle trade-offs. If you use a pointer, then you only allocate if you use the Singleton for example. This may or may not matter depending on the size of the object.
@letslearn1703
@letslearn1703 11 ай бұрын
Hi Mike I have a doubt , if we define a class member variable, then we need to allocate the memory by declaring the same variable outside class scope. But if we have a static variable in member function of a class we need not to declare the variable out side class for memory allocation. I could not understand how memory gets allocated for it, can you please explain. You have used static variable class function in singleton class design pattern video. Please explain this.
@MikeShah
@MikeShah 11 ай бұрын
I believe the static variables inside of functions are allocated already -- the issue with the static class member variables is they need a concrete instantiation defined somewhere in a file (so the generated .object file knows where to store them).
@letslearn1703
@letslearn1703 11 ай бұрын
@@MikeShah Great video , thanks for your comments
@MikeShah
@MikeShah 11 ай бұрын
@@letslearn1703 Cheers!
@julianbittner4822
@julianbittner4822 2 жыл бұрын
Do you have courses on udemy?
@MikeShah
@MikeShah 2 жыл бұрын
My courses are all slowly migrating to courses.mshah.io
@xinking2644
@xinking2644 2 жыл бұрын
great video, helps a lot !
@MikeShah
@MikeShah 2 жыл бұрын
Thank you for the kind words!
@pedrolobo9835
@pedrolobo9835 Жыл бұрын
Hey! Great video, but I think this code might leak memory, because I didn't see the delete operator anywhere.
@pedrolobo9835
@pedrolobo9835 Жыл бұрын
How about? static Logger& GetInstance(){ static Logger sInstance; return sInstance; }
@MikeShah
@MikeShah Жыл бұрын
@@pedrolobo9835 I should probably delete the pointer, you're right. The trade-off is that. by using the pointer I don't have to allocate ever if I never use the class -- though of course I might cause a memory leak :)
@MikeShah
@MikeShah Жыл бұрын
Using std::unique_ptr would be the right thing to do in an even better implementation of singleton :)
@user-ql7pw7ld1n
@user-ql7pw7ld1n 5 ай бұрын
Understood nothing... after 13:30
@MikeShah
@MikeShah 5 ай бұрын
This video on the 'static' keyword may be useful, as that is introduced at the 13:30 mark :) kzbin.info/www/bejne/nnrWdmN-nrVke5o
@user-ql7pw7ld1n
@user-ql7pw7ld1n 5 ай бұрын
@@MikeShah Hi mike thanks for the video link.. I just saw full video. I am known to behavior of static , so only thing new is "static variables are stored in binary",...Now back to the main thing, in the singleton video, What I didnt understood is that --------> you are making a static instance of the class itself, that too inside the class.. This is really confusing..
@MikeShah
@MikeShah 5 ай бұрын
@@user-ql7pw7ld1n Constructor is private, so cannot be made. The way to get around that is to have a single 'static' (stored in the binary as you said) and you can instantiate that value at compile-time, or otherwise instantiate it in a public member function.
@user-ql7pw7ld1n
@user-ql7pw7ld1n 5 ай бұрын
@@MikeShah ok...got an outline..im studying about "static instance" which is core concept of singleton ig...
SINGLETONS in C++
19:16
The Cherno
Рет қаралды 200 М.
He bought this so I can drive too🥹😭 #tiktok #elsarca
00:22
Elsa Arca
Рет қаралды 56 МЛН
At the end of the video, deadpool did this #harleyquinn #deadpool3 #wolverin #shorts
00:15
Anastasyia Prichinina. Actress. Cosplayer.
Рет қаралды 19 МЛН
5 Design Patterns That Are ACTUALLY Used By Developers
9:27
Alex Hyett
Рет қаралды 270 М.
Singleton Design Pattern | C++ Example
13:24
Portfolio Courses
Рет қаралды 15 М.
Singleton Design Pattern | Implementation with details & code ✌🏻
21:09
8 Design Patterns EVERY Developer Should Know
9:47
NeetCode
Рет қаралды 1 МЛН
He bought this so I can drive too🥹😭 #tiktok #elsarca
00:22
Elsa Arca
Рет қаралды 56 МЛН