Тёмный

The Observer Design Pattern in C++ - Part 3 of n - Registration and Lifetime 

Mike Shah
Подписаться 20 тыс.
Просмотров 2,2 тыс.
50% 1

►Software Design and Design Patterns Playlist: • C++ Software Design an...
►Find full courses on: courses.mshah.io/
►Join as Member to Support the channel: / @mikeshah
►Lesson Description: In this lesson we are going to refactor our code yet again, this time to more safely register (add/subscribe) and unregister (unsubscribe/remove) observers to their subjects. Note that this lesson builds off of the previous two in this series.
00:00 Introduction
00:45 Recap of the previous lesson.
02:40 Demonstrating the problem with our observers
05:00 Utilizing RAII with our Concrete Observer (Watcher) class to register/unregister
08:14 Creating a test case in our main
09:48 Separating our project into separate files so we have concrete types
13:33 Demonstration of our new Watcher class preventing lifetime errors.
15:10 Review of the key insight from this lesson.
16:24 Conclusion
►RU-vid Channel: / mikeshah
►Please like and subscribe to help the channel!
►Join our free community: courses.mshah.io/communities/...

Наука

Опубликовано:

 

20 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 26   
@user-ql7pw7ld1n
@user-ql7pw7ld1n 3 месяца назад
Very good demonstration Mike. Loved it...to the point and easy to grasp :)
@MikeShah
@MikeShah 3 месяца назад
Cheers!
@thoosequa
@thoosequa Год назад
This would make for a great crossover episode for the SFML series! Integrating the observer pattern into the component architecture, to have gameobjects communicate with each other
@MikeShah
@MikeShah Год назад
Great idea -- The pieces are all here otherwise for what you'd need
@ProrokLebioda
@ProrokLebioda 8 дней назад
Great explanation! I've went with std::shared_ptr and std::weak_ptr on your previous parts, so I'm not sure if I should stick with it or extend using RAII. Smart pointers take care of most of my issues at the moment.
@MikeShah
@MikeShah 8 дней назад
Cheers!
@ssaadhussain902
@ssaadhussain902 Год назад
Thanks man. I'm writing a boost asio based pubsub system at my job and your videos are really helping me. Especially this one. I just wanna ask how much is left now and also if you could recommend me some other resources for the above mentioned task. Thanks.
@MikeShah
@MikeShah Год назад
1 more video recorder, then probably part 5 will wrap things up. I'd recommend looking at other apis (e.g. Java, Dlang, QT) for their apis or signal/slots variation
@bsdooby
@bsdooby Год назад
Forward decl. works with pointers only. And take care of the order of your ctor initializer list (follows class member order, not the order of the args to the ctor).
@MikeShah
@MikeShah Год назад
Correct, only way to know the size of the type if it's a pointer. You are also correct about initializer list order following class order
@bsdooby
@bsdooby Год назад
I think the Observer design pattern is my preferred one. Easy to grasp, and not too difficult to explain. And used anywhere in frameworks with a dynamic behavior. Other DPs loose ground, as programming languages keep evolving and natively integrate these concepts.
@MikeShah
@MikeShah Год назад
Observer is a very useful pattern in software development :) I'm seeing more frequently in languages (Java, DLang, etc.) that these patterns like observer become part of the standard libraries as you mention.
@fishplantsandhamsters8391
@fishplantsandhamsters8391 11 месяцев назад
First of all thank you Mike for these great tutorials! I do have a couple of questions: 1. What if i have to create the watcher object before i can add it to subject's? for sure there are situations where watcher will be inherited by another class and an object of that class will be create before the subject is even created 2. what is the subject is destroyed within the scope? how will the watchers' be notified about this? their destructors are accessing the subject object that may not exist anymore. Apologies for these questions, as I am new to C++. thanks!
@MikeShah
@MikeShah 11 месяцев назад
Cheers. 1. You can set up your code such that a subject can add or 'own' watchers. Some list or vector to keep track of owned watchers may be useful here. 2. If subject is destroyed, you can have it destroy all of the watchers perhaps -- that would be fine.
@fishplantsandhamsters8391
@fishplantsandhamsters8391 11 месяцев назад
Thank you @mikeShah! For item 2, I think i get it. My brain was wrapped with the idea that the watchers class can only be used when it is inherited by other classes that wants to listen to the subject. I realize that is not the case. watchers are just listeners. If they hear something, all they need to do is tell other objects to fire up their event handlers(iff subject do certain something that watcher is listening to). these objects with event handlers do not need to know if the watcher exist. It just needs to be registered to it. if the watcher dies, the objects with event handlers won't care. It just won't be able to fire up its event handlers anymore. Am i on the right direction with these assumptions?
@MikeShah
@MikeShah 11 месяцев назад
@@fishplantsandhamsters8391 Yup, that sounds about right to me! Sometimes folks will store the watchers as weak_ptr's as well so you can check if the pointer is expired.
@SelectsCanneberges
@SelectsCanneberges Год назад
Appreciate the great content and your method of teaching!
@MikeShah
@MikeShah Год назад
Cheers, thank you for the kind words!
@beizhou4025
@beizhou4025 Год назад
There have been a lot of hype about Rust recently. I do not know if I should be a better programmer on C++ or get my hand on Rust? Could you please give me your thoughts on Rust? Thanks😊
@MikeShah
@MikeShah Год назад
I haven't done much Rust programming. Generally speaking, it's probably a good language to learn from if you're going into systems programming. Understanding Rust ownership and the problems it solves will likely benefit your programming skills in other languages like C++ (i.e. in C++ ownership we use std::move and smart_pointers). The advantage with Rust is that it makes many more compile-time guarantees.
@theintjengineer
@theintjengineer Год назад
You didn't ask me, but my answer would be: Be a better C++ Programmer haha If you were coming from OS stuff on C, then maybe I'd say "yeah, ok, you can do that with Rust". But if that's not your case, or if you already started with C++, even if just via a Hello World, then be good at it and be a beast. You don't need to be a **Prof. Zhou Award Winner 5th Galaxy Distinguished Pikadasgalaxias C++ Engineer**. If you know your way around C++, people will always think twice before referring to you, and there's no better hype than that.
@beizhou2488
@beizhou2488 Год назад
@@theintjengineer thanks for sharing your thoughts on this matter on which I completely agree.There has been a lot of hype around Rust lately but I will just ignore then and stick with C++.
@hoangbuihp2974
@hoangbuihp2974 4 месяца назад
Thanks you so much ! By the way , Do you think about making a linux tutorial ?
@MikeShah
@MikeShah 4 месяца назад
Cheers -- eventually that would be a good video to make :) I have a brief video on VM's in linux here: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-ln0bxbDkzPY.html and terminal stuff here: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-pTVxZHWsJnU.html
@michaellundgren6949
@michaellundgren6949 Год назад
if the subject goes out of scope first you will the same issue, right?
@MikeShah
@MikeShah Год назад
Correct -- if subject goes out of scope, we'll want to in our destructor reregister (i.e. just re-assign) the subject. In this example, the subject destructor doesn't do anything :)
Далее
Stray Kids "Chk Chk Boom" M/V
03:26
Просмотров 30 млн
УГАДАЙ ГДЕ ПРАВИЛЬНЫЙ ЦВЕТ?😱
00:14
Cpp2 - First Impression [Programming Languages Episode 27]
1:08:19
APPLE дают это нам БЕСПЛАТНО!
1:01
Просмотров 661 тыс.
iPhone 15 Pro в реальной жизни
24:07
Просмотров 402 тыс.