So I have been coding in C# for the past 6 month and I want to go back and finish (update probably a better word) a project that has been in the backburner since November, however I was in the middle of a re-factoring of code so I could introduce new features.
Quite a lot of this code was the UI, I wanted a clean way in triggering events and well, if I Java 7 had delegates or lamdas I could easily pass said function to the UI element in question and have it invoke the method upon press. However, of course you can’t do this in Java 7 or below :(.
What are your handy alternatives to this problem? Anonymous interfaces seem like the only way to sort of do what you require.
Like I could do this:
public interface IEntityAction{
void execute();
}
Then pass it to a button like so:
Button moveLeft = new Button(position, new IEntityAction{
public void execute(){
// Change state here
}
});
That looks just…horrible.
As you know with C# you could easily just do:
Button moveLeft = new Button(position, player.MoveLeft);
// Then in button
public void OnClick()
{
action.Invoke();
}
Any ideas? I would be interested to know how you guys deal with this. I never really knew what Lambdas and Delegates were intil I used C#, I am finding it very hard to think of solutions to prevent a huge amount of cross talk between unrelated classes, although it does not matter to have calls to the player instance in the UI, no one likes it :p.
EDIT:
If you are wondering why I am a little anal about the cleanlinesses of the code is due to this project being developed and updated in the long run, I also won’t be the the only person implementing said updates, I might not even be working with the project any more. The last thing I want to do is dump the poor sap with an absolutely horrible source.