munificent / game-programming-patterns

Source repo for the book
http://gameprogrammingpatterns.com/
Other
4.03k stars 495 forks source link

Command pointer #334

Closed Aleksluciano closed 5 years ago

Aleksluciano commented 6 years ago

In code:

class InputHandler { public: void handleInput();

// Methods to bind commands...

private: Command buttonX_; Command buttonY; Command* buttonA; Command* buttonB_; };

Why buttonX_ knows that your execute method is jump(), if it ins't a JumpCommand pointer? When this was delegate to it? Sorry for my ignorance.

wake-0 commented 6 years ago

buttonX_ does not know which method (jump, run, etc) is called it is just a reference to some command. This could also be a RunCommand, FireCommand, etc. The reason why buttonX_ does not need to be of type JumpCommand* is inheritance.

You know that each command which could be referenced by Command* buttonX_ is of type Command*. So each reference object must be at least of type Command and this implicitly means every referenced object supports an execute method and this execute method abstracts the real command (run, jump, etc.) behaviour.