Is Monostate the good cousin of the evil Singleton?

前端 未结 8 1645
梦如初夏
梦如初夏 2021-02-08 23:21

Singleton is definitely one of the most misused and abused patterns out there. Many of us have been infected with Singletonitis at one point or another. Curiously, its close c

8条回答
  •  后悔当初
    2021-02-08 23:40

    The deal with Monostate is that you need to know less about the implementation of the object in order to use it. In other words you save a few keystrokes because you don't have to call the objects getInstance method ( Singleton s = Singleton::getInstance; s.Method(); ) to get a reference to the object, you simply use normal language constructs ( Monostate ms; ms.Method(); ). A fine line indeed.

    Every programming langauge construct can be labeled evil because every programming language contrsuct can be abused.

    When used reasonably, I see neither one being "evil" or "good."

提交回复
热议问题