Purpose: The intention behind this code is to have multiple applications that each implement an Actor. Each application should be responsible for defining which actions the given Actor should implement and how they should be implemented. The main() is an example of how an application might define Actor behavior.I am looking for overall feedback regarding this pattern. Does it have a specific name? It seems to resemble the delegate/callback/handler model I remember using back in C#.Otherwise, I am looking for specific feedback regarding my use of std::function, is that the right use for it? How about std::map to deduce the specialized handler? In this example there are only 3 action types but I plan on having about 15.Finally, are there any changes you would make to optimize the code? Such as applying some sort of tempalting in order to reduce code bloat? Perhaps switching to a generic callback so that my Actor class doesn't end up having 15 different callbacks that need connecting?#include <functional>#include <iostream>#include <map>#include Action.h#include Distance.h#include DriveParameters.h#include Speed.h#include Style.h#include SwimParameters.h#include Vehicle.h#include WalkParameters.hclass Actor {private:    std::function<bool(const Vehicle&, const Speed&, const Distance&)> driveCallback;    std::function<bool(const Style&, const Distance&)> swimCallback;    std::function<bool(const Speed&, const Distance&)> walkCallback;    bool driveHandler(const Action&);    bool swimHandler(const Action&);    bool undefinedHandler(const Action&);    bool walkHandler(const Action&);    std::map<Action::Type, bool(Actor::*)(const Action&)> ActionToHandler = {        { Action::Type::UNDEFINED, &Actor::undefinedHandler },        { Action::Type::DRIVE, &Actor::driveHandler },        { Action::Type::SWIM, &Actor::swimHandler },        { Action::Type::WALK, &Actor::walkHandler },    };    bool driveHandler(const Action& action) {        if (!driveCallback) {            return false;        }        DriveParameters parameters = std::static_cast<DriveParameters>(action.getParameters());        if (!parameters) {            return false;        }        return driveCallback(parameters.getVehicle(), parameters.getSpeed(), parameters.getDistance());    }    bool swimHandler(const Action& action) {        if (!swimCallback) {            return false;        }        SwimParameters parameters = std::static_cast<SwimParameters>(action.getParameters());        if (!parameters) {            return false;        }        return swimCallback(parameters.getStyle(), parameters.getDistance());    }    bool undefinedHandler(const Action& action) {        return false;    }    bool walkHandler(const Action& action) {        if (!walkCallback) {            return false;        }        WalkParameters parameters = std::static_cast<WalkParameters>(action.getParameters());        if (!parameters) {            return false;        }        return walkCallback(parameters.getSpeed(), parameters.getDistance());    }public:    void connectDriveCallback(std::function<bool(const Vehicle&, const Speed&, const Distance&)> callback) {        driveCallback = callback;    }    void connectSwimCallback(std::function<bool(const Style&, const Distance&)> callback) {        swimCallback = callback;    }    void connectWalkCallback(std::function<bool(const Speed&, const Distance&)> callback) {        walkCallback = callback;    }    bool performAction(const Action& action) {        (this->*(ActionToHandler[action.getType()]))(action);    }};bool driveCallback(const Vehicle& vehicle, const Speed& speed, const Distance& distance) {    std::cout << Vehicle =  << vehicle << , Speed =  << speed << , Distance =  << distance << std::endl;}bool swimCallback(const Style&, const Distance&) {    std::cout << Style =  << style << , Distance =  << distance << std::endl;}bool walkCallback(const Speed&, const Distance&) {    std::cout << Speed =  << speed << , Distance =  << distance << std::endl;}int main() {    Actor actor;    actor.connectDriveCallback([&](const Vehicle& vehicle, const Speed& speed, const Distance& distance) { return driveCallback(vehicle, speed, distance); });    actor.connectSwimCallback([&](const Style& style, const Distance& distance) { return swimCallback(style, distance); });    actor.connectWalkCallback([&](const Speed& speed, const Distance& distance) { return walkCallback(speed, distance); });    WalkParameters parameters;    parameters.setDistance(Distance::METERS, 100);    parameters.setSpeed(Speed::KPH, 5);    Action action;    action.setType(Action::Type::WALK);    action.setParameters(parameters);    actor.performAction(action);}
Call Specialized Callback from Generic Object
c++;c++11;callback