Borg 模式或只是带有函数的模块

发布于 2024-10-03 23:58:05 字数 369 浏览 3 评论 0原文

我正在考虑在我的项目中使用单例模式,因此我搜索了 StackOverflow 以找到一种Pythonic 方式来实现它。我发现这个问题接受的答案是“带有函数(而不是类)的模块可以很好地用作单例”。另一方面,第二个答案建议使用Borg 模式。对我来说,使用模块是一个简单而直接的解决方案,所以我想了解何时使用 Borg 更好。

I was thinking about using Singleton pattern in my project, so I searched StackOverflow to find a pythonic way to implement it. I found this question with the accepted answer saying that "a module with functions (and not a class) would serve well as a singleton". On other hand the second answer had a suggestion to use the Borg pattern. For me using a module is a simple and straightforward solution, so I'd like to understand when using Borg is preferable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

筱果果 2024-10-10 23:58:05

在整个应用程序生命周期中,单例和模块仅代表一个实例。即使不需要该实例,该实例仍会保持实例化状态。

borg 模式是关于共享状态的。每个客户端类都会创建一个新的 borg 实例,但该实例将在不再需要时被处置 - 这是一种更优雅的方法。

除此之外,对 borg 进行子类化或模拟要容易得多。

A singleton and a module represent only one instance, for all the application lifetime. This instance is kept instantiated, even when it's not needed.

The borg pattern is about shared state. Each client class will create a new instance of the borg, but that instance will be disposed when it's not needed anymore - it is a much more elegant approach.

Beyond that, it is much easier to subclass or mock a borg.

若水般的淡然安静女子 2024-10-10 23:58:05

不同之处在于,在 Borg 模式中,您将拥有属性相同的不同对象,而使用模块版本将为您提供一个对象(模块)。

此外,对象和模块也略有不同:您不能 pickle 模块,但可以 pickle 类。此外,您还可以对对象进行操作(>、<、+、- 等)。

稍微偏离主题一下:经过一些修改,Borg 模式可以用作非常简单的 Multiton:

class Multiton(object):
    __shared_states = {}

    def __init__(self, name):
        if not self.__shared_states.has_key(name):
            self.__shared_states[name] = {}

        self.__dict__ = self.__shared_states[name]

The difference is that in the Borg pattern you'll have different objects whose attributes are the same while using the module version gets you one object (the module).

Also an object and a module are slightly different: you cannot pickle modules but you can pickle classes. Also, you can have operations on objects (>, <, +, -, etc.)

To be a bit off-topic: with some modification the Borg pattern can be used as a very simple Multiton:

class Multiton(object):
    __shared_states = {}

    def __init__(self, name):
        if not self.__shared_states.has_key(name):
            self.__shared_states[name] = {}

        self.__dict__ = self.__shared_states[name]
第七度阳光i 2024-10-10 23:58:05

你可以使用静态类来代替吗? SO问题

ie

class Test(object):
    i = 3 # class (or static) variable
    @classmethod
    def g(cls, arg):
        # here we can use 'cls' instead of the class name (Test)
        if arg > cls.i:
            cls.i = arg # would the the same as  Test.i = arg1

Could you use a static class instead? SO Question

i.e.

class Test(object):
    i = 3 # class (or static) variable
    @classmethod
    def g(cls, arg):
        # here we can use 'cls' instead of the class name (Test)
        if arg > cls.i:
            cls.i = arg # would the the same as  Test.i = arg1
心凉怎暖 2024-10-10 23:58:05

我有一个 borg 模式大放异彩的用例:

你不能在模块级别将函数定义为 @property。如果你想要一些通用数据(如配置)返回动态属性,你可以将其从 Borg 派生,然后编写属性方法。

I have one use case where borg pattern shines:

you cannot define function at the module level as a @property. If you want some common data (like config) returning dynamic properties, you can have it derived from Borg and then write property methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文