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

Поділитися
Вставка
  • Опубліковано 17 гру 2024

КОМЕНТАРІ •

  • @mlt_sk
    @mlt_sk Місяць тому +1

    Finally a video that breaks this step by step explaining every little detail, great video

    • @MikeShah
      @MikeShah  Місяць тому +1

      Cheers, you are most welcome!

  • @StevenMartinGuitar
    @StevenMartinGuitar 2 роки тому +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 роки тому +1

      Thank you for the kind words! More on the way :)

  • @georgiosdoumas2446
    @georgiosdoumas2446 4 місяці тому +1

    30:35 ,the usage of words "move " and "undo " was confusing for me, but I translated in my mind as (and that is the output that I would rather see) : "move by x and y" and instead of undo : " move by -x and -y ".

    • @MikeShah
      @MikeShah  4 місяці тому

      Since the value of the move that is being written is not what is being output(i.e. c.x and c.y), x and y still need to be negated.

  • @alrutto
    @alrutto Рік тому +1

    I just found your channel and I must say the content is great! Everything really well explained. Thanks for sharing.

    • @MikeShah
      @MikeShah  Рік тому

      Cheers, thank you for the kind words!

  • @julyordinary7538
    @julyordinary7538 Рік тому +1

    Mike, why compiler complained about move being created over stack and pushed over vector? Why is breaking here? 23:07

    • @MikeShah
      @MikeShah  Рік тому

      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);

  • @citiesinspace
    @citiesinspace 2 роки тому +1

    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  2 роки тому

      Cheers, thank you for the kind words! More to come!

  • @rashidsiddiqui4502
    @rashidsiddiqui4502 10 днів тому +2

    Awesome session sir

  • @mudkiptheengineer5339
    @mudkiptheengineer5339 Рік тому +1

    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

  • @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

  • @pedrolobo9835
    @pedrolobo9835 Рік тому +1

    Another great C++ tutorial video! Thanks for sharing it.

  • @peach1721
    @peach1721 2 роки тому +4

    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!

  • @Prash1c
    @Prash1c Місяць тому +1

    What other books are a good reference material book for C++ design patterns such as these to read more about?

    • @MikeShah
      @MikeShah  Місяць тому

      You can seek Klaus Uglberger's software design book.

  • @DeadlyShadow7
    @DeadlyShadow7 6 місяців тому +1

    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  6 місяців тому

      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.

  • @AlbertRyanstein
    @AlbertRyanstein 3 роки тому +1

    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  3 роки тому

      More on the way :) The factory method is next up!

    • @AlbertRyanstein
      @AlbertRyanstein 3 роки тому

      @@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  3 роки тому +1

      @@AlbertRyanstein More C++ videos on the way! :) Thanks again for your support!

  • @bruno-zl9qn
    @bruno-zl9qn 8 місяців тому +1

    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  8 місяців тому +1

      Cheers, very cool! SDL probably just uses a union of structs for events, but same idea indeed of pushing events to some queue 🙂

  • @markbennett1237
    @markbennett1237 5 місяців тому

    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  5 місяців тому

      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 :)

  • @ByChris
    @ByChris 5 місяців тому +1

    Amazing explanation! Thank you

    • @MikeShah
      @MikeShah  5 місяців тому

      Cheers, thank you for the kind words!

  • @jeffsad8391
    @jeffsad8391 Місяць тому +1

    I have a question:Is good to learn first about cpu,ram,hdd,ssd basics and then move to C++?

    • @MikeShah
      @MikeShah  Місяць тому

      Most folks learn the language and build some software first, and then in order to write very efficient/fast software, it is good/necessary to know hardware.

    • @jeffsad8391
      @jeffsad8391 Місяць тому +1

      @@MikeShah but is a good choice with learnning C++?

    • @MikeShah
      @MikeShah  Місяць тому +1

      @@jeffsad8391 Yes, because C++ is a systems language, it goes well with learning hardware :)

    • @jeffsad8391
      @jeffsad8391 Місяць тому +1

      @@MikeShah i m intersted like with software of operating system and AI

    • @MikeShah
      @MikeShah  Місяць тому +1

      @@jeffsad8391 Great, you're in the right place!

  • @VoidloniXaarii
    @VoidloniXaarii Рік тому +1

    Fascinating! Thanks yet again!

  • @Mzkysti
    @Mzkysti Рік тому +4

    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  Рік тому +1

      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_ Рік тому +1

      @@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  Рік тому

      That's the idea!@@_w62_

  • @bsdooby
    @bsdooby 2 роки тому +1

    Robert Nystrom's book is really great

    • @MikeShah
      @MikeShah  2 роки тому +1

      I read it online, then I bought a copy as it truly deserved a place on the bookshelf.

    • @bsdooby
      @bsdooby 2 роки тому +2

      @@MikeShah Has a place on my bookshelf, too ;)

  • @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.

  • @raghul1208
    @raghul1208 2 роки тому +1

    awesome playlist

  • @binhnguyenthanh5084
    @binhnguyenthanh5084 Рік тому +1

    seem you computer have a little lagging :)