I'm implementing classes for simulating and generating different kind of automata. I'd prefer to use the same State and Transition classes for all the automata: NFA, DFA, PDA, etc.
For a PDA a transition from one state to another work require some sort of an extension, because the transition requires to pop something from and push something on the stack.
So I came up with the idea to let the transitions accept a extension class as template parameter:
public class Transition<T> {
private State target;
private T extension;
public Transition(State target, T extension) {
this.target = target;
this.extension = extension;
}
public State getTarget() {
return target;
}
public T getExtension() {
return extension;
}
}
Note that there are EpsilonTransition and RegularTransition derived classes from this base class.
A PDA would require for instance this extension:
public class StackExtension {
private Character toPop, toPush;
public StackExtension(Character toPop, Character topush) {
this.toPop = toPop;
this.toPush = topush;
}
public Character getToPop() {
return toPop;
}
public Character getToPush() {
return toPush;
}
}
So that the PDA class can use Transition<StackExtension> instances.
Although this is a solution for my problem, it feels like bad design. Especially for the NFA and DFA, which do not require any extension.
Is there another design pattern for my particular problem?