Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
664 views
in Technique[技术] by (71.8m points)

design-patterns - 静态类和单例模式之间的区别?(Difference between static class and singleton pattern?)

What real (ie 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?

(还有其他区别吗?)

  ask by Jorge Córdoba translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What makes you say that either a singleton or a static method isn't thread-safe?

(是什么让您说单例或静态方法不是线程安全的?)

Usually both should be implemented to be thread-safe.

(通常,两者都应实现为线程安全的。)

The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces (or derive from useful base classes, although that's less common, in my experience), so you can pass around the singleton as if it were "just another" implementation.

(单例和一堆静态方法之间的最大区别在于,单例可以实现接口(或从有用的基类派生,尽管根据我的经验,这种情况不那么常见),因此您可以将单例当作“只是另一个实施。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...