We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionExhibition › Getting my head round OOP - feedback request.
Page Index Toggle Pages: 1
Getting my head round OOP - feedback request. (Read 1115 times)
Getting my head round OOP - feedback request.
Apr 30th, 2009, 8:31am
 
Hello...

I've been playing around with Processing for a while, but am more of a bodger than a proper coder.  I also started off with this type of coding in Actionscript, so I tend to rely more on procedural programming techniques than OOP.  I did manage to build my MA project in Processing, interfacing with a Wii remote (http://www.blindfish.co.uk/projects/perspective/), but since it wasn't a programming-based course I got away with a lot of kludges Smiley

Anyway, I'm now trying to get a better grip on OOP:

http://www.blindfish.co.uk/code/processing/ball/

This is a recreation of something I originally built in Flash (with help from Keith Peter's book Actionscript Animation).  I figured it made sense to store 'world' properties (gravity, friction, limits) in a separate super class - but I'm thinking that it's not entirely logical for my Ball class to extend this...  Or is it?  By making the World properties static I believe this means they should only get stored once in memory, so it's more efficient, but I can't hep feeling the approach is still wrong.

Is there a better way to deal with this situation?  (i.e. where a class is dependent on properties of another class, but isn't conceptually a child object of that class?  Or since the Ball exists in the 'world' does it make sense for it to be a sub-class?)

I'm also still relying on procedural techniques to handle the ball collisions and mouse events.  Is there a better OOP approach to this?

I appreciate that compared to a lot of the projects posted here this is probably all pretty basic stuff, so I'll understand if the response is RTFM - but if anyone can offer some words of wisdom: thanks in advance Smiley
Re: Getting my head round OOP - feedback request.
Reply #1 - May 1st, 2009, 2:01am
 
Note that your "static class" might not do what you expect (remember that classes in a .pde file are nested classes -- in the sketch class!).
Having just the members static is enough.

Some people will deliberately inherit from the World as a constant provider, because it is convenient: constants are available without prefix. But as you guessed, it isn't very satisfying conceptually, as a Ball isn't a World, and it would prevent you from inheriting another class.
Other people would transform the World class to an interface (thus making the constants final, ie. effectively constant, not what you want I guess), to avoid the inheritance lock.

But the best way is probably just to add World as a member of your Ball class: all the balls will have a reference to the world (they know what it is). Look tutorials on difference between is-a and has-a.
Actually, there is another way, to refer the World directly, since its members are all static.

I know these concepts might seem abstract and hard to grasp at first (I come from the procedural world too), but after a while, looking at (good) programs and tutorial, and practicing, it will become intuitive...

PS.: I am not too sure, but I think the statics variables inherited from World are actually shared among instances of ball: if you change one, you will change the others.

So you have two different but roughly equivalent ways of doing this:

class Ball with static World w
You have to get a reference to the (singleton) World instance.
use the constants as w.GRAVITY.

Or simpler, no World member in Ball, just refer to them:
World.GRAVITY
Re: Getting my head round OOP - feedback request.
Reply #2 - May 2nd, 2009, 2:54am
 
Thanks for the quick and helpful response!

I must admit that I wasn't aware of the fact that processing classes are nested under the sketch class and I'm still not sure I understand the implications of this - I'll obviously need to do some more reading!

Here's the reasoning to why I did things the way I did (if this doesn't make sense then please let me know - that's exactly why I'm posting this)...

- It seemed wrong for a class to rely on global variables defined outside of the class so I created the World class to hold these.
- Since I wasn't sure of the best way to go about it I decided that my Ball class would extend the World class.
- I realised that each instance of Ball held its own values for the World properties.  I tried to resolve this by declaring these properties as 'static', but this was apparently not possible unless I also declared them final.  Whilst it makes sense that constants can't be changed I also wanted global variables that could be changed such as the world 'limits'.
- I discovered that by making the class static I could define the properties as static, but didn't have to make them final.  I must admit I don't understand the implications of making the class static and obviously need to research this - but it seems to do what I want.

The end result is that I don't actually need to instantiate the World class for my sketch to run - the properties can be set from any of the Ball instances and will apply to all balls (which answers your PS).  It just makes more sense - in terms of reading the code - to apply the changes to the World class, and would also seem to be more efficient.

I've read about the 'is-a' and 'has-a' concept and understand that conceptually, but in this case I'm not sure of the best approach?  Surely it's more a case of the World having a Ball rather than a Ball having a World?  [scratches head - actually I guess both could be said to make sense...   Undecided ]

I guess the question I'm asking is what's the best way to declare 'global' properties  for a class, so that there is only one reference of each property for all instances of that class?  I'm assuming that if I'd declared my Ball class as static I could simply have included the World properties in the Ball class, meaning that the World class is in fact superfluous...

For this approach to work efficiently - where the World stores properties referenced by another class, rather than its properties being inherited - I guess I need to create a dependency between two classes, so that, in this case at least one instance of the World needs to be created before any Balls can be created...  I'm just not sure how to do that yet.

Sorry - a lot of this is me thinking aloud: I find it helps to make things clear in my head.  As you say with more practice and a bit more research I should be able to get my head round this.  In the meantime if anyone would like to confirm or correct my ramblings it will obviously be much appreciated Smiley
Re: Getting my head round OOP - feedback request.
Reply #3 - May 2nd, 2009, 5:07am
 
blindfish wrote on May 2nd, 2009, 2:54am:
I guess the question I'm asking is what's the best way to declare 'global' properties  for a class, so that there is only one reference of each property for all instances of that class

Well, as I wrote, all the code in a .pde file becomes actually code inside a class (whose name will be the sketch name), so if you declare variables and constants before setup(), you don't create "true" globals but members of this created class.
Well, in practice, they act like globals! Wink
Now, if you put your globally accessible variables inside the main class or inside another class, it doesn't make much difference.
And I wouldn't worry too much with the fact they are global variables: I know they are frown upon, but Processing code is often more informal than pure Java code, aiming at small programs, and not being super-reusable or generic.
Now, it is of course a good idea to ask these questions to improve OO knowledge.

Note: by declaring the World class as static, it acts like it is outside of the sketch's class, ie. as if you put it in a .java class.

Quote:
Surely it's more a case of the World having a Ball rather than a Ball having a World

The ball doesn't have a world, it just have a reference to it, it is like saying it is aware the world exists...
Page Index Toggle Pages: 1