I'm making a framework in Java and have a method in my abstract class, BasicPhase, called processAction (snippet below). It takes two abstract classes as parameters since the user should implement them in child classes. However, it results in the user being required to constantly downcast every time they override the method. How can this be avoided?
BasicPhase.java
public class BasicPhase {
...
public BasicGameState processAction(BasicAction action, BasicGameState state);
...
}
Example User Class
MyPhase.java
public class MyPhase extends BasicPhase {
@Override
public BasicGameState processAction(BasicAction basicAction, BasicGameState basicState) {
MyGameState state = (MyGameState) basicState; //undesired
MyAction action = (MyAction) basicAction; //undesired
//Game Logic Here
}
}
processAction
clearly cannot work on the base action and state types, so you need to make the types that need to be passed as parameters strongly specified. Do this by makingBasicPhase
aBasicPhase<TAction, TState>
and theprocessAction
aprocessAction(TAction action, TState state)