Ruby中的Singleton vs. Monostate模式

时间:2022-05-21 22:28:50

Suppose a class needs to load an external library which takes some time to load and thus should be loaded only once. Two natural solutions to this would be to use the singleton pattern or the monostate pattern. Is there any advantage to either of these solutions in this particular context in Ruby?

假设一个类需要加载一个外部库,这需要一些时间来加载,因此只应加载一次。对此的两个自然解决方案是使用单例模式或单稳态模式。在Ruby的特定上下文中,这些解决方案中是否有任何优势?

For example:

# Using a Singleton class
require 'singleton'

class Parser
  include Singleton

    def initialize
      @parser = load_external_library
    end

    def parse(sentence)
      @parser.parse(sentence)
    end
end

# Then calling using...
Parser.instance.parse(sentence)

Versus:

# Using a Monostate class

class Parser
    def self.parse(sentence)
      @@parser ||= load_external_library
      @@parser.parse(sentence)
    end
end

# Then calling using...
Parser.parse(sentence)

Since the second syntax is much cleaner, are there any advantages to using the Singleton in Ruby?

由于第二种语法更清晰,在Ruby中使用Singleton有什么好处吗?

1 个解决方案

#1


4  

The singleton pattern structurally enforces the fact that you can never have more than one instance of a class at a time, and it is obvious to the developers that they are dealing with a singleton.

单例模式在结构上强制执行这样一个事实,即一次只能有一个类的实例,并且开发人员明白他们正在处理单例。

The monostate enforces the behavior of a singleton without the structure of the monostate.

monostate强制执行没有monostate结构的单例的行为。

You might find situations where you still need instance data. Therefore a monostate would be better. You can create the instance, use methods to affect instance data and still have access to the static data. With a singleton, you cannot have instance data.

您可能会发现仍需要实例数据的情况。因此,单稳态会更好。您可以创建实例,使用方法来影响实例数据,并仍然可以访问静态数据。使用单例,您不能拥有实例数据。

Besides, If you plan on deriving classes from the singleton and you want those classes to be singletons, your better choice is monostate. That’s because all classes derived from a monostate are monostates. Classes derived singleton classes are not singletons by default. You would have to add the static method and attribute to each derived class.

此外,如果您计划从单例中派生类,并且您希望这些类是单例,那么您更好的选择是单值。这是因为所有来自monostate的类都是monostate。默认情况下,类派生的单例类不是单例。您必须将静态方法和属性添加到每个派生类。

#1


4  

The singleton pattern structurally enforces the fact that you can never have more than one instance of a class at a time, and it is obvious to the developers that they are dealing with a singleton.

单例模式在结构上强制执行这样一个事实,即一次只能有一个类的实例,并且开发人员明白他们正在处理单例。

The monostate enforces the behavior of a singleton without the structure of the monostate.

monostate强制执行没有monostate结构的单例的行为。

You might find situations where you still need instance data. Therefore a monostate would be better. You can create the instance, use methods to affect instance data and still have access to the static data. With a singleton, you cannot have instance data.

您可能会发现仍需要实例数据的情况。因此,单稳态会更好。您可以创建实例,使用方法来影响实例数据,并仍然可以访问静态数据。使用单例,您不能拥有实例数据。

Besides, If you plan on deriving classes from the singleton and you want those classes to be singletons, your better choice is monostate. That’s because all classes derived from a monostate are monostates. Classes derived singleton classes are not singletons by default. You would have to add the static method and attribute to each derived class.

此外,如果您计划从单例中派生类,并且您希望这些类是单例,那么您更好的选择是单值。这是因为所有来自monostate的类都是monostate。默认情况下,类派生的单例类不是单例。您必须将静态方法和属性添加到每个派生类。