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

问题描述

不是一个具有所有静态成员/方法的类是一种单例设计模式?特别是有这样的类有什么不利吗?详细解释会有帮助。

解决方案

这种类称为 - 它与单例有些不同。



为什么要使用单稳态单身?在他们的中模式,Bell& Crawford建议三种原因(由我改写):




  • 更自然的存取语法



  • 我必须承认,我没有找到任何这些特别引人注目。另一方面,单状态绝对不比单例更糟。


    Isn't a class with all static members/methods a kind of singleton design pattern? Is there any disadvantage in particular of having such classes? A detailed explanation would help.

    解决方案

    This kind of class is known as a monostate - it is somewhat different from a singleton.

    Why use a monostate rather than a singleton? In their original paper on the pattern, Bell & Crawford suggest three reasonns (paraphrased by me):

    • More natural access syntax
    • singleton lacks a name
    • easier to inherit from

    I must admit, I don't find any of these particularly compelling. On the other hand, the monostate is definitely no worse than the singleton.

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

08-18 13:55