When is it wise to use Singleton classes in Ruby? [closed]

余生长醉 提交于 2020-01-13 08:11:22

问题


I was reading about Singletons in Ruby. I never really had the need to use them before, but I got curious and decided to look them up, to see if I should've used them in the past, or maybe I could use them in the future if I knew what they are used for.

The only times I can think of to use this is:

  • ? When I need to have a special object. Example: "smart" Hash object, that acts like a normal Ruby hash with a few additional quirks.
  • When I want to make it so only one instance of a class can exist.

However, I am not really sure I had the need for either of the above.


回答1:


Note that a class mixing in the Singleton module is functionally equivalent to a class or module with 'class' methods and either a guaranteed initialization call or inline initialization. Compare this usage of Singleton:

require 'singleton'
class Bar
  include Singleton
  attr_reader :jam
  def initialize
    @jam = 42
  end
  def double
    @jam *= 2
  end
end

b1 = Bar.instance
b1.double
b2 = Bar.instance
b2.double
p b1.jam          #=> 168

with this no-magic module:

module Foo
  @jam = 42
  def self.double
    @jam *= 2
  end
  def self.jam
    @jam
  end
end

Foo.double
Foo.double
p Foo.jam          #=> 168

In both cases you have a single global object that maintains state. (Because every constant you create in the global scope, including classes and modules, is a global object.)

The only functional difference is that with the Singleton you delay the initialization of the object until the first time you ask for it.

So, if you ever have 'class' methods on a class or module and you use those to change the state of that object (e.g. a class keeping track of all subclasses that inherit from it) you are essentially using a singleton.




回答2:


Like you said, A Singleton class is needed when you need to have only one instance of the class. A very good example of the Singleton class in a widely accepted framework is the Rails Logger class.




回答3:


You use singleton classes all the time in Ruby. In fact, every object has a singleton class. (Note: for performance reasons, singleton classes are actually only created when you first access them, but that's a performance optimization. Semantically, every time you try to look at a singleton class, it will be there.)

So called "class methods", for example, don't actually exist in Ruby. They are just regular instance methods of the class object's singleton class. (It's just so much easier to say "class method" instead.)

Everytime you define a method with the def foo.bar syntax, you are adding a method to the singleton class of foo.




回答4:


your second bullet point is exactly on point. think of a singleton class as an entirely static object. the Boolean and Null objects in ruby are examples.



来源:https://stackoverflow.com/questions/13313993/when-is-it-wise-to-use-singleton-classes-in-ruby

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!