什么是Singleton模式的实际应用 [英] What are the real world applications of Singleton Pattern

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

问题描述

复制



在设计模式:当使用Singleton

 类辛格尔顿
{
私人静态Singleton实例;

私人辛格尔顿(){}

公共静态Singleton实例
{
得到
{
如果(例如== NULL)
实例=新辛格尔顿();

返回实例;
}
}
}


解决方案

简单。什么是单身吗?




  • 它提供了一个对象实例的全局访问,和

  • 它保证该类型的不超过一个实例可以曾经创建。



所以,你使用当你需要的这两个东西单身。



这是罕见的。
全局都一般来说,坏的。我们倾向于避免他们在可能的情况。
和建筑周围的假设,如果有多个实例存在,这是一个错误您的应用程序是危险的,因为你通常发现的假设并不成立。也许你希望能够在本地创建多个实例,用于缓存的目的。也许原来需要多个数据库,多个日志,或者线程性能要求您为每个线程提供自己的实例。



在任何情况下,你不需要执行只有一个实例可能存在的假设。如果你只需要一个实例,只需创建只有一个实例。但离开构造公开可见,让更多的实例可以被创建的如果事实证明有必要



在换句话说,两者的由单提供的功能实际上是阴性。一般情况下,我们不这样做的希望的我们的数据是全局可见的,我们不的希望的带走无缘无故的灵活性。



如果你确实需要由一个单独提供的功能之一,实施一个特征,没有其他。如果你需要的东西,是全球访问,使其成为一个全球性的。不是单例。如果你确实需要强制执行,只有一个实例会存在(我想不出任何可行的情况下,您会想这一点),然后实现,如果没有全球知名度。



我见过一直以建筑师已经阅读GoF的书,并决定在补习班设计模式无处不在。,或单身人士的唯一现实世界的应用程序的一些程序员停留在80年代的不舒适与整个面向对象的事情,并希望在程序代码,这意味着存储数据的全局变量。而单身听起来像一个面向对象的方式,使全局而不在获得喊道。



的关键点是一个单身混合二,非常不同,而且每个很少需要,责任。通常情况下,你要的最多的那些在任何特定的对象之一。


Duplicate

On Design Patterns: When to use the Singleton

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.

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模式的实际应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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