Jump to content

  • Log In with Google      Sign In   
  • Create Account

Downsides/upsides to these absurd ideas?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 TheChubu   Crossbones+   -  Reputation: 4761

Like
0Likes
Like

Posted 10 April 2013 - 06:44 PM

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?


"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator


Sponsor:

#2 SeraphLance   Members   -  Reputation: 1454

Like
2Likes
Like

Posted 10 April 2013 - 09:31 PM

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, 10 April 2013 - 09:36 PM.


#3 TheChubu   Crossbones+   -  Reputation: 4761

Like
0Likes
Like

Posted 11 April 2013 - 08:33 PM

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.


"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS