The Singleton Pattern - Programming Design Patterns - Ep 2 - C++ Coding

preview_player
Показать описание
In this episode we introduce singleton, which is a pattern to help create and manage things in your program while only allowing a single instance.
---
---
Chapters:
0:00 Intro
0:15 Coding, Talking, Theory
28:20 Outro
---
Video Written, Edited, & Produced By:
▶ Matthew Early
---
Thanks for watching! I appreciate any and all support!
---
Some of Matt's Other Stuff (feel free to follow!):

_support:
Patrons help this channel go round and keep creating. If you don't like patreon but still would like to support regularly, you can do a similar subscription via the YouTube Join, or through Github's sponsor button.

This is were I most the majority of the code seen on the channel.

We have a Discord! We welcome all various code and tech chatter.

I stream code and/or gaming every Thursday! I also randomly stream a time or two throughout most weeks.
Рекомендации по теме
Комментарии
Автор

I'm loving your commentary! Thanks for the interesting and in depth content! I really appreciate that you're live coding this. It's entertaining and relieving to see that process take place.

Kshores
Автор

Hi Matt, great vid as always!
Just a few points:
1. You dont actually have to create the singleton on the heap (unlike Java) u can just do:

Manager& Instance() {
static Manager s_Instance;
return s_Instance;
}

This is faster and simpler.

Also, to declare a static class member u have to either initialize it in the .cpp file or declare it with 'inline'.
but in the case of a singleton, it wouldnt be a good idea if u dont want to allocate it on the heap and initialize it with nullptr because contrary to the in-scope static init, static class members are initialized at the start of the program whether the Instance() function is called or not.

Another thing is if u have a getter function for a rather large data member like the teas vector, a good idea would be
to return a const reference to it, that way the user doesn't have to make a copy of it, and they cannot make changes as it is const. (I have to say I disagree with const being pointless. It exists to alert the programmer that specific data shouldn't be changed. If u want to write unsafe code or if u know what ure doing is up to u...)

omermarom
Автор

Really love this series! Something I found helpful was already knowing about the private implementation of the constructor to prevent instantiation; however, the static instantiation was really neat. I often think about static for counters and literally forget you can use statics to have only one of something across all objects. I'm so looking forward to the event system delegation!

reverse_shell
Автор

At 11:55 it looks like Matt probably didn't remember that he could initialize Tea's 3 public data members using aggregate initialization: Tea tea { 1, 2, 3 } ; Since Tea is a POD type (plain old data type it can be initialized using aggregate initializtion syntax: Tea tea{1, 2, 3};
The reason Tea tea(1, 2, 3); didn't work is because Tea didn't have a custom constructor taking 3 integer arguments. Since C++ 20 tea can be initialized using the designated initializer syntax as well:
Tea tea{.cost=1, .strength=2, .quantity=3};

At 19:18 you need to define Manager* Manager::instance{nullptr}; outside the Manager class in one of the translation units (singleton.cpp or main.cpp) or if you have a fairly modern C++ compiler (>= C++ 17 std.) you can declare/define the static Manager* instance variable inside the Manager class by modifying static Manager* instance = nullptr; to inline static Manager* instance = nullptr;

At 22:53 there's an alternative way to initialize the Manager singleton object without constructing it on the heap. It's called the Meyer's singleton pattern. Instead of 'static Manager* instance = new Manager(); return *instance;' you simply write 'static Manager instance; return instance;'. Since C++ 11 initialization of all local static objects is guaranteed to be done in a thread safe manner so you don't need to use a mutex to protect against potential data races (torn read/torn write) when initializating static Manager* instance = new Manager(); unless you separate the definition/default initialization and manual initialization of static Manager* instance with an if condition as show in the following code snippet:
static Manager& Instance() {
static Manager* instance; // it's automatically zero-initialized to nullptr in a thread-safe manner (guaranteed by the C++ 11 standard)

if (std::lock_guard<std::mutex> lg(mu); !instance) // std::lock_guard<std::mutex> protects instance from being potentially initialized by multiple threads at the same time (data race, torn read/torn write) but the whole static Instance() method is not thread safe because access to the Manager& singleton object is not thread safe.
{
instance = new Manager();
}
return *instance;
}

AttilaBudai-nj
Автор

This is used in embedded development, where you usually only have one instance of the hardware, so singleton makes the most sense

FlatFace
Автор

God bless you brother great concept to learn from you

AshishSingh-
Автор

You need to level up your c++ skill. Some people will learn from your videos. After 30 minutes you still have a poor implementation. Using heap and static means no way to test it properly in a framework like Google test. You cannot make two use case tests because you are stucked with the first instance since there is no way to get rid off the instance. The proposed thing with constructor is not great. You always prefer having compile time error than runtime error. People work in a team, you must design your thing by considering that it must be difficult to make mistakes using your code.

trackvegeta
welcome to shbcf.ru