哪个是更好的单例实现? [英] Which is a better singleton implementation?

查看:79
本文介绍了哪个是更好的单例实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Singleton模式的以下两种实现方式:

I was looking at these two implementations of the Singleton Pattern:

 Class Customer {

int x;
static int count = 0;

private Customer()
{
    x = 1;
}

public static Customer GetCustomer()
{
    if(count==0)
    {
        count++;
        return new Customer();
    }
    else
    {
        return null;
    }
}
}

实现1,其中构造函数如果该类已经实例化一次,则不调用。或

Implementation 1 in which the constructor is not called if the class is already instantiated once. OR

 Class Customer{

int x;
static int count = 0;
public Customer()
{
    if(count == 0)
    {
        x = 1;
        count++;
    }
    else
    {
        return;
    }
}

实施例2,无论天气如何,调用构造函数该类是否已经实例化一次。我在网上观看了一个视频讲座,该视频说实现2不是首选,因为它虽然没有第二次实例化该对象,但却为构造函数分配了内存。我知道Java具有自动垃圾回收功能,但是只是想知道我在视频讲座中观看的内容是否相关。

Implementation 2 in which the constructor is called irrespective of weather the class is already instantiated once or not. I watched a video lecture online that says Implementation 2 is not preferred as it allocates memory for the constructor although the object is not instantiated for the second time. I am aware Java has automatic Garbage Collection, but just wanted to know if what I watched in the video lecture is relevant.

推荐答案

有些人会说 no 实施单例是正确的,但是我在那个阵营中还不是很清楚。

There are some that would say that no implementation of a singleton is the correct one, but I'm not quite in that camp.

人经常会不好用它们(例如,作为上帝的对象),但它们仍然有自己的位置(在我的观点中,与这个答案无关)。

People often tend to use them badly (as a God object, for example) but they still have their place (in my opinion, which has little to do with this answer).

出于这个答案的目的,我假设您已经做出了需要单身人士的正确决定,但是我强烈建议您仔细阅读其潜在的问题-可能有

For the purposes of this answer, I'm going to assume you've made the right decision in needing a singleton, but I do urge you to read up on its potential problems - there may well be a better way to achieve your ends.

话虽如此,我不确定您的代码示例是否正确。单例应该返回一个且只有一个实例,必要时创建一个实例,如果没有,则为您提供先前创建的实例。

Having said that, I'm not sure if your code samples are correct anyway. A singleton is supposed to return one and only one instance, creating one if necessary, giving you the previously created one if not.

我看不到您的第一个代码示例履行合同。它将在第一次调用时为您提供一个新的代码,然后之后您将一无所获。

I fail to see how your first code sample honors that contract. It'll give you a new one the first time it's called, then it will give you nothing after that.

第二个代码示例似乎根本不会阻止多个对象。

And your second code sample doesn't seem to prevent multiple objects at all.

所以我会非常仔细地考虑是否 如果这是他们提供的教育质量,那么您想继续观看该视频。

So I'd be thinking very carefully about whether you want to continue watching that video, if this is the quality of the education they deliver.

无论如何,我首选只构造一次的对象,因为您只应该拥有该类的一个对象。换句话说,一个静态的 getMe()调用已正确同步,以防止竞争条件允许创建多个对象,它第一次创建一个新对象,在随后的调用中返回该相同对象。

In any case, I prefer the one that only constructs once, since you're only supposed to have one object of that class. In other words, a static getMe() call that is properly synchronised to prevent race conditions allowing creation of more than one Me object, and that creates a new object the first time, returning that same object on subsequent calls.

用伪代码,类似于:

class Me {
    private Me() {};

    private static Me *loner = NULL;

    public static synchronised Me *getMe() {
        if loner == NULL
            loner = new Me();
        return loner;
    }
}

这篇关于哪个是更好的单例实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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