Тёмный

Design Patterns - Command Pattern Explanation and Implementation in C++ 

Mike Shah
Подписаться 20 тыс.
Просмотров 21 тыс.
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
►Git Repo: github.com/MikeShah/DesignPat...
►Lesson Description: In this lesson I teach you one of the most powerful behavioral design patterns, the command pattern. This pattern models objects to act as a command, and it is one of the most convenient ways to store a series of 'commands' or actions that happen throughout your program. I will show you a use case of using the pattern to store commands as a queue, and this pattern could be used to elegantly implement and undo/redo system.
Free resource: gameprogrammingpatterns.com/c...
00:00 What are Design patterns
00:30 Behavioral Design Patterns
1:00 Command Pattern
1:30 Command Patterns uses
1:59 Design Pattern Resources
2:50 Conceptual Understanding of command pattern
4:00 A game controller example
4:40 Utilizing inheritance for is-a relationship
5:20 Key idea of what a command does
5:50 C++ explanation of virtual
6:30 Enqueing commands in some data structure
7:40 Undo commands in our history
9:10 Starting our command pattern in C++
10:15 Creating our command class
10:45 Creating our interface member functions
11:25 A simple use case for our command pattern
12:40 Creating a Move, which is a type of Command
13:50 Showing how pure virtual functions must be implemented
15:00 Creating a character
15:45 Passing our character as an argument
17:02 Preparing our character to move
18:25 Adding some output to see what is going on
20:00 Our characters first command
20:40 Storing multiple commands in a queue
23:00 Managing lifetime of our commands with pointers
24:40 Undoing our commands
25:30 How do we know it is working?
26:00 Storing position data for our character in move
29:35 Verifying our execute and undo work
31:30 Using a different data structure to perform undo
32:12 Final code walk through
34:10 Closing
►RU-vid Channel: / mikeshah
►Please like and subscribe to help the channel!
►Join our free community: courses.mshah.io/communities/...

Наука

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

 

