Skip to main content
Removing blacklisted engine tag.
Link
Vaillancourt
  • 16.4k
  • 17
  • 56
  • 61

Recommended approach What are the recommended approaches for an event system in Java?

Source Link

Recommended approach for event system in Java

What are the recommended approaches for an event system of a turn-based RPG's battle engine?

The system I'm currently working on has a runEvent method that sorts the actions queue based on their priorities. This method should also call the object's (item, player, condition) method according to an eventid, which currently is a String ("TurnStart", "TurnEnd", etc). The different objects may or may not have the same parameters for the same eventid.

In JavaScript, it would be something like the following:

retval = action['on' + eventid].apply(this, args); 

I've read about using Java's Reflection to call a method through a String stored in a variable, but some people advise against it because of its security flaws.

I also thought about having a switch (eventid) inside a handler method that would call the appropriate method, but I think that would become impractical, given the substantial amount of different parameters combinations.

So, what is the recommended approach for this kind of situation in Java?

Note: It doesn't need to be based on String passing.