I've been programming for a couple of years now. Interfaces have always seemed to have been difficult topic for me to wrap my head around. Is it a good practice to abstract as much functionality as possible into interfaces?
I have never fully understood the benefit of them. I always thought that "why not just write the methods normally". They are just methods. And then I started learning about dependency injection and how it really simplifies code to have the the constructor be the main point of contact to the outside world for the class.
But then, recently, I started thinking about interfaces again and how they can be thrown around like Types, not just a cute way to tag methods.
So in my first real experimentation with interface use in Java, I made this example, and what I would like to know is:
For Java developers who know the power of interfaces, does this little snippet of code I wrote illustrate a good pattern or good line of thinking? Basically is this good code so far? I would just like to know if I'm on the right path. I have a second interview tomorrow for a job and would like to bring up this realization during the interview, if and only if I am on the right path.
Here is the code:
interface Fightable{
void kick(Fightable enemy);
void punch(Fightable enemy);
void takeDamage(Fightable enemy, String typeOfStrike);
void strikeEnemy(Fightable enemy, String typeOfStrike);
}
interface CanEat{
void chew();
void swallow();
}
interface MotherShip{
int motherShipBoost = 50;
String getColony();
void sendHelp(int[] location);
String buildMoreShips(int amount);
void flyToNewSystem(String newSystem);
int[] beamDownRandomly();
}
interface Animatable{
void animateChew(CanEat eater);
void animateSwallow(CanEat eater);
void animateMove(Moveable mover);
void animateRevive(CanEat eater);
void animateStrike(Fightable striker, Fightable receiver, String typeOfStrike);
}
interface Moveable{
void setNewLocation(int []newLocation);
int[] getLocation();
void move();
}
public class GameCharacter implements Fightable, CanEat, Moveable{
private int health;
private String name;
private int[] location;
private String stamina;
private String origin;
private String colony;
private Animatable animator;
private MotherShip motherShip;
private boolean isRecruited;
public GameCharacter(MotherShip motherShip, String name, Animatable animator){
this.motherShip = motherShip;
this.animator = animator;
this.name = name;
this.location=this.motherShip.beamDownRandomly();
this.colony = this.motherShip.getColony();
}
@Override
public void kick(Fightable enemy) {
strikeEnemy(enemy, "KICK");
}
@Override
public void punch(Fightable enemy) {
strikeEnemy(enemy, "PUNCH");
}
@Override
public void takeDamage(Fightable enemy, String typeOfHit) {
this.health = this.health - 3;
animator.animateStrike(enemy, this, typeOfHit);
if(this.health < 10){
motherShip.sendHelp(this.location);
}
}
@Override
public void strikeEnemy(Fightable enemy, String typeOfStrike) {
enemy.takeDamage(this, typeOfStrike);
animator.animateStrike(this, enemy, typeOfStrike);
}
@Override
public void chew() {
animator.animateChew(this);
}
@Override
public void swallow() {
animator.animateSwallow(this);
health = health + 10;
animator.animateRevive(this);
}
@Override
public void setNewLocation(int[] newLocation) {
this.location = newLocation;
}
@Override
public int[] getLocation() {
return this.location;
}
@Override
public void move() {
animator.animateMove(this);
}
}
Does it look like I have the right idea?
Thank you for any feedback
-T