静态类的优点超过使用Singleton

静态类的优点超过使用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