标签: monostate

静态类和单例

不是一个具有所有静态成员/方法的类是一种单例设计模式吗?有这样的课程有什么不利之处吗?详细解释会有所帮助.

c++ singleton static design-patterns monostate

12
推荐指数
2
解决办法
6939
查看次数

Ruby中的Singleton vs. Monostate模式

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

例如:

# 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)
Run Code Online (Sandbox Code Playgroud)

与:

# 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)
Run Code Online (Sandbox Code Playgroud)

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

ruby oop singleton design-patterns monostate

8
推荐指数
1
解决办法
309
查看次数

Monostate的Python弃用警告__new__ - 有人能解释原因吗?

我有一个基本的Monostate与Python 2.6.

class Borg(object):
    __shared_state = {}
    def __new__(cls, *args, **kwargs):
        self = object.__new__(cls, *args, **kwargs)
        self.__dict__ = cls.__shared_state
        return self

    def __init__(self, *args, **kwargs):
        noSend = kwargs.get("noSend", False)
        reportLevel = kwargs.get("reportLevel", 30)
        reportMethods = kwargs.get("reportMethods", "BaseReport")
        contacts= kwargs.get("contacts", None)

a = Borg(contacts="Foo", noSend="Bar", )
Run Code Online (Sandbox Code Playgroud)

哪位高兴地给了我以下的弃权警告..

untitled:4: DeprecationWarning: object.__new__() takes no parameters
  self = object.__new__(cls, *args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

经过一段谷歌搜索后,我发现这是附加到Bug#1683368.我无法弄清楚这意味着什么.它抱怨以下这一行

self = object.__new__(cls, *args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

这似乎没问题.有人可以用非专业术语解释为什么这是一个问题.我理解"这与其他内置插件不一致,如列表",但我不确定我理解为什么.有人会解释这个让我看到正确的方法吗?

谢谢

python deprecated monostate

5
推荐指数
2
解决办法
1175
查看次数

我应该使用什么而不是void作为变体中的替代类型之一?

我想要一个可能包含类型Foo,(不相交)类型Bar的变体,或者什么都没有.好吧,当然,我正在考虑使用std::variant<Foo, Bar, void>- 但这似乎不起作用.也就是说,您可以定义此类型,但如果您尝试实例化它,则会失败(GCC 8.2).

那我该用什么呢?某种空结构?

c++ variant monostate c++17 std-variant

5
推荐指数
1
解决办法
628
查看次数