Interfaces.

Subscribe to Interfaces. 9 posts, 4 voices

Sign in to reply


 
avatar for Cloud_9ine Cloud_9ine 2231 posts
Flag Post

I’m needing help implementing an interface for my many enemy classes as a way of implementing polymorphism. I read the moock book but I cant seem to make much out of it. Could someone point me to a guide or something and maybe help me decide/tell me when i should take advantage of them?

 
avatar for Jabor Jabor 9656 posts
Flag Post

It sounds to me like you don’t necessarily want an interface for your enemy classes here – much better would be a base “Enemy” class and all your other classes inheriting from that.

Inheriting a base class is usually the better solution for “is-a” relationships – like a walking squid “is-a” Enemy, a bus “is-a” automobile, and so on; while interfaces are used for behaviour relationships.

 
avatar for jonathanasdf jonathanasdf 1592 posts
Flag Post

like, for the bus, it would subclass an automobile, but it might implement wheels (if Automobile doesn’t already implement wheels)

 
avatar for Jabor Jabor 9656 posts
Flag Post

No, that’s still not a “behaviour” relationship. A behaviour relationship is something like “carries passengers” – buses, planes, cars and taxis would all implement IPassengerCarrier.

 
avatar for Cloud_9ine Cloud_9ine 2231 posts
Flag Post

Okay I got you guys. so an interface could be used for something where some enemys havee a feature that some have and some dont and a sub sub class thing wont work. Right?

 
avatar for bLasTamos bLasTamos 546 posts
Flag Post

Kind of, but not exactly.
When a class implements an interface, it means I can expect that class to be part of a family of classes that do something, rather than being from a family of classes that are something.

I can give you the example of an interface I made recently.
I have a level editor for my game. Then I have a NormalObject which extends MapObject. NormalObject has x, y, width and height. Then I have the Room object extending MapObject. Rooms are composed of lines that can be arranged in a complex manner, so there is no x and y for a room.
How would you make it so you could create an action of moving both a Room and a NormalObject at the same time? Interface.
I created an IMovableObject interface, with method moveWith(vector:Point).
Implementing this interface on the NormalObject means I change the x and y values, while implementing on the Room means changing the position of every line.
In this case, the ‘do’ component is move the object.

By handling an IMovableObject, the only thing you can expect from it is moving it with a certain vector.
Same thing with Java’s Queue or Stack interfaces. The only thing I’m expeting from a Queue is to add an element to it’s tail and remove an element from it’s head, as well as I’m only expecting a Stack to push, pop, or peak an element to/from it’s head, although both interfaces are implemented by the same class, LinkedList.

 
avatar for jonathanasdf jonathanasdf 1592 posts
Flag Post

basically, what he means is that the interface defines that this object which implements it definitely has a certain function, which in this case is the moveWith function, however depending on the actual object how that function is defined may vary. But, you know that moveWith definitely exists on anything that implements the interface.

 
avatar for bLasTamos bLasTamos 546 posts
Flag Post

No interfaces are not for saying some class has some function. You can interpertate it that way but that’s not what they are.
Conceptually an interface defines a group of classes whose you can expect behaviour from, such as the Stack interface. I know how it behaves, not just because it has those functions, but because its conceptually what a stack represents.

 
avatar for jonathanasdf jonathanasdf 1592 posts
Flag Post

I guess I’m still not very clear about it myself :)

thanks for clearing it up.

Sign in to reply


Click Here