Class#allocate及其用法 [英] Class#allocate and its uses

查看:66
本文介绍了Class#allocate及其用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读 http://www.seejohncode.com/后, 2012/03/16/ruby​​-class-allocate/,并进一步研究分配方法:解决方案

allocate存在的主要原因是允许您为对象构建自定义构造函数.如您所链接的文章所述,您可以将SomeClass.new方法设想为默认执行以下操作:

class SomeClass
  def self.new(*a, &b)
    obj = allocate

    # initialize is a private instance method by default!
    obj.send(:initialize, *a, &b) 
  end
end

尽管文档说了什么,但allocate方法的存在与其说是内存管理,不如说是关于在对象创建生命周期中提供更精细的控制.在大多数情况下,您将不需要此功能,但对于某些极端情况很有用.

例如,在Newman邮件框架中,我将这种技术用于为TestMailer对象实现伪构造函数;它实现了new方法以实现API兼容性,但实际上返回了一个实例,无论它被调用了多少次:

class Newman::TestMailer
  def self.new(settings)
    return self.instance if instance

    # do some Mail gem configuration stuff here

    self.instance = allocate
  end

  attr_accessor :instance
end

如上所述,除了重新定义new之外,我还没有看到其他许多用例(尽管我认为有些奇怪的序列化东西也使用了此功能).但请记住,值得指出的是,Ruby始终提供这些扩展点,无论您是否需要定期使用它们.罗伯特·克莱姆(Robert Klemme)有一篇很棒的文章,名为完整类,我强烈建议阅读如果您想看看在Ruby中这个设计概念已经走了多远:-)

After having read http://www.seejohncode.com/2012/03/16/ruby-class-allocate/ and looking more into the allocate method: http://www.ruby-doc.org/core-1.9.3/Class.html#method-i-allocate I became very curious.

Ruby was built in a way that we did not have to manually allocate or free space for/with objects, but we are given the ability to do so. Why?

What are the uses in Ruby of allocating Objects manually? The article I read showed a custom initialize method, but are the uses of it so limited?

解决方案

The main reason allocate exists is to allow you to build custom constructors for your objects. As the article you linked mentioned, you can envision the SomeClass.new method as doing something like the following by default:

class SomeClass
  def self.new(*a, &b)
    obj = allocate

    # initialize is a private instance method by default!
    obj.send(:initialize, *a, &b) 
  end
end

Despite what the documentation says, the existence of the allocate method is not so much about memory management as it is about providing some finer grained control over the object creation lifecycle. Most of the time, you won't need this feature, but it is useful for certain edge cases.

For example, in the Newman mail framework, I used this technique to implement a fake constructor for a TestMailer object; it implemented the new method for API compatibility, but actually returned a single instance regardless of how many times it was called:

class Newman::TestMailer
  def self.new(settings)
    return self.instance if instance

    # do some Mail gem configuration stuff here

    self.instance = allocate
  end

  attr_accessor :instance
end

I've not seen many other use cases apart from redefining new as shown above (although I imagine that some weird serialization stuff also uses this feature). But with that in mind, it's worth pointing out that Ruby consistently provides these kinds of extension points, regardless of whether or not you'll need to use them regularly. Robert Klemme has a great article called The Complete Class which I strongly recommend reading if you want to see just how far this design concept has been taken in Ruby :-)

这篇关于Class#allocate及其用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