Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
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  
  implementation of "instanceof"  (Read 13858 times)
0 Members and 1 Guest are viewing this topic.
Offline xinaesthetic

Senior Member


Medals: 1



« Reply #30 - Posted 2008-09-15 15:15:00 »

That sounds like a good place to use http://en.wikipedia.org/wiki/Composite_pattern
Hmmm... but there, the logic for processing the items is held within the items themselves; in fact, I believe the types in question do also use this pattern.  What I didn't make clear is that as I want to have different methods for processing the items in different ways, it seems they also need to apply some reasoning based on the type.  This does end up happening in some fairly tight loops as well, so if there was a substantial cost I'd like to know.
Quote
I don't think that there is any performance difference. Profile your application before thinking about optimizing performance.
Quite.  Must get myself better set up with profiling tools... later.  For now, I'm certainly not worried about the cost of instanceof... In fact, I guess I was thinking from more of a design perspective after all.  There were comments earlier that people seemed to think instanceof would only end up appearing as the result of some other bad design choice; I'm not so sure, and think maybe the pattern I ended up with is a reasonable one.  Comments?
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #31 - Posted 2008-09-15 15:50:10 »

There were comments earlier that people seemed to think instanceof would only end up appearing as the result of some other bad design choice; I'm not so sure, and think maybe the pattern I ended up with is a reasonable one.  Comments?

I generally find instanceof to be pretty fragile and a smell that indicates you've got something else wrong somewhere. I only use it when forced to (usually by external APIs), but even that is rare. 'instanceof' is really not too different from random casts - it might work now, but it's very easy to break without warning a few refactorings down the road.

In your particular example, I'd probably just make every Item have a child list and remove the CollectionItem interface totally. If you're worried about the extra overhead you could lazily instantiate the list. Or as already suggested the composite pattern is a good fit here.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline noblemaster

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #32 - Posted 2008-09-15 19:03:10 »

Let's say a have a class of type Vehicle which can be a Car or Ship. How would I create the correct panel for the vehicle? I have a CarPanel and a ShipPanel. The best I came up with is to use instanceof. Suggestions how to do it without instanceof?  Huh

1  
2  
3  
4  
5  
6  
7  
8  
9  
// create a panel for the given vehicle
Vehicle vehicle = ...
if (vehicle instanceof Car) {
  panel = new CarPanel((Car)vehicle);
}
else if (vehicle instanceof Ship) {
  panel = new ShipPanel((Ship)vehicle);
}
...

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

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #33 - Posted 2008-09-15 19:09:24 »

What's wrong with:

1  
panel = vehicle.createPanel();


?

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline xinaesthetic

Senior Member


Medals: 1



« Reply #34 - Posted 2008-09-15 21:09:02 »

In your particular example, ... as already suggested the composite pattern is a good fit here.
Probably right.  I didn't really think through the properties of that pattern just then (or read the wikipedia page properly).  Hardly complicated, I know...  Undecided

Still, it seems odd that a good solution is to have a high level interface with properties that are only really relevant to a distinct subset of implementations... that said, I don't see it leading to the same problems with refactoring etc that my method, for example, could.  Well, tree structures are hardly novel and I suppose established conventions are probably established with good reason.
Offline cylab

JGO Ninja


Medals: 43



« Reply #35 - Posted 2008-09-15 22:29:41 »

What's wrong with:

1  
panel = vehicle.createPanel();


?

Normally you don't want to create this kind of coupling between the data model classes and views.

Mathias - I Know What [you] Did Last Summer!
Offline noblemaster

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #36 - Posted 2008-09-15 22:50:52 »

Normally you don't want to create this kind of coupling between the data model classes and views.
Yes, that's my point! My GUI/View classes are separate from the Model. How can I create a View component without changing the model? The solution I came up with is "instanceof". Any other ideas?

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #37 - Posted 2008-09-15 23:16:02 »

Ah you want to avoid the dependancy. How about a factory which creates appropriate vehicle/control panel pairs? I assume you've already solved the instantiation problem once somewhere when you decide what specific type of Vehicle to create. If the panel is created much later, then you might want a factory object which remembers what it originally created (and maintains a suitable reference to it).

It feels like you've lost some information by allowing a Car/Ship to decay to a Vehicle and instanceof is the only way to get it back. I guess you've got to either create the right panel when you create the vehicle (before you "lose" the information) or maintain it as extra state (either as a factory object, behind the vehicle interface, or in a higher level class somewhere).

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline cylab

JGO Ninja


Medals: 43



« Reply #38 - Posted 2008-09-16 08:00:10 »

Normally you have this kind of objects in a collection and only want to instanciate the appropriate panel, when you really need to display/edit the object. You even might want to make sure only one panel of a kind exists that can be reused for any object of a type. Or you might want view pooling, if you allow multiple views at a time. Maintaining it as an extra state smells like the "getType()" method already discussed in this thread. I would say, this is a perfect place to use instanceof.

Mathias - I Know What [you] Did Last Summer!
Offline princec

JGO Kernel


Medals: 361
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #39 - Posted 2008-09-16 08:30:47 »

This is what I call object oriented wank. Just write it! It's no wonder no-one ever seems to finish any games.

Cas Smiley

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

JGO Knight


Medals: 25



« Reply #40 - Posted 2008-09-16 10:38:44 »

I agree with princec here. Java developers should learn at least one thing from .NET developers: to think "You're not gonna need it!" when you are thinking "what if, what if, what if". It saves a lot of design and development time and head aches. With today's tools it is so much easier to refactor a piece of code should you need to expand something you have hardcoded into something more abstract.
Offline xinaesthetic

Senior Member


Medals: 1



« Reply #41 - Posted 2008-09-16 13:31:22 »

Indeed.  I try to remind myself of this often, but it doesn't always work... finally working with other people on real projects is proving good therapy from this point of view.
Offline noblemaster

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #42 - Posted 2008-09-18 23:55:40 »

It's one of those programmer's blocks where you try to find the perfect solution...

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #43 - Posted 2008-09-29 02:25:23 »

As long as you are not using it very frequently there really is little problem. I don't see what all the fuss is about.

See my work:
OTC Software
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.

atombrot (24 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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!