Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  is "switch (instanceof)" possible?  (Read 9812 times)
0 Members and 1 Guest are viewing this topic.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #30 - Posted 2011-02-04 13:47:27 »

I would investigate a way to roll all the commonality into a base class which would store a single int or enumeration.  Then instead of switching your code should be able to do what it needs to do via array indexing off that int.

An enum could be used, but you would be sending an extra int over the network for no real reason. Note this is an extra overhead of 1 or 2 bytes, so not a big deal. However, this is easily avoided with the solutions in my post above.

Forget about this craziness and just send some custom text or JSON. Like Riven says, I'd keep it simple at first.

The "problem" remains. Instead of 120 'if (object instanceof Xxx)' you would have 120 'if (string.equals("Xxx"))'.

Anyway, I doubt Mads actually has 120 objects and is just annoyed at how the code looks with a bunch of instanceofs. There is no bottleneck.

Correct, but there will eventually come a time where I have too many objects, to just put if-statements.
I also have made networking before using a text-based protocol, and blocking I/O, but that is just a cheap solution (..or so I think).

Offline princec

« JGO Spiffy Duke »


Medals: 434
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #31 - Posted 2011-02-04 14:51:48 »

Hence my technique Smiley Which is still the simplest to understand, neatest, easiest to not get wrong, and fastest so far...

Cas Smiley

Offline JL235

JGO Coder


Medals: 10



« Reply #32 - Posted 2011-02-05 01:03:56 »

Hence my technique Smiley Which is still the simplest to understand, neatest, easiest to not get wrong, and fastest so far...

Cas Smiley
Again, I disagree with part of this. Your method would be faster, but it requires altering both the objects being worked on and the event handling class. With my solution (and it's similar alternatives) you only code the event handling bit.

With mine you only need to understand two parts: adding handlers and invoking them on an object. With yours you need to understand adding the interface stub to your objects, the double-dispatch mechanism and building your event handling class.

Needing to write less code and having less concepts to learn makes mine (in my opinion) simpler (but yes, also slower).

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

« JGO Spiffy Duke »


Medals: 434
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #33 - Posted 2011-02-05 01:30:52 »

It does rather depend on a whole load of other factors we're not privy to tho' eh? I mean, if I could alter the classes, I would, in a shot; if I couldn't, the Map method's best. Etc.

Cas Smiley

Offline JL235

JGO Coder


Medals: 10



« Reply #34 - Posted 2011-02-05 03:53:52 »

It does rather depend on a whole load of other factors we're not privy to tho' eh? I mean, if I could alter the classes, I would, in a shot; if I couldn't, the Map method's best. Etc.

Cas Smiley
I'd say the only factor it depends on is performance (and I'd argue only if you really need the performance).

Something that just hit me is that with the map method it's implementation is entirely separate to the code using it. It could even be provided from an external jar of utility classes. Where as with the visitor pattern you have to bake this yourself directly into your code and your design. That's a big difference in terms of simplicity.

Pages: 1 [2]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (33 views)
2014-12-15 09:26:44

Mr.CodeIt (24 views)
2014-12-14 19:50:38

BurntPizza (51 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (46 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (61 views)
2014-11-29 21:32:03

toopeicgaming1999 (124 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!