static class and singleton

前端 未结 5 1748
谎友^
谎友^ 2020-12-14 21:04

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 wo

5条回答
  •  醉话见心
    2020-12-14 21:31

    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.

提交回复
热议问题