The problem, however, is that the instance methods to do so are in class Game, but the caller is an instance of State, which Game has instantiated
So like
class Game{
State state = new State();
void changeVelocity(int someVelocity){ ... }
}
class State{
//...
//want to change game speed
gameThatOwnsThisState.changeVelocity();
}
You use of parent
and child
here are inaccurate; Game is not the parent, State is not the child. They have a "Game has-a State" relationship, but if they were parent and child in the inherited sense (what OO people think of whenm they see parent/child) then they would have a "State is-a Game" relationship
In short, I think the answer to you question is "no", or "it depends what you mean by 'elegant non cyclic', but probably no".
Simplistically the Game can pass itself to the State when it creates it, and the state then has access to the Game instance on which to invoke the method - i think you'd class this as a cyclic reference/dependency.
You could have some eventing mechanism where State will announce a "WantToChangeSpeed" event that Game will listen to, but that seems similarly circularly dependent because all you've done really is pass a delegate method of Game to State for it to call when it wants to change speed. Or perhaps this satisfies you because it isn't a direct dependency; any method/delegate passed will suffice and State could exist independently of Game
Perhaps if Game is regularly inspecting the state it can read a property of the State and decide to change speed and thus remains "outside looking in" and State is relatively ignorant of everything - it just mutates every tick, however long a tick takes