Use of Java [Interfaces / Abstract classes] [dupli

2019-01-04 01:51发布

This question already has an answer here:

Lately i decided to take a look at Java so i am still pretty new to it and also to the approach of OO programming, so i wanted to get some things straight before learning more, (i guess it's never to soon to start with good practices).

I am programming a little 2D game for now but i think my question applies to any non trivial project. For the simplicity I'll provide examples from my game.

I have different kinds of zombies, but they all have the same attributes (x, y, health, attack etc) so i wrote an interface Zombie which i implement by WalkingZombie, RunningZombie TeleportingZombie etc. Is this the best thing to do? Am i better of with an abstract class? Or with a super class? (I am not planning to partially implement functions - therefor my choice for an interface instead of an abstract class)

I have one class describing the main character (Survivor) and since it is pretty big i wanted to write an interface with the different functions, so that i can easily see and share the structure of it. Is it good practice? Or is it simply a waste of space and time?

I hope this question will not be rated as subjective because i thought that experienced programmers won't disagree about this kind of topic since the use of interfaces / super classes / abstract classes follows logical rules and is thereby not simply a personal choice.

9条回答
聊天终结者
2楼-- · 2019-01-04 02:32

I don't think that in your case your interface and class structure aligns well with the reality. In fact, I believe (correct me if I'm wrong) that each zombie can be walking, running, teleporting etc. depending on where it is.

Therefore, you should have a zombie class or interface and have actions which modify the zombie's state. The action would probably be an interface or an abstract class, so that you can apply any action to a zombie without knowing what the exact action does (e.g. action.perform(zobie)).

If you have different kinds of zombies, such as three-legged-zombie and one-armed zombies, you might want to implement different classes which handle the zombie stuff, such as displaying themselfes or validating state changes (e.g. a special kind of zombie may not accept to be teleported).

查看更多
放荡不羁爱自由
3楼-- · 2019-01-04 02:34

in terms of your Zombie example, the interface will do well, unless you have common code that you want all zombies to do.

Say you have a Move method, that makes walkingzombies walk, runningzombies run, etc. However, if you want "Move" to make any kind of zombie do something common, then the interface is going to force you to duplicate code, as you cant put a body in an interface.

查看更多
孤傲高冷的网名
4楼-- · 2019-01-04 02:34

My opinion is you better use abstract class called Creature as a super class for all type of, well, creatures, and extend it to Zombie for all type of zombies.
And you will also need an interface.. to define what are the things that a creature can do..
like maybe, walk, or claw, or scream...
the reason why you need an abstract class is to disable the instantiation of Creature, you wouldn't want to have a creature without knowing what creature it is, right?

查看更多
爷、活的狠高调
5楼-- · 2019-01-04 02:35

Nobody agrees about the use of interfaces over super/abstract classes ;)

The main reason to use interfaces and super/abstract classes is to enable polymorphism. In your case for instance, you have stuff moving on the screen (the player and the zombies and so on). Why not make them all move on the screen using the same method? Maybe inherit everything that's going to move on the screen from an object called "Movable" or something like that.

And if you're really into this stuff you might want to look at mixins as well. It's not something that Java supports directly but there are libraries built for it.

查看更多
我想做一个坏孩纸
6楼-- · 2019-01-04 02:38

I have different kinds of zombies, but they all have the same attributes (x, y, health, attack etc) so i wrote an interface Zombie which i implement by WalkingZombie, RunningZombie TeleportingZombie etc. Is this the best thing to do? Am i better of with an abstract class? Or with a super class?

an abstract class will be a super class for your zombies. an interface would also in some sense be a super class (super interface?) for your zombies.

the common properties suggest at least an abstract base class for common properties.

(I am not planning to partially implement functions - therefor my choice for an interface instead of an abstract class)

not sure what you mean by this.

if you had different kinds of monsters (goblins, orcs, etc.) you might find behaviour common to these that would want to belong to different base classes. this would suggest an interface.

i would start with an abstract base class and see what the code tells you as you write it.

I have one class describing the main character (Survivor) and since it is pretty big i wanted to write an interface with the different functions, so that i can easily see and share the structure of it. Is it good practice? Or is it simply a waste of space and time?

your survivor is what is called a player-character (as opposed to a non-player character - someone in a game who will normally not attack your survivor).

most games treat all of these character types as some kind of monster since they will all have many properties in common (health. magic, treasures, weapons, etc.)

so perhaps that's more of an argument for an interface.

see:

Using inheritance and polymorphism to solve a common game problem Class diagram examples for RPG (Role Playing Game) designing class hierarchy for typical characters in role playing game

查看更多
Fickle 薄情
7楼-- · 2019-01-04 02:43

When in doubt, I choose to follow the GOF paradigm.

Encapsulate what varies: - Define unique behavior in its own class. To refer the above example, implement behaviors for walking, running and teleportation in its separate class. This way, polymorphic behavior is implemented.

Conversely, **Aggregate what is common** - Use Abstract classes to define common behavior in polymorphic associations. I use these principles when designing relationships between objects.

查看更多
登录 后发表回答