Sign in to follow this  

Downsides/upsides to these absurd ideas?

Recommended Posts

So, I was thinking about taking some features a bit to the extreme, some "What if..." scenarios.


Say that I don't want to use "classical" inheritance at all. What would be the downside of relying on interfaces only?


First thing that comes to mind is the overhead of the vtables the JVM would have to use. Just skimmed over some things and seems like HotSpot uses a vtable for every kind of inheritance, but it still does more work for resolving abstract methods.


Second thing is repetition of code. If I say, well my IPosition GetX() method returns the position of the object in the X axis. It might aswell be exactly the same for 10 other classes that use that interface, but I'd have to implement it on every one of them separately instead of just inheriting the working method.


Instead of establishing a hierarchy of "Actor is a(inherits) SolidWorldObject" or "Actor has a(composition) SolidWorldObject" you'd have a "Actor implements SolidWorldObject" you could get rid of the hierarchy and be able to ask the Actor object directly for its position instead of asking to the SolidWorldObject inside the Actor.


On the other hand, everything using interfaces would make you able to do some cool stuff, like grouping things in collections without worrying of "can I cast this to that, is that child or parent of that other class" and so on.


It would make easier to get a more "data oriented" design with huge collections of objects and the program iterating over them with the same interface, again, without the obtuseness that some class hierarchies might have.


And other thing I thought is if it would be any different to replace all instanced object methods with static methods that have a reference as parameter.


Say, instead of calling vec.normalize(); You'd call, Vector4f.normalize(vec); Instead of calling player.getX() You'd call Actor.getX(player);


The object would only hold data whereas the class would hold the static methods to operate on such data.


I'm not quite sure but I guess that's how method calls are implemented when they're compiled. While the per-object method calls gives you a nice way to relate the method call with the object you're using. So I guess you'd be doing "compiler work" by doing the reference passing by yourself instead of relying of the per-object method call and the "this" keyword.


It all came out of me thinking what could I do to make my math library better (something I might ask for more directly on another thread). What do you think?

Share this post

Link to post
Share on other sites

Interfaces are typically coupled with Dependency Injection, which you seem to recognize.  Composition has to happen at some level.  When you kludge over an existing system (like Java's standard library, with it's rigid inheritance chain), wrapping big objects with DI is commonly done.  If you're instead architecting such a system, you have more options.


Notably, Component-Based Entity Systems.  We've had a recent surge of articles involving them, and this is exactly what they are for.  Rather than re-implement GetX() for every object that has an X-coordinate, or wrap an existing monolithic object that already does so as mentioned above, inject that simple behavior (or simple data structure in a pure component environment) in isolation.


IMO, this really captures the spirit of interfaces in the first place -- collections of functionality that are contractually required to be implemented in some fashion.  If you don't want to system-drive everything as in a pure gamedev-ish Component System, perhaps the Strategy Pattern would be a good place to look for inspiration.


Your second thing is basically how objects are implemented in C.  At some level, that's how most OOP works under the hood, as you suspect.  I wouldn't really examine Math libraries that way though -- math libraries are typically static classes (or namespaces with functions, whatever) because math, or at least the kind of math in simple math libraries, is typically stateless.  I don't think it has any utility in a real OO language though; C++ pretty much does this conversion for you at zero cost over implementing semantically the same thing in C.

Edited by SeraphLance

Share this post

Link to post
Share on other sites

Hm. I've just used a few interfaces for another project (just recently discovered fractal terrains) and ended up doing what you mentioned. Doing several getX,setX,getY,setY just didn't seemed nice at all. Made a few Position, Direction, etc objects, "composed" the others with them and thats it. Not so clean in the calling (this.particle.position.getX() looks kinda bad) but it halves the work I'd have to make if I was just using "IPosition" or "IDirection" interfaces.


I knew about the strategy patter by name only (I think it was mentioned in Effective Java) I'll read more about it!


About the class statics. Nah, I wasn't going to use that for my math library. Even the normalize method, which I've seen implemented directly in Vector objects, is a separate static method in my math class that receives a vector to normalize. Otherwise it would make my vector classes very complicated.


Thanks for your input.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this