Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Should I use Java Observer?  (Read 191 times)
0 Members and 1 Guest are viewing this topic.
Offline Gibbo3771
« Posted 2014-06-11 13:53:22 »

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?

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #1 - Posted 2014-06-11 14:08:52 »

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?

Offline SilverTiger

Junior Member


Medals: 4
Exp: 2 years


がんばってください!


« Reply #2 - Posted 2014-06-11 14:10:09 »

Well the benefit from using the Java Observer is, that you don't have to write it yourself Roll Eyes

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
    entity.addObserver(observer)
  • When a change in your entity happens do
    setChanged()
    and
    notifyObservers()

I wouldn't bother writing your own Observer pattern Wink
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Gibbo3771
« Reply #3 - Posted 2014-06-11 14:13:53 »

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

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.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #4 - Posted 2014-06-11 14:47:06 »

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

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. Roll Eyes

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.

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.

Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (13 views)
2014-09-19 03:14:18

Dwinin (31 views)
2014-09-12 09:08:26

Norakomi (58 views)
2014-09-10 13:57:51

TehJavaDev (79 views)
2014-09-10 06:39:09

Tekkerue (40 views)
2014-09-09 02:24:56

mitcheeb (62 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (30 views)
2014-09-07 01:12:14

Longarmx (36 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!