Difference between static class and singleton patt

2018-12-31 01:09发布

What real (i.e. practical) difference exists between a static class and a singleton pattern?

Both can be invoked without instantiation, both provide only one "Instance" and neither of them is thread-safe. Is there any other difference?

30条回答
听够珍惜
2楼-- · 2018-12-31 01:54

In many cases, these two have no practical difference, especially if the singleton instance never changes or changes very slowly e.g. holding configurations.

I'd say the biggest difference is a singleton is still a normal Java Bean as oppose to a specialized static-only Java class. And because of this, a singleton is accepted in many more situations; it is in fact the default Spring Framework's instantiation strategy. The consumer may or may not know it's a singleton being passed around, it just treat it like a normal Java bean. If requirement changes and a singleton needs to become a prototype instead, as we often see in Spring, it can be done totally seamlessly without a line of code change to the consumer.

Someone else has mentioned earlier that a static class should be purely procedural e.g. java.lang.Math. In my mind, such a class should never be passed around and they should never hold anything other than static final as attributes. For everything else, use a singleton since it's much more flexible and easier to maintain.

查看更多
怪性笑人.
3楼-- · 2018-12-31 01:55

I read the following and think it makes sense too:

Taking Care of Business

Remember, one of the most important OO rules is that an object is responsible for itself. This means that issues regarding the life cycle of a class should be handled in the class, not delegated to language constructs like static, and so on.

from the book Objected-Oriented Thought Process 4th Ed.

查看更多
几人难应
4楼-- · 2018-12-31 01:55

There is a huge difference between a single static class instance (that is, a single instance of a class, which happens to be a static or global variable) and a single static pointer to an instance of the class on the heap:

When your application exits, the destructor of the static class instance will be called. That means if you used that static instance as a singleton, your singleton ceased working properly. If there is still code running that uses that singleton, for example in a different thread, that code is likely to crash.

查看更多
情到深处是孤独
5楼-- · 2018-12-31 01:59

From a client perspective, static behavior is known to the client but Singleton behavior can be completed hidden from a client. Client may never know that there only one single instance he's playing around with again and again.

查看更多
浮光初槿花落
6楼-- · 2018-12-31 02:00
  1. Lazy Loading
  2. Support of interfaces, so that separate implementation can be provided
  3. Ability to return derived type (as a combination of lazyloading and interface implementation)
查看更多
呛了眼睛熬了心
7楼-- · 2018-12-31 02:01

One notable difference is differed instantiation that comes with Singletons.

With static classes, it gets created by the CLR and we have not control on it. with singletons, the object gets instantiated on the first instance it's tried to be accessed.

查看更多
登录 后发表回答