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.
IndexProgramming Questions & HelpPrograms › "outsourcing" object methods
Page Index Toggle Pages: 1
"outsourcing" object methods? (Read 804 times)
"outsourcing" object methods?
May 31st, 2007, 12:58am
 
This is more a question about good code writing than a specific syntax. So this one is for you coding-veterans out there.

I have a class that creates objects, let's say squares. Each square can change its position. It may fall under a downward gravitational pull or is attracted towards the mousepointer or can move with a set velocity in a random direction. That's three different ways the x/y-Position can be calculated.

I believe it would be extremely "unsexy" coding, if the objects had all three methods written in them and on creation receive a command to execute one but ignore the other two, like:

Code:

if positionCalculation is "gravity"
calculate position with gravityPull()
else if positionCalculation is "mouse"
calculate position with mouseAttraction()
else if positionCalculation is "direction"
calculate position with velocityDirection()


(In my project I'm planning on incorporating far more ways to calculate the position, so it will make even less sense there to have all functions ready but only execute one of possible 15.)

I'm imagining (but really have no idea), that it should be possible to tell an object on creation which way it has to calculate its position, and that the object gets only the necessary code from outside itself, maybe from something like a library of position-calculating-methods? Does this make any sense? Someone mentioned to me this might be called a "delegation model/pattern" or something like that?

Anyone got a hint how I should approach this (if there is any good way), possibly even with example code?

I'd be sooooo thankful!

Re: "outsourcing" object methods?
Reply #1 - May 31st, 2007, 2:23am
 
Hello sir

You can accomplish this by making primitive objects from which you inherit using "extends".

Code:

void setup(){}
void draw(){}

class PhysicsObject
{
PhysicsObject(){}

public void pullByGravity(){
// some shared gravity code
}

public void slowByFriction(){
// some shared friction code
}
}

class Brick extends PhysicsObject
{
public void slowByFriction(){
// code that over-writes Primitive.slowByFriction()
}
}


So, Brick now inherits PhysicsObject and has all the methods of it, including pullByGravity. However, we want Brick to have its own slowByFriction() method, so we simply re-define it. Everything else remains the same (for all intents and purposes).

Hope this is what you're looking for!
Re: "outsourcing" object methods?
Reply #2 - May 31st, 2007, 11:30am
 
I can think of one fairly clean way of doing it, and that's by having a "movementMethod" interface, creating a class that implrements that interface for each movement type, and then upon object creation, you pass an instance of one of these types to the object.
I think however that you'd also need a unified position/velocity object as well to pass current position/speed into it, and to return the new values.

e.g.

Code:
class speedVelocityInfo
{
float x_position,y_position;
float x_velocity,y_velocity;
speedVelocityInfo(float x, float y)
{
x_position=x;
y_position=y;
x_velocity=0; //you could have a 4 argument constructor
y_velocity=0; //that sets these as well for instance.
}
}

interface movementMethod
{
Object move(Object current);
//use Object since you can't use custom classes from
//within processing for interfaces.. we can cast later.
}

class gravity implements movementMethod
{
Object move(Object current)
{
//we have to cast from object now where we can use
//custom classes
speedVelociyInfo svi=(speedVelocityInfo)current;
svi.y_velocity-=0.5; //Or whatever
svi.y_position-=svi.y_velocity;
return svi;
}
}

class square
{
movementMethod mm;
speedVelocityInfo pos;
square(speedVelocityInfo start, movementMethod _mm)
{
pos=start;
mm=_mm;
}

void draw()
{
//have to cast it from Object to our speed & velocity
//info object
pos=(speedVelocityInfo)mm.move(pos);
//now draw it...
}
}


So you can create as many different classes that implement movementModifier as you want, each with unique behaviour, and then pass any of them to your moving things.
Re: "outsourcing" object methods?
Reply #3 - May 31st, 2007, 1:14pm
 
Michael,
John,

I'm amazed every single time at how swift and helpful people reply on the Processing board. It'll probably take some time and experimentation until I manage to wrap my head around the approaches you both propose, but it's definitely helpful already. Thanks!

Here's what wikipedia has to say about the aforementioned delegation pattern.

cheers,
Greg
Re: "outsourcing" object methods?
Reply #4 - Jun 1st, 2007, 12:19am
 
And just to differentiate, movax has suggested a polymorphic solution, whereas john has suggested a delegate solution.
Re: "outsourcing" object methods?
Reply #5 - Jun 1st, 2007, 2:38am
 
by movax you mean mflux Smiley

I was going to suggest using interfaces except it might over-complicate things, since interfaces are great for much much more than just this particular use.
Re: "outsourcing" object methods?
Reply #6 - Jun 2nd, 2007, 2:36am
 
Yikes, you're right! My apologies.
Re: "outsourcing" object methods?
Reply #7 - Jun 3rd, 2007, 3:03pm
 
Interfaces Hmm...

Maybe I should clarify what exactly I'm trying to do. My project (my thesis work) is basically intended to be a drawing application. Instead of giving the user a fixed set of tools to draw with I want the user to be able to create his own tools.
This happens by defining the tool-parameters: shape, size, color, position and duration. These can be fixed values or, more interestingly simple behaviors. e.g.:

A tool could be round, 10px diameter, black, starts drawing at the mouseposition, and draws during mousePressed. This is a classic brush tool.

On the other hand the tool could be round, slowly fade from a random initial size to 0, fade the color from red to green over time, fall under the laws of gravity and exist on the canvas for 5 seconds. Now you have a completely different tool, resulting in different drawing aesthetics.

The parameters should be adjustable with a simple gui.

Is the overall idea clear Do these "interfaces" make more sense for such a project The deadline is in three weeks (including documentation and exhibition planning, yikes!).

*UPDATE*
I've whipped up two rough sketches to illustrate my idea, sans any gui and application options:

linefade
directional




Page Index Toggle Pages: 1