I have two classes that are similar in nature but they have different functional signatures. I am considering between having two interfaces vs having one. Let me illustrate:
Approach 1: two interfaces
public interface RaceCar {
CompletableFuture<Double> drive(final Wheel arg1, Tactic tactic);
}
public interface Bus {
CompletableFuture<Double> drive(final String someOtherKey, final Controller arg2);
}
One abstract class AbstractCar to share logic between the two:
public abstract AbstractCar {
// Add shared logic here...
}
And the implementation.
public class RaceCarImpl extends AbstractCar implements RaceCar {
...
}
public class BusImpl extends AbstractCar implements Bus {
...
}
Results: 2 interfaces, 1 abstract, 2 implementation = 5 files.
Approach 2: One interface, two methods
public interface Vehicle {
CompletableFuture<Double> drive(final Wheel arg1, Tactic tactic);
CompletableFuture<Double> drive(final String someOtherKey, final Controller arg2);
}
public abstract AbstractVehicle implements Vehicle {
// Sharing code here
}
public class RaceCar extends AbstractVehicle {
CompletableFuture<Double> drive(final Wheel arg1, Tactic tactic) {
// Implement this ...
}
CompletableFuture<Double> drive(final String someOtherKey, final Controller arg2) {
throw new IllegalStateException("Not implemented");
}
}
public class Bus extends AbstractVehicle {
CompletableFuture<Double> drive(final Wheel arg1, Tactic tactic) {
throw new IllegalStateException("Not implemented");
}
CompletableFuture<Double> drive(final String someOtherKey, final Controller arg2) {
// Implement this
}
}
Results: 1 interfaces, 1 abstract, 2 implementation = 4 files.
Verdict
I think having 1 interface is superior because it requires less files. What are your thoughts?
Which approach above incurs less technical debt in the long term?