Singleton-为什么使用类? [英] Singleton - Why use classes?

查看:72
本文介绍了Singleton-为什么使用类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就在前几天,我看到了使用所谓的单例模式的代码。类似于

Just the other day I have seen code that uses the so called singleton pattern. Meaning something along the lines of

class MySingleton{
public:
    void foo() { ... }
    static MySingleton&get_instance(){
        static MySingleton singleton;
        return singleton
    }
private:
    MySingleton(){ ... }
    ~MySingleton(){ ... }
    int bar;
};

我确实知道为什么要这么做:

I do see why one would want to do that:


  • 使该实例可全局访问。

  • 确保该类的实例不超过一个。

但是我不明白为什么这种做事方式优于一些免费功能。我实现的方式是

However I do not see why this way of doing things is superior to a couple of free functions. The way I'd implement it is to put

namespace some_name{
    void foo();
}

在标题中和

namespace some_name{
    void foo(){
        ...
    }
}

在实施文件中。如果需要初始化和/或清理,可以添加必须显式调用的几个函数,或者添加

in the implementation file. If I need initialization and/or cleanup I either add a couple of functions that must be explicitly called or I add

namespace{
    class Dummy{
        Dummy(){ ... }
        ~Dummy(){ ... }
    }dummy;
}

到实现文件中。

我知道这是从语义角度来看的单例,但是我看到第一个变体在C ++代码中使用的频率比第二个变体要高得多。为什么?我认为第二个版本略胜一筹,因此我问自己是否缺少明显的东西。

I know that this is from a semantic point of view a singleton, however I see the first variant used far more often in C++ Code than the second. Why? I consider the second version to be slightly superior, so I asking myself if I'm missing something obvious.


  • 第二个版本更容易实施且不易出错。在第一个变体中,故意没有专用副本构造函数来演示这一点。在第二个版本中,没有办法解决此错误。

  • 在第二个版本中,实现和接口更好地分开了。首先,必须在标头中声明所有私有成员。这样的好处是您可以从头开始重写实现,甚至不需要重新编译使用单例的任何内容。使用第一种变体时,即使仅更改了少量实现细节,您很有可能必须重新编译所有用户代码。

  • 在两种情况下,实现细节都被隐藏。在第一个使用private的变量中,在第二个使用未命名空间的变量中。

您能解释一下为什么每个人都使用第一个变量吗?我看不出在C语言中做事的好方法有什么优势。

Can you please explain me why everybody uses the first variant? I don't see a single advantage over the good old way of doing things in C.

推荐答案

根据聚会路线( E. Gamma,R。Helm,R。Johnson和J. Vlissides。设计模式:可重用的面向对象软件的元素。Addison-Wesley,雷丁,MA,1995年,第128页),相对于您提出的解决方案,单例具有以下优势。

According to the party line (E. Gamma, R. Helm, R. Johnson, and J. Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, Reading, MA, 1995, p. 128), the singleton offers the following advantages over the solution you propose.


  • 您可以优化操作和表示形式,例如通过子类化。

  • 您可以稍后改变主意并拥有多个实例。

  • 您可以多态覆盖单例的方法。

  • 您可以通过使用所需的类初始化单例实例来在运行时配置应用程序。

  • You can refine the operations and the representation e.g. through subclassing.
  • You can change your mind at a later point and have multiple instances.
  • You can override the singleton's methods polymorphically.
  • You can configure your application at runtime by initializing the singleton instance with the class you need.

具有说,在大多数情况下,我认为额外的复杂性过高,并且很少在我编写的代码中使用该模式。但是当您设计供其他人使用的API时,我会看到它的价值。

Having said that, in most cases I consider the additional complexity excessive and rarely use the pattern in the code I write. But I can see its value when you design an API that others will use.

这篇关于Singleton-为什么使用类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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