单例模式在现实世界中有哪些应用? [英] What are the real world applications of the singleton pattern?

查看:172
本文介绍了单例模式在现实世界中有哪些应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设计模式:何时应使用单例?

class Singleton
{
    private static Singleton instance;

    private Singleton() {}

    public static Singleton Instance
    {
        get
        {
            if (instance == null)
                instance = new Singleton();

            return instance;
        }
    }
}


推荐答案

简单。单身人士做什么?

Simple. What does a singleton do?


  • 它提供对对象实例的全局访问,并且

  • 它保证最多只能创建一个实例。

  • It provides global access to an instance of an object, and
  • It guarantees that no more than one instance of that type can ever be created.

因此,您使用当您需要两者都需要一个单例时。

So you use a singleton when you need both of these things.

这很少见。
全球人士一般来说是不好的。我们倾向于尽可能避免使用它们。
围绕如果存在多个实例,这是一个错误的假设构建应用程序很危险,因为您通常会发现假设不成立。也许您希望能够在本地创建多个实例以进行缓存。也许事实证明您需要多个数据库,多个日志,或者线程性能要求您为每个线程提供自己的实例。

And that is rare. Globals are generally speaking, bad. We tend to avoid them when possible. And building your application around the assumption that "if more than one instance exists, it is an error" is dangerous, because you typically find out the assumption didn't hold. Perhaps you want to be able to create multiple instances locally, for caching purposes. Perhaps it turns out you need more than one database, more than one log, or perhaps threading performance requires you to give each thread its own instance.

无论如何,您都需要无需强制执行可能只存在一个实例的假设。如果只需要一个实例,只需创建一个实例即可。但是请让构造函数公开可见,以便在有必要的情况下创建更多实例。

In any case, you don't need to enforce the "only one instance may exist" assumption. If you only need one instance, simply create only one instance. But leave the constructor publicly visible so that more instances can be created if it turns out to be necessary.

单例提供的功能实际上是负数。通常,我们不会希望将数据全局可见,也不会希望无缘无故地失去灵活性。

In other words, both of the features offered by a singleton are actually negatives. In general, we don't want our data to be globally visible, and we don't want to take away flexibility for no reason.

如果确实需要单例提供的功能之一,请实现该功能,而无需其他功能。如果您需要全局可访问的内容,请将其设为全局。不是单身。而且,如果您确实需要强制实施仅一个实例(我想在任何可能的情况下都不需要这样做),那么就可以在没有全局可见性的情况下实施该实例。

If you do need one of the features offered by a singleton, implement that one feature, without the other. If you need something to be globally accessible, make it a global. Not a singleton. And if you do need to enforce that only one instance will exist (I can't think of any plausible situations where you'd want this), then implement that, without the global visibility.

我所见过的在现实世界中唯一的单例应用是一位架构师已经阅读了GoF的书,并决定将设计模式塞入任何地方。,或者某些陷入80年代的程序员并非如此。对整个面向对象事物感到满意,并希望进行程序编码,这意味着将数据存储为全局变量。单例听起来就像是一种 OOP方式,使全局变量不会被大吼大叫。

The only real world application of singletons I've ever seen has been "an architect has read the GoF book, and decided to cram design patterns in everywhere.", or "some programmer stuck in the 80's isn't comfortable with the whole "object oriented" thing, and wants to code procedurally, which means storing data as globals. And singletons sounds like an "OOP" way to make globals without getting yelled at".

关键是单例混合了两个非常不同且很少需要的职责。通常,您要在任何给定对象中最多 个。

The key point is that a singleton mixes two, very different, and each very rarely needed, responsibilities. Typically, you want at most one of those in any given object.

这篇关于单例模式在现实世界中有哪些应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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