I have a tree-like structure of objects. Objects have different types but all of them are inherited from the same base class. Now I want to send some kind of command objects to the nodes. Command object has address of the target node and should be passed through root node and intermediate nodes untill it reaches the target node. Target node accepts command and allows it to perform some actions on the node:
void Node::processCommand (Command& cmd) {
if(cmd.address == this->address)
cmd.execute(this);
else
child[cmd.address]->processCommand(cmd);
}
First question: is it a command pattern, or may be some of it's kind?
Next, consider I have nodes of classes ClassA and ClassB and I want to send some commands to objects of both classes, and some commands only for objects of ClassB, something like that:
class CommandForBoth {
void execute (BaseClass* obj) {
obj->doSomething();
}
}
class CommandForB {
void execute (ClassB* obj) {
obj->doSomethingSpecial();
}
}
Is it possible to implement that without casts and without multiple methods for passing commands of different types, like:
void Node::processCommand (CommandForBoth& cmd);
void Node::processCommand (CommandForClassA& cmd);
void Node::processCommand (CommandForClassB& cmd);