I'm currently trying to extend WPILib. Unfortunately I would need multiple inheritance.
I've drawn a uml diagram of the problem (forgive me my crappy handwriting)
As you can see with the red arrows I would need to extend from two classes.
I can't change any thing inside the WPI box and the requires
method from ICommand
needs some functions from CommandBase
so I can't make it default in ICommand
.
How can I solve this?
If you want to try some thing out here's my github repo.
If I understood correctly, you want to have your custom classes (SequentialCommandGroup, ParallelCommandGroup) to extend from library classes, but also to have your own implementation for that "Command" part.
You cannot have multiple inheritance in Java of course, but I think you could utilize Proxy pattern here.
Your classes would still inherit from Library classes, but the implementation of ICommand interface could be delegated to Command object.
Example:
public class MyParallelCommandGroup extends ParallelCommandGroup implements ICommand {
private Command commandProxy;
public MyParallelCommandGroup() {
// instantiate commandProxy here, or inject it as constructor param
}
@Override
public void requires() {
commandProxy.requires();
}
}
You could even extract whole Command class to an interface and implement it in your proxy class. I'm not sure whether this covers your use-case, but maybe it will help you to find a proper solution.