如何只使用类方法创建一个能够记住值的类? Core Animation如何做到这一点?

时间:2021-11-11 06:16:09

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:

例如,无论我需要计算加速度,我都包含SLPhysics.h并在我的代码中写入:

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.

这很好用。但我更喜欢一种行为类似于Core Animation的方式,其中大部分必须完成的细节都是从类的用户隐藏的。

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:

所以,为了防止我让实例变量记住上一个测量值v1和t1,我更喜欢这样一个简单易操作的调用:

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.

注意context参数。我认为这是要走的路。但是目前我不知道这些类方法如何访问数据结构,例如NSMutableDictionary。如果我要创建实例变量,则类方法将无法访问。如果我使它成为一个实例方法,那么使用将是丑陋的并且难以阅读,并且用户会受到内存管理和其他东西的困扰。所有这些分配和初始化都可能花费太多(我认为)。我的意思是......看看Core Animation。那太容易了!我想要同样的事情。但我猜他们也有这个问题。他们必须记住一些东西。动画持续时间,上下文等。

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"?

我考虑过将值写入文件,但这太贵了。我可以使用SQLite。太昂贵的操作,可能。这些方法可用于每秒需要5到100次计算的地方。不是有“类变量”之类的东西吗?

1 个解决方案

#1


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

使用单身人士。您的类将所有“类变量”存储为单例变量。将单例实现为类方法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.

如果需要,添加线程安全。

#1


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

使用单身人士。您的类将所有“类变量”存储为单例变量。将单例实现为类方法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.

如果需要,添加线程安全。