静态类和单例 [英] static class and singleton

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

问题描述

不是所有静态成员/方法的类都是一种单例设计模式?有没有什么缺点,特别是有这样的类?详细的解释将有所帮助。

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.

推荐答案

这种类被称为 monostate - 它与单身人士有所不同。

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):


  • 更自然的访问语法

  • singleton缺少

  • 更容易继承自

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

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.

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

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