什么是Singleton Pattern的真实世界应用 [英] What are the real world applications of Singleton Pattern

查看:190
本文介绍了什么是Singleton Pattern的真实世界应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设计模式:何时使用单身人士

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.

这篇关于什么是Singleton Pattern的真实世界应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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