I wonder why passing a Command object two-level down of a parent-child class won’t work.
Like this (Class A is parent of B and B is parent of C):
Class A (Creates the instance of the Command object that is passed down to B)
+-- Class B ( Pass down the object to C)
+------- Class C ( Callsthe execute() method of the Command object)
Whereas, when the instance of the Command object is created in Class B and the passed down to class C it works fine.
Example:
EDIT:
Example (Hand-written code):
public class A {
private Command command;
private B b;
public A() {
b = new B();
command = new Command() {
public void execute() {
// Do something
}
}
b.setCommand(command);
}
public class B {
private Command command;
private C c;
public B() {
c = new C();
c.setCommand(this.command);
}
public void setCommand(Command command){
this.command = command;
}
}
public class C {
private Command command;
public C() {
}
public void doStuff() {
command.execute();
}
public void setCommand(Command command){
this.command = command;
}
}
Problem was that during the process of passing down, the Command object from Class A is assigned to local Command object of Class B, which is the local Command object of Class B is sent to Class C.