Singleton:应该如何使用 [英] Singleton: How should it be used

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

问题描述

编辑:
从另一个问题,我提供了一个回答,其中有许多关于单身的问题/答案的链接:更多关于单例的信息:



所以我已经阅读了线程单身:好的设计或拐杖?

和参数仍然愤怒。



我看到Singletons是一个设计模式(好和坏)。


Singleton的问题不是模式,而是用户(对不起大家)。每个人和他们的父亲认为他们可以正确实施一个(从我进行的许多访谈,大多数人不能)。因为每个人都认为他们可以实现一个正确的单例,他们滥用了模式,并在不适当的情况下使用它(用单数替换全局变量!)。



需要回答的问题是:




  • 您应该何时使用Singleton

  • 正确实现Singleton



我希望这篇文章是我们可以集中在一个地方(而不是google和搜索多个站点)一个权威的时间源(然后如何)正确使用Singleton。也适当的是一个反用法和常见的错误实现的列表,解释它们为什么它们不能工作,以及好的实现它们的弱点。




所以让球滚动:

我会举起我的手,说这是我使用,但可能有问题。

我喜欢斯科特·迈尔斯处理主题他的书有效C ++


使用单身人士(不多)的良好情况:




  • 记录框架

  • 线程回收池




  / * 
* C ++ Singleton
*限制:单线程设计
*参见:http://www.aristeia。 com / Papers / DDJ_Jul_Aug_2004_revised.pdf
*对于在多线程应用程序中锁定的问题
*
*限制:
*如果你在一个析构函数中使用这个Singleton另一个单例(B)
*这个单例(A)必须在(B)
*的构造函数之前完全构造。
* /
class MySingleton
{
private:
//私人构造函数
MySingleton();
//停止编译器生成方法复制对象
MySingleton(MySingleton const& copy); // Not Implemented
MySingleton& operator =(MySingleton const& copy); // Not Implemented

public:
static MySingleton& getInstance()
{
//唯一的实例
//保证被延迟初始化
//保证它会被正确销毁
static MySingleton instance;
return instance;
}
};

确定。

: - )

解决方案

你们都错了。
阅读问题。
回答:



使用Singleton如果:




  • 需要在系统中只有一个类型的对象。



如果:

$ b $不使用Singleton b

  • 如果你想保存记忆

  • 如果你想尝试新的


  • 因为其他人都在做这项工作(请参阅货物





  • b

    如何创建最好的单身人士:




    • 越小越好。

    • 确保它是线程安全的

    • 确保它不会为空

    • 确定它只创建一次

    • 延迟或系统初始化?满足您的要求

    • 有时操作系统或JVM为您创建单例(例如在Java中,每个类定义都是单例)

    • 提供析构函数或以某种方式了解如何处理资源

    • 使用少量内存


    Edit: From another question I provided an answer that has links to a lot of questions/answers about singletons: More info about singletons here:

    So I have read the thread Singletons: good design or a crutch?
    And the argument still rages.

    I see Singletons as a Design Pattern (good and bad).

    The problem with Singleton is not the Pattern but rather the users (sorry everybody). Everybody and their father thinks they can implement one correctly (and from the many interviews I have done, most people can't). Also because everybody thinks they can implement a correct Singleton they abuse the Pattern and use it in situations that are not appropriate (replacing global variables with Singletons!).

    So the main questions that need to be answered are:

    • When should you use a Singleton
    • How do you implement a Singleton correctly

    My hope for this article is that we can collect together in a single place (rather than having to google and search multiple sites) an authoritative source of when (and then how) to use a Singleton correctly. Also appropriate would be a list of Anti-Usages and common bad implementations explaining why they fail to work and for good implementations their weaknesses.


    So get the ball rolling:
    I will hold my hand up and say this is what I use but probably has problems.
    I like "Scott Myers" handling of the subject in his books "Effective C++"

    Good Situations to use Singletons (not many):

    • Logging frameworks
    • Thread recycling pools

    /*
     * C++ Singleton
     * Limitation: Single Threaded Design
     * See: http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf
     *      For problems associated with locking in multi threaded applications
     *
     * Limitation:
     * If you use this Singleton (A) within a destructor of another Singleton (B)
     * This Singleton (A) must be fully constructed before the constructor of (B)
     * is called.
     */
    class MySingleton
    {
        private:
            // Private Constructor
            MySingleton();
            // Stop the compiler generating methods of copy the object
            MySingleton(MySingleton const& copy);            // Not Implemented
            MySingleton& operator=(MySingleton const& copy); // Not Implemented
    
        public:
            static MySingleton& getInstance()
            {
                // The only instance
                // Guaranteed to be lazy initialized
                // Guaranteed that it will be destroyed correctly
                static MySingleton instance;
                return instance;
            }
    };
    

    OK. Lets get some criticism and other implementations together.
    :-)

    解决方案

    All of you are wrong. Read the question. Answer:

    Use a Singleton if:

    • If you need to have one and only one object of a type in system

    Do not use a Singleton if:

    • If you want to save memory
    • If you want to try something new
    • If you want to show off how much you know
    • Because everyone else is doing it (See cargo cult programmer in wikipedia)
    • In user interface widgets
    • It is supposed to be a cache
    • In strings
    • In Sessions
    • I can go all day long

    How to create the best singleton:

    • The smaller, the better. I am a minimalist
    • Make sure it is thread safe
    • Make sure it is never null
    • Make sure it is created only once
    • Lazy or system initialization? Up to your requirements
    • Sometimes the OS or the JVM creates singletons for you (e.g. in Java every class definition is a singleton)
    • Provide a destructor or somehow figure out how to dispose resources
    • Use little memory

    这篇关于Singleton:应该如何使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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