静态类vs单例类 [英] static class vs singleton class

查看:95
本文介绍了静态类vs单例类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个话题已被一遍又一遍地讨论和杀害,但我仍有一个疑问,我希望有人可以帮助我或指导我在SO上预先存在的帖子。

I know this topic has been discussed and killed over and over again, but I still had one doubt which I was hoping someone could help me with or guide me to a pre-existing post on SO.

在传统的C中,静态变量存储在数据段中,局部变量存储在堆栈中。与局部变量相比,我认为这会使静态变量的存储和维护成本更高。对吗?

In traditional C, static variables are stored in data segments and local variables are stored in the stack. Which I would assume will make static variables more expensive to store and maintain when compared to local variables. Right?

当尝试用Java或C#来理解时,与单例类相比,这对于静态类是否有利?由于整个类在类初始化之前被加载到内存中,所以除非我们有小的内联函数,否则我看不出它有什么优势。

When trying to understand in terms of Java or C#, would this be dis-advantage for static classes when compared to singleton class? Since the entire class is loaded into memory before class initialization, I don't see how it can be an advantage unless we have small inline-able functions.

我喜欢Singleton课程,并且不愿意看到它成为一种反模式,我仍然在寻找它带来的所有优势......然后松散其中包括线程安全的论点。

I love Singleton classes, and would hate to see it become an anti-pattern, I am still looking for all the advantages that come with it...and then loose to the argument of thread-safety among others.

-Ivar

推荐答案

与C不同,Java类定义中的 static 关键字仅仅意味着,这只是一个普通的类,就像任何其他类一样,但它恰好被声明了在另一个类中组织代码。换句话说,以下两种声明方式之间没有任何行为差异*:

a)

Different from C, the static keyword in Java class definition merely means, This is just a normal class like any other class, but it just happens to be declared inside another class to organize the code. In other words, there is no behavioral difference whatsoever between the following 2 way of declaration*:
a)

class SomeOtherClass {
    static class Me {
        // If you "upgrade" me to a top-level class....
    }
}

b)

class Me {
    // I won't behave any different....
}

当第一次使用类时,类定义被加载到内存中,对于静态和非静态类都是如此。内存的使用方式也没有区别。在较旧的JVM中,对象始终存储在堆中。现代JVM 在可能和有益的情况下在堆栈上分配对象,但这种优化对编码器是透明的(不可能通过代码影响这种行为),并且使用 static 关键字对此行为没有任何影响。

Class definitions are loaded to memory when the class is used for the first time, and this is true for both "static" and "non-static" classes. There are no difference in how memory will be used, either. In older JVMs, objects were always stored in heap. Modern JVMs do allocate objects on stack when that is possible and beneficial, but this optimization is transparent to the coder (it is not possible to influence this behavior via code), and use of the static keyword does not have any effect on this behavior.

现在,回到原来的问题,正如我们所看到的,我们真的无法比较Java中的静态类和Singleton,因为它们在Java中是完全不同的概念(我也不确定静态类与Singleton相比如何,但我会在这个答案中关注Java)。 Java中的 static 关键字被重载并具有很多含义,因此可能会令人困惑。

Now, back to your original question, as we have seen we really can't compare static classes and Singleton in Java as they are completely different concept in Java (I'm also not sure how static classes would compare with Singleton, but I will focus on Java in this answer). The static keyword in Java is overloaded and has many meanings, so it can be confusing.

Singleton 会自动成为反模式吗?我不这么认为。 Singleton的滥用是,但Singleton模式本身可以有很多好的用途。它恰好被滥用了很多。如果您有正当理由使用Singleton模式,使用它没有任何问题。

Is Singleton automatically an "anti-pattern"? I don't think so. Abuse of Singleton is, but the Singleton pattern itself can have many good uses. It just happens to be abused a lot. If you have legitimate reason to use the Singleton pattern, there is nothing wrong in using it.





*注意:为什么要写 static ,你可能会问。事实证明,非静态嵌套类有其自身有些复杂的内存管理含义,除非你有充分的理由,否则通常不鼓励使用它(请参阅其他问题获取更多信息)。



*Note: Why write static at all, you might ask. It turns out "non-static" nested classes have their own somewhat complicated memory management implication, and its use is generally discouraged unless you have a good reason (pls refer to other questions for more info).

class SomeOtherClass {
    Stuff stuff;
    class Me {
        void method(){
            // I can access the instance variables of the outer instance 
            // like this:
            System.out.println(SomeOtherClass.this.stuff);
            // Just avoid using a non-static nested class unless you 
            // understand what its use is!
        }
    }
} 

这篇关于静态类vs单例类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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