Swing and a miss. instanceof is often just a single comparison and so very fast. We covered this recently as well: http://www.java-gaming.org/index.php/topic,18750.0.html
Didn’t understand this bit. If the Vampire in the example is a sub-class of an existing class you can distribute as much as you add extensions as much as you want. With composition it’s the same, if you want to add on new uses of existing stuff thats fine, but as soon as you want to add a new type of component you’re back to the same problem. I guess you could end up with a really high level component interface that satifys everything, but most likely that’d be a real PITA to work with.
Kev
Yeah, I thought the conclusion from that thread was that it was slower than other options, bot not much so? I guess not… Either way, instead of having in your list always saying if (thing instanceof OtherThing) {doStuff();}, if you’re using a component system you can just tell absolutely everything to doStuff(), and if it has no stuff() component it will just ignore it. So you’re avoiding a comparison. But scratch that, because really you’re just moving the work around… so yeah I guess it’s basically going to be the same no matter what.
@Kev: I was more thinking about how Vampire will probably not add any new functionality (because it is so close to Zombie and Undead) and so you’ll just be able to make an XML file or whatever for it that has the correct components to use in it. If you had a Vampire class, you need to deal with putting that in the right classpath to avoid problems, whereas the XML can be loaded from wherever, including servers on the net and the like. But I guess if you configure your classpath right that becomes pretty moot anyway. And you’re right that if you actually want to add new functionality, rather than a new combination of traits you already have, then either way you’re going to need to distribute new code. But really in the end I guess this is just more proof that you should just do what feels best for you and makes the most sense in your project.
The really depends on if your asking if an object is an instanceof a plain object or an interface. Here’s some pseudo-code:
// check if class 'S' implements the interface 'T'
boolean
implements(class S, class T)
{
for (class interface = all interfaces implmented by S) {
if (interface == T || implements(interface, T))
return true;
}
return false;
}
// Is 'S' a superinterface of 'T'?
boolean
isSuperinterface(class S, class T) )
{
while (S) {
if (implements(S,T))
return true;
S = parent of S;
}
return true;
}
boolean instanceof(class S, class T)
{
// Trivally assignable if the same class
if (S==T)
return true;
if (T is a class) {
if (S is a class) {
// -> S must be T of a subclass of T
do {
S = parent of S;
if (S==T)
return true;
} while(S is not Object);
}
else {
// * T is class type
// * S is interface
// -> T must be Object
return T == java.lang.Object;
}
}
else {
// * T is interface
// -> S must implement T
return isSuperinterface(S, T);
}
return false;
}
This can be speed-up in a number a ways. Example is to track the hierarchy depth with each class and to allow skipping and immediate failure if impossiable.
I don’t quite get what your code is for. To check if one class is the super class or interface of another you can just do:
superClass.isAssignableFrom( subClass );
Admittedly I find isAssignableFrom tends to be a very slow function, so I just cache the result.
The pseudo-code is “not Java” ™. It is what the VM/JIT must insure for ‘instanceof’ (and likewise assignment compat). It’s intended to point out that 'object instanceof SomeClass" is much less complex than “object instanceof SomeInterface” and that in general both will be more complex than the quoted “single if” cost.
(EDIT: Note I left out dealing with arrays on purpose).