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 IFight{
void kick(IFight enemy);
void punch(IFight enemy);
void takeDamage(IFight enemy, String typeOfStrike);
void strikeEnemy(IFight enemy, String typeOfStrike);
}
interface IEat{
void chew();
void swallow();
}
interface IMotherShip{
int motherShipBoost = 50;
String getColony();
void sendHelp(int[] location);
String buildMoreShips(int amount);
void flyToNewSystem(String newSystem);
int[] beamDownRandomly();
}
interface IAnimate{
void animateChew(IEat eater);
void animateSwallow(IEat eater);
void animateMove(IMove mover);
void animateRevive(IEat eater);
void animateStrike(IFight striker, IFight receiver, String typeOfStrike);
}
interface IMove{
void setNewLocation(int []newLocation);
int[] getLocation();
void move();
}
public class GameCharacter implements IFight, IEat, IMove{
private int health;
private String name;
private int[] location;
private String stamina;
private String origin;
private String colony;
private IAnimate animator;
private IMotherShip motherShip;
private boolean isRecruited;
public GameCharacter(IMotherShip motherShip, String name, IAnimate animator){
this.motherShip = motherShip;
this.animator = animator;
this.name = name;
this.location=this.motherShip.beamDownRandomly();
this.colony = this.motherShip.getColony();
}
@Override
public void kick(IFight enemy) {
strikeEnemy(enemy, "KICK");
}
@Override
public void punch(IFight enemy) {
strikeEnemy(enemy, "PUNCH");
}
@Override
public void takeDamage(IFight 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(IFight 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
Aucun commentaire:
Enregistrer un commentaire