Pages

Thursday, August 7, 2008

Dynamic instanceof?

Just came across an interesting line that I thought was worth sharing.
[java]
public static boolean isInterfaceOf (Object o, Class interfac) {
return interfac.isAssignableFrom(o.getClass());
}


Now java obviously has a instanceof keyword that allows you to do this, shorter to type and more recognisable.
The project I’m having a peek at (Archimedes an open source CAD program) only has one use for this where instanceof couldn’t work.
It hides the collection of objects and you can get all of the objects of a certain type. You could get around this by exposing the collection.

Just thought that this method actually makes the coding longer than what it actually does.

No comments:

Post a Comment