Should I use Java Observer?

So I have been studying Design patterns and decided I am going to create basic games (such as breakout, space invaders etc etc) with a added functionality to make them more fun, kind of like (I hate to say it) what candy crush done.

I am wanting to use an Observer type pattern in order to trigger events, such as when the ball hits a block I want to let the collision, sound and scoring system to know this happened, in once nice neat line of code.

Should I use the Java observer? Or just go ahead and roll my own for all the time it takes, am I going to get any benefit from using the Java one?

I see no problem with the Observer pattern in this instance. I use it my own games to notify the underlying systems such as sound, music and statistics logging.

I do not know if it is a good idea to let the physics depend on entities being Observable though, because in order for the entities to register the state change they have to perform some physics calculation. I fear that this might lead to having half of the physics code stuck inside an Observable, and the other half stuck inside an Observer, and I am not sure that is an ideal situation.

I suggest (for the physics only) that you update the model from outside the model. Still, make it Observable for the things not directly related to it, but for core functionality like the physics in a pong game, I really would not do it.

Side note: If you are having fun with design patterns consider the Command pattern for your simple games. I know it is a little bit of a hassle, but it allows for great replay-functionality as well as aids AI development.

Side note two: Have you seen this excellent free e-book?

Well the benefit from using the Java Observer is, that you don’t have to write it yourself ::slight_smile:

The only things you would have to do:

  • Create your Entity class (extends java.util.Observable)
  • Create an EntityObserver class (implements java.util.Observer)
  • When creating a entity do [icode]entity.addObserver(observer)[/icode]
  • When a change in your entity happens do [icode]setChanged()[/icode] and [icode]notifyObservers()[/icode]

I wouldn’t bother writing your own Observer pattern :wink:

Yeah, read it more times than I can remember.

Thanks for the advice, I am not sure how I would apply the Command pattern in a sense that I get the same result as the Observer.

As it stands I simply have my CollisionHandler hold observers, which are the classes that handle sound, animation and scoring. Then when a certain collision happens, notify everything it just happened.

I am also not using any complicated physics, at all lol.

[quote=“Gibbo3771,post:4,topic:49413”]
That’s not what I intented to communicate at all, so that is probably why it does not make any sense. I meant that while you are having fun applying design patterns to games, the command pattern might be a good pattern to get to know. Unrelated to the issue of the thread. ::slight_smile:

[quote=“Gibbo3771,post:4,topic:49413”]
Oh. I understood that you were going to notify a PhysicsHandler of some description, that would then take care of the physics part of your game. I would advise against using an Observer to handle any kind of physics.