Skip to main content
Tweeted twitter.com/StackSoftEng/status/1578127980160843778
Post Reopened by candied_orange, Christophe, Doc Brown
Post Closed as "Opinion-based" by Doc Brown, gnat, mmathis
deleted 24 characters in body
Source Link
Tinker
  • 155
  • 3

NOTE: This question is reposted from SO because it violates community guidelines for being opinion-based.

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?

NOTE: This question is reposted from SO because it violates community guidelines for being opinion-based.

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?

NOTE: This question is reposted from SO because it violates community guidelines for being opinion-based.

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.

Which approach above incurs less technical debt in the long term?

Source Link
Tinker
  • 155
  • 3

Should I implement one interface with two methods or two interfaces?

NOTE: This question is reposted from SO because it violates community guidelines for being opinion-based.

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?