I have created a rather complicated piece of (Java) code for a game I am working on. I’m fairly sure I will need to change it in the future to optimize or otherwise change the exact functioning. The code can efficiently loop through a subset of the game entities.
The problem is that while looping over this subset different codes should be executed. For example:
There are several simple solutions possible:
As one can see, both efficient and easily manageable code is required. Are there any other solutions possible with the Java programming language? I would prefer to supply the code with a pointer of some sort to the method that takes care of the specific needs but I do not know how or if that is possible in Java.
Besides the hope for a neat solution, I ask this because I want to learn every in and out of Java! Thanks for any answers!
Cheers,
Armored
Declare an interface with methods that the looping code calls, then supply the looping code with different implementations of the interface depending on what you need it do to.
This is commonly referred to as the Template Method Pattern