自定义 ruby .new 运算符
假设我有一个类 Foo
并且构造函数有 2 个参数。 基于这些参数,初始化方法会进行一些繁重的计算,并将它们作为变量存储在类的实例中。对象已创建。
现在我想优化它并创建这些对象的缓存。创建新的 Foo 对象时,如果参数匹配,我想从缓存中返回现有的对象。我该怎么做?
我目前有一个 self.new_using_cache(param1, param2),但我希望将其集成到普通的 Foo.new() 中。 这有可能吗?
我还可以推断,将 .new()
与缓存结合使用在语法上并不正确。 这意味着该方法应该被称为 new_or_from_cache()
。
澄清 这不仅与繁重的计算有关,而且还因为限制了重复对象的数量而成为首选。当我可以从缓存中获得 50 个唯一的对象时,我不希望内存中存在 5000 个对象。所以我真的需要自定义 .new 方法,而不仅仅是缓存的值。
Let's say I have a class Foo
and the constructor takes 2 parameters.
Based on these parameters the initialize method does some heavy calculations and stores them as variables in the instance of the class. Object created.
Now I want to optimize this and create a cache of these objects. When creating a new Foo object, I want to return a existing one from the cache if the parameters match. How can I do this?
I currently have a self.new_using_cache(param1, param2)
, but I would love to have this integrated in the normal Foo.new()
.
Is this possible in any way?
I can also deduct that using .new()
combined with a cache is not really syntactical correct.
That would mean that the method should be called new_or_from_cache()
.
clarification
It's not just about the heavy calculation, it's also preferred because of limiting the amount of duplicate objects. I don't want 5000 objects in memory, when I can have 50 unique ones from a cache. So I really need to customize the .new method, not just the cached values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是我通过定义通用缓存模块提出的解决方案。该模块期望您的类实现“retrieve_from_cache”和“store_in_cache”方法。如果这些方法不存在,它不会尝试进行任何奇特的缓存。
Here's a solution I came up with by defining a generic caching module. The module expects your class to implement the "retrieve_from_cache" and "store_in_cache" methods. If those methods don't exist, it doesn't attempt to do any fancy caching.
像这样的东西吗?
已编辑
当参数与以前相同时不创建实例,
Something like this?
Edited
To not create an instance when the parameters are the same as before,
您可以使用类级实例变量存储先前对象实例化的结果:
You could use a class-level instance variable to store results from previous object instantiations:
正如您可能知道的那样,您已经重新发明了工厂方法设计模式,并且使用您的名字这是一个完全有效的解决方案对于工厂方法。事实上,如果其他人必须理解它,最好不要重新定义 new 。
但是,这是可以做到的。这是我的看法:
这会导致:
As you probably know you have reinvented the factory method design pattern and it's a perfectly valid solution using your name for the factory method. In fact, it's probably better to do it without redefining
new
if anyone else is going to have to understand it.But, it can be done. Here is my take:
And this results in:
您实际上可以定义
self.new
,然后如果您确实想使用Class#new
,则调用super
。此外,如果实际上不需要新实例,这种完全方法可以防止发生任何实例化。这是因为初始化方法实际上并没有做出决定。
You can actually define
self.new
, then callsuper
if you actually want to useClass#new
.Also, this totally approach prevents any instantiation from ever occurring if a new instance isn't actually needed. This is die to the fact the initialize method doesn't actually make the decision.