简体   繁体   中英

Object Oriented Design - Game

I'm designing a Game with Java, there are 2 players, different types of enemies and potions. This is my classes hierarchy:

在此处输入图片说明

As you can see I have the Touchable interface, I know interfaces are contracts with classes that describe a certain behavior of the class, so I declared the void collision(Player p) method inside the Touchable interface because not every GameObject is Touchable (by the player) but then I realized something, I can put the void collision(Player p) method in the GameObject class this way:

public void collision(Player p) {}

and then get rid of the Touchable interface, and get rid of this if statement in the for each loop over all the GameObjects:

if (gameObject instanceOf Touchable) { ... }

and just call gameObject.collision(p) and if it is not implemented by the subclass nothing will happened. Do you think this is a better way?

I think it should still be part of the interface for the sake of code readability. It's much more obvious that way and you can also put any other methods that you discover later on to be constant throughout your program inside the same interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM