具有静态成员的类 vs 单例 [英] Class with static members vs singleton

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

问题描述

只有静态成员的类不就是一种单例设计模式吗?有这样的课有什么坏处吗?详细的解释会有所帮助.

Isn’t a class with only static members a kind of singleton design pattern? Is there any disadvantage of having such a class? 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):

  • 更自然的访问语法
  • 单身人士没有名字
  • 更容易继承

我必须承认,我不觉得这些特别引人注目.另一方面,单态绝对不比单态更糟.

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.

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

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