【发布时间】:2018-11-14 00:06:36
【问题描述】:
我已经编程了几年了。接口似乎一直是我难以理解的话题。将尽可能多的功能抽象到接口中是一种好习惯吗?
我从来没有完全理解它们的好处。我一直认为“为什么不正常编写方法”。它们只是方法。然后我开始学习依赖注入,以及它如何真正简化代码,让构造函数成为类与外界的主要联系点。
但是,最近,我再次开始思考接口,以及如何将它们像类型一样扔掉,而不仅仅是标记方法的可爱方式。
所以在我第一次在 Java 中使用接口的真实实验中,我做了这个例子,我想知道的是:
对于了解接口强大功能的 Java 开发人员,我写的这个小 sn-p 代码说明了一个好的模式还是好的思路?到目前为止,基本上这是好的代码吗?我只想知道我是否走在正确的道路上。我明天要进行第二次工作面试,并且希望在面试中提出这一认识,前提是我走在正确的道路上。
代码如下:
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);
}
}
看起来我的想法是对的吗?
感谢您的反馈
-T
【问题讨论】:
-
仅供参考,Java 约定不以
I为接口名称添加前缀。 -
你也可以考虑
extending 接口见stackoverflow.com/questions/13437131/… -
学习策略设计模式对我理解接口的用途有很大帮助。 Head First Design Patterns 一书很好地解释了它和其他常见的设计模式。
-
看到这个链接,我想它可以帮助你。 stackoverflow.com/a/53040308/9998609
-
查看此链接。我认为它可以帮助您。 stackoverflow.com/a/53040308/9998609
标签: java class design-patterns dependency-injection object-oriented-analysis