0

I posted not so long ago a class diagram for an application that I am making. Got some helpful tips and went to work. Designing sure is tough! Anyways, I made a version 2.0, but ran into other things. Maybe somebody could give pointers, or comments, or advice concerning my class diagram :-)

Space Invaders Architecture V. 2.0

First I had 'Speed' in SomeInterface as part of the Sprite abstract class. After consideration I figured that this was not the best place to put 'Speed'. A not so good thing was that I was not able to give a proper name to the interface, and that I didn't know where to put 'Speed', in the Attribute or Operations compartment since it is a property...

Each object (Bullet, Invader, Ship) moves at it's own rate, so I put 'Speed' into an interface. All objects inherit from super class Sprite, and only override the Update() method. The Bullet abstract class does nothing with the Update method it got from Sprite, it's just there to show that it got it from there. I do not know if this is the correct way, or that I should leave it out and only show it in the classes that override it?

Another problem that I do not know how to handle is the animation that is done by the invaders. I got the following properties: SheetSize, FrameSize, and CurrentFrame. Sheet contains the alien pictures (like a flip book), FrameSize is for selecting just on frame on the sheet, and CurrentFrame... well, holds the current frame. Since Ship and Bullet do not animate, these properties are not usefull for them. Where to put them?

Finally, I didn't where to implement IBulletBehavior. First I had each bullet behavior implement IBulletBehavior, but switched to letting the abstract Bullet class implement it. Is there any rule that says which one to take?

casperOne
  • 73,706
  • 19
  • 184
  • 253
TheDude
  • 197
  • 3
  • 10
  • 2
    Slow, Fast, Devastating sounds bad for classes IMHO, it's more like action than objects. And is Bullet really Sprite? Or just some sprite object should paint bullets? – Dainius Feb 21 '11 at 16:01
  • Bullit should be a sprite, yes. It is an object that moves across the screen. Or do you perhaps mean that I shouldn't use a Sprite for it because it isn't animated? The Slow, Fast, and Devastating objects are part of the strategy pattern - dynamic behavior etc. – TheDude Feb 21 '11 at 16:14

1 Answers1

0

Abstract classes describe what a class is. You do not have to implement the actual functionality of the method calls, so don't feel like you have to implement everything about a bullet in your Bullet class. Also, interfaces are great for describing what you class is capable of. Abstract classes show what something is (Is-A) and interfaces describe what the class can do.

(I don't know if this is just a class/interface exercise or if you are actually making a game. If it is just an exercise, you can skip to the next paragraph). For your SomeInterFace interface, how about something like IMoveable or IMobile? In here you can also look at mathematical vectors. The interface should not expose Speed, unless you want objects outside of the one implementing it to interact with Speed. Instead, it should be something along the lines of Move () and/or DetectCollision (). External interactions go in the interface--you're telling the world what you can do.

Finally, what is IBulletBehavior and why should a sprite care if it is one? This comes back to describing what you class can do. A bullet is a sprite but implements IBulletBehavior (whatever that is). Do you want all sprites to implement IBulletBehavior? What happens when you want a rock or something to just sit in the players way? You will now have an object that doesn't implement the specified behavior.

Jim D'Angelo
  • 3,952
  • 3
  • 25
  • 39
  • Yes, I am trying to make a game :-) The abstract class I have will only implement the Draw() method. I think I get what you're trying to say with Speed. Maybe instead of a property a private variable and a method? But couldn't that also be done with a property? The IbulletBehavior should not be in the Sprite class, since other classes also can inherit from sprite, and wouldn't per sé need bullet behavior. IBulletBehavior btw is there because aliens can shoot each of the three bullets. I needed some dynamic changing of behavior (strategy pattern). – TheDude Feb 21 '11 at 16:21
  • I think a better name for this sort of interface would be something such as IShooter. Then you can have a bunch of classes that can implement it and when it is their turn, they can fire their rounds (and the game could care less how they implement it). Your interfaces should clearly describe what the implementing object can do. – Jim D'Angelo Feb 21 '11 at 16:23