I need advice on interfaces. I have a class called Unit which can attack other units. The attack can be air or ground. I need single method to achieve attacking called attack(). Some units can only attack ground units, some air units or both.
This is what I’ve come up so far:
public interface Attack() {
public void attack(SCObject object);
}
public interface GroundAttack() extends Attack {
public void groundAttack(SCObject object);
}
public interface AirAttack() extends Attack {
public void airAttack(SCObject object);
}
I have can have different units:
Unit extends SCObject implements GroundAttack {
}
Unit extends SCObject implements AirAttack {
}
Unit extends SCObject implements AirAttack, GroundAttack {
}
The problem is this implementation will reveal the two ground and air options whereas I only want attack() method to be visible.
Can you propose solution or does this seem ok to you?
I would use
I don’t see a reason to have a specific ground or air attack method given you didn’t want to expose this behaviour.