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

本文是小编为大家收集整理的关于静态类和单子的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

推荐答案

这种类被称为 monostate - 它与单例有些不同.

为什么使用单态而不是单态?在他们的 原始论文中 关于模式,Bell &Crawford 提出了三个原因(由我解释):

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

我必须承认,我没有发现其中任何一个特别引人注目.另一方面,单态绝对不比单态差.

本文地址:https://www.itbaoku.cn/post/627774.html

问题描述

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.