静态类的优点超过使用Singleton [英] Advantage of Static class over use of Singleton

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

问题描述


单身人士有什么问题?

单身人士:好的设计还是拐杖?

Singleton:How应该使用它

这么糟糕的是单身人士






您可以找到使用Singleton静态类。但肯定有一些情况下,最好在Singleton之前使用一个静态类。他们是什么?


You can find numerous reasons for using a Singleton over a Static class. But there must surely be some situations where it is better to use a static class before a Singleton. What are they?

推荐答案

在以下情况下可以使用静态类:

You can use static class when:

1)所有的方法都是实用程序(好的例子 - 类Math)

1) all its methods are utilities (nice example - class Math)

2)你不想处理保留你的实例从垃圾收集器,但我最好使用singleton有

2) you don't want to deal with preserving your instance from garbage collector (in applets), but I would better use singleton there

3)你绝对确定它不会在将来变成有状态,你确信你总是只需要一个该类的实例

3) you are absolutely sure that it wouldn't become stateful in the future and you are sure that you will always need only one instance of that class

如果你使用singleton,并且在某一时刻你意识到你需要几个实例,那么你的singleton可以很容易地转换为多音节,但你会有静态类

If you are using singleton and in one moment you realize that you need several instances then your singleton easily can be transformed to multitone, but you'll have a problem with static class

这篇关于静态类的优点超过使用Singleton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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