静态类和单例模式的区别? [英] Difference between static class and singleton pattern?

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

问题描述

静态类和单例模式之间存在什么真正的(即实际的)区别?

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?

推荐答案

是什么让您说单例方法或静态方法不是线程安全的?通常应该实现线程安全.

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.

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

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