views:

81

answers:

1

I have some useful methods for physics calculations like acceleration / deceleration, speed, and some more.

Most of them require at least two measurements asunder in terms of time. So every time I want to use them, I have to implement a bunch of instance variables in my object that needs the calculation, for example for calculating an acceleration.

For example, wherever I need to calculate an acceleration, I include SLPhysics.h and write in my code:

double acceleration = [SLPhysics + calculateAccelerationForFirstMeasuredSpeed:v1 secondMeasuredSpeed:v2 firstMeasuredTime:t1 secondMeasuredTime:t2];

This works fine. But I would prefer a way that behaves like Core Animation, where most of the details for doing what has to be done is hidden from the user of the class.

So, in order to prevent me from making instance variables to remember the last measured values v1 and t1, I would prefer a simple-to-handle call like this:

double acceleration = [SLPhysics + calculateAccelerationForMeasuredSpeed:v measuredTime:t context:self];

Notice the context parameter. I think that's the way to go. But currently I have no big idea how those class methods can have access to a data structure, an NSMutableDictionary for example. If I would make instance variables, the class method would have no access. If I make it an instance method, the usage would be ugly and hard to read, and the user would be bothered by memory management and other stuff. Also all this allocation and initialization may cost too much (I think). I mean...look at Core Animation. That's so easy! I want the same thing for this. But they also had that problem, I guess. They have to remember some stuff. The animation duration, the context, etc.

I thought about writing values to a file, but that's too expensive. I could use SQLite. Too expensive operartions, probablys. These methods may be used in places that need 5 to 100 calculations per second. Arent there things like "class variables"?

+4  A: 

Use a singleton. Your class store all it's 'class variables' as singleton variables. Implement the singleton as a class method static:

@interface SLPhysics {
   int a;
   int b;
}

+(SLPhysics*)getSingleton;

+calculate;
@end

@implementation SLPhysics 

+(SLPhysics*)getSigleton {
 static SLPhysics* singleton;
 if (null == singleton) singleton = [[SLPhysics alloc] init];
 return singleton;
}

+calculate {
  SLPhysics* singleton = [SLPhysics getSingleton];
  // use singleton.a singleton.b here
}

@end

Add thread safety if needed.

Remus Rusanu
If I'm getting that right, there will be only one instance of the SLPhysics class around. But who owns it? Who's responsible for releasing the object when it's not needed anymore? Or do I have not to worry about releasing it, since it will be kept around as long as my app runs?
Thanks
There's one static instance, as created in the getSingleton method, that exists for the duration of your application. Nothing owns it or needs to manage its memory, as it will remain around as long as your application is running. The memory usage for this one class will be trivial (as long as you're not store image data or the like in it).
Brad Larson
You could use an C struct for data and avoid all NSObject alloc/retain/release issues.
Remus Rusanu