dimanche 26 avril 2020

Interface to have dynamic method names

Let's say an interface is exposed like this:

public interface IAction {
  void run();
  void walk();
  void stop();
  void jump();
}

Now like this there can be any number of actions. And each action is associated with an enum type as below:

public enum Action {
  RUN("run"),
  WALK("walk"),
  STOP("stop"),
  JUMP("jump")
}

Now every time I want to add more actions then I need to add an enum and also associated method in IAction interface.

Instead, Can the interface methods be exposed from the enum anyhow? And any implementation of the IAction interface should always be dependent on enum Action. And all those enum types in Action should be exposed. Is this possible?





Aucun commentaire:

Enregistrer un commentaire