21 июл 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 43   
@citiesinspace4864
@citiesinspace4864 Год назад
I've been slowly combing through your C++ playlists, and I've been learning so much! I like your teaching style a lot. Thanks for the great content! Excited to see the channel grow :-)
@MikeShah
@MikeShah Год назад
Cheers, thank you for the kind words! More to come!
@StevenMartinGuitar
@StevenMartinGuitar 2 года назад
Enjoyed this, do more! If you had 500+ people in a room listening to you teach you'd feel the power. Views are no different.
@MikeShah
@MikeShah 2 года назад
Thank you for the kind words! More on the way :)
@mudkiptheengineer5339
@mudkiptheengineer5339 Год назад
After rewatching it and as an advice for anyone starting on the topic I would recommend to think about using a range for loop for running the commands, and even though smart pointers are mentioned I think considering type aliases together with the semantics of unique_ptr could be something to take into consideration. (using Commads = unique_ptr --->std::vector instead of std::vector ) great vid once again overall
@alrutto
@alrutto 9 месяцев назад
I just found your channel and I must say the content is great! Everything really well explained. Thanks for sharing.
@MikeShah
@MikeShah 9 месяцев назад
Cheers, thank you for the kind words!
@peach1721
@peach1721 2 года назад
Hey Mike, I really like your videos and the way you explain the general concepts of the different patterns, before taking a deep dive in code examples. Most video covering these kind of topics lack a conceptual overview and rely solely in "explanation by example". I also would be interested to see some approaches how to combine different pattern, because they're often used in combination. Maybe, you would be interested in making a video about this topic some day? :)
@MikeShah
@MikeShah 2 года назад
Thank you for the kind words! Noted the request!
@pedrolobo9835
@pedrolobo9835 Год назад
Another great C++ tutorial video! Thanks for sharing it.
@MikeShah
@MikeShah Год назад
Cheers!
@bruno-zl9qn
@bruno-zl9qn 3 месяца назад
Thanks for this serie Mike. Just starting it. Ambition is to code your examples in vlang. Is it correct to say that this pattern is what’s implemented in an event loop, in SDL for instance?
@MikeShah
@MikeShah 3 месяца назад
Cheers, very cool! SDL probably just uses a union of structs for events, but same idea indeed of pushing events to some queue 🙂
@DeadlyShadow7
@DeadlyShadow7 Месяц назад
So by handing the Character as an argument, you essentially need different command classes for different signatures. If you'd want to have a command queue that could handle different sorts of commands, you'd have to use variadic functions in the Command class, right? Or storing arguments in the command I guess (like with functors) and hand them to the Move class instead of the execute function. Thats probably easier.
@MikeShah
@MikeShah Месяц назад
Using a variant or indeed using a union with 'commands' would also be another strategy. The polymorphism here has the advantage of type-safety, and restricts us (which may be good) to using just 'execute' and 'undo' -- or otherwise as you have suggested, sending in as a parameter to 'execute' a functor() or std::function to otherwise change behavior further.
@VoidloniXaarii
@VoidloniXaarii Год назад
Fascinating! Thanks yet again!
@MikeShah
@MikeShah Год назад
Cheers!
@deepikagoyal07
@deepikagoyal07 Год назад
hardly anybody bothers to give book references. I feel sad when i watch any good video. Its so important to acknowledge the sources of information. Thanks Mike!!
@MikeShah
@MikeShah Год назад
Cheers! Thank you for the kind words
@ByChris
@ByChris 21 день назад
Amazing explanation! Thank you
@MikeShah
@MikeShah 20 дней назад
Cheers, thank you for the kind words!
@AlbertRyanstein
@AlbertRyanstein 2 года назад
I noticed you didnt do many patterns, just to say this was helpful to me, theres a lot of videos but not loads in c++, particularly in their title
@MikeShah
@MikeShah 2 года назад
More on the way :) The factory method is next up!
@AlbertRyanstein
@AlbertRyanstein 2 года назад
@@MikeShah sometimes you gotta get a load of videos up and the exposure comes later. put the word gaming in your titles lol. or game making.
@MikeShah
@MikeShah 2 года назад
@@AlbertRyanstein More C++ videos on the way! :) Thanks again for your support!
@julyordinary7538
@julyordinary7538 7 месяцев назад
Mike, why compiler complained about move being created over stack and pushed over vector? Why is breaking here? 23:07
@MikeShah
@MikeShah 7 месяцев назад
The below would be okay, because we know exactly how much memory to allocate for 'Move'. For 'Command' it's an abstract class, at the top of the error messages, the compiler warns us it does not know how to create a vector of an abstract type. Move m; std::vector test; test.push_back(m);
@markbennett1237
@markbennett1237 8 дней назад
I really enjoy watching your videos. For this pattern, I was curious about parameters. In your Move example, the x and y positions are randomly generated in the execute method, but what if you wanted these values to be specified in the main function? Would they be pass into a Move constructor, or is there a better way to do this?
@MikeShah
@MikeShah 6 дней назад
Correct, the move object itself could have a constructor to populate these values. In this way, you try to keep your classes simple, in that they are again just implementations of 'Command' interface, and you just have to construct the command and push it into the queue :)
@raghul1208
@raghul1208 Год назад
awesome playlist
@MikeShah
@MikeShah Год назад
Cheers! Enjoy!
@mudkiptheengineer5339
@mudkiptheengineer5339 Год назад
So the Action design pattern would be like a message queue in a way?
@MikeShah
@MikeShah Год назад
Correct, it's not a bad idea to queue up the commands and then execute them. 'Event Queue' or 'polling' are probably the search terms to find out more in other libraries related.
@Mzkysti
@Mzkysti Год назад
Hi, thanks, great video and nice link for the book. I didn't understood why you couldn't push "Command"'s into the vector but "Command*" was OK but maybe that is another story?
@MikeShah
@MikeShah Год назад
Taking advantage of polymorphic behavior by using 'Command*'. Meaning that anything that 'is-a' 'type of' Command in the inheritance hierarchy, during run-time the correct member functions are called for whatever type of Command* is called.
@sugoilang
@sugoilang Год назад
I have the same question here, why does compiler not allow us to push Command object into the vector, but ok to a Command pointer. Maybe because Command is a pure virtual Class? Also, I got "invalid new-expression of abstract class type ‘Command’" error which is different from "static assertion" in the tutorial when I'm trying it. ( I have no clue of the reason behind it)
@sugoilang
@sugoilang Год назад
It turns out different g++ version result in error difference, thx for the informative lesson
@_w62_
@_w62_ 7 месяцев назад
@@MikeShah Having command as the base class with everything virtual is to support polymorphism. I may create, say, Teleport, inherited from Command. So at run-time, Command.execute() can be Move.execute() or Teleport.execute(), depending on the run-time context. Please correct me if I am wrong.
@MikeShah
@MikeShah 7 месяцев назад
That's the idea!@@_w62_
@bsdooby
@bsdooby Год назад
Robert Nystrom's book is really great
@MikeShah
@MikeShah Год назад
I read it online, then I bought a copy as it truly deserved a place on the bookshelf.
@bsdooby
@bsdooby Год назад
@@MikeShah Has a place on my bookshelf, too ;)
@binhnguyenthanh5084
@binhnguyenthanh5084 Год назад
seem you computer have a little lagging :)
Далее
skibidi toilet 76 (full episode)
08:11
Просмотров 11 млн
His reaction 😳 (via @kaitlyn.b0506/TT) #shorts
00:10
WHY did this C++ code FAIL?
38:10
Просмотров 225 тыс.
how Google writes gorgeous C++
7:40
Просмотров 822 тыс.
5 Design Patterns That Are ACTUALLY Used By Developers
9:27
SINGLETONS in C++
19:16
Просмотров 196 тыс.
ОБСЛУЖИЛИ САМЫЙ ГРЯЗНЫЙ ПК
1:00
Красиво, но телефон жаль
0:32
Просмотров 1,3 млн