Is Monostate the good cousin of the evil Singleton

2019-03-20 14:15发布

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 cousin Monostate is less famous and less used. What is your opinion of Monostate? Good or just as evil? Is it a better alternative to using Singleton? Would you also discourage its use as you would with Singleton?

8条回答
神经病院院长
2楼-- · 2019-03-20 14:33

how about not using patterns just because?

update: abuse of singleton occurs because people add the pattern w/o justification. It often adds arbitrary constraint to no benefit. using monostate w/o justification is perhaps less harmful, but no more justified. if the universe won't collapse if there's more than one instance, than don't enforce it with a pattern - just create only one instance.

查看更多
时光不老,我们不散
3楼-- · 2019-03-20 14:43

According to this definition of Monostates, is this note:

"Monostates are evil in the same way that SingletonsAreEvil."

I don't necessarily agree. The basic premise is that Singletons and Monostates are global variables, and since Globals are evil, so too must be Singletons and Monostates.

The problem with this line of reasoning is that it doesn't take into account WHY globals are evil. Globals are evil primarily because they are variables that can be accessed outside the local scope accidentally, similarly to how non-typed languages often allow variables to be created simply by referencing them.

Singletons and Monostates can't cause the same kinds of problems, because it's virtually impossible to "accidentally" reference a global static, call it's instance method, then utilized the instance.

In other words, globals are evil because they cause subtle and hard to see problems. Singletons and Monostates do not cause the same kinds of problems, so I don't see them as evil for the same reasons, which is where most people seem to go wrong in this argument.

Now, can singletons and monostates cause other kinds of problems? Sure. Apparently, the TDD people hate them because they are hard to test correctly with automated testing. Ok, fine, but for those people that don't use TDD, those problems don't exist.

Of course singletons can be misused. People that use them simply to avoid passing an instance around are misusing them. I think Monostates are better for that than a singleton.

Many people propose factory patterns instead of singletons, but I feel that Factories are just fancy singleton generators. No, there is no static "instance" method, but it basically does the same thing (when a factory creates a single instance object that is). Factory.Create is no different from Singleton.Instance.

EDIT:

One aspect of Singletons and Monostates that is comparable to Globals is that they are shared, and thus not thread safe. While this is a concern if you plan to do multi-threaded apps, if you know this you can take steps to serialize access to the shared objects. So this is probably the only area where, generally, all three types can be thought of as causing troubles.

查看更多
Summer. ? 凉城
4楼-- · 2019-03-20 14:43

Here is an excerpt from page 127 of Design Patterns: Elements of Reusable Object-Oriented Software by Gamma, Helm, Johnson, and Vlissides.

Use the Singleton pattern when

  • there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point
  • when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code

Does it really make singletons evil just because they are misused and misunderstood?

查看更多
混吃等死
5楼-- · 2019-03-20 14:44

Um, monostate is Singleton... so it has the exact same problems.

  • testing
  • hiding dependencies
  • inflexible
  • thread safety
  • global state makes it difficult to ensure correctness
查看更多
ら.Afraid
6楼-- · 2019-03-20 14:50

Why are you assuming that people would discourage the use of Singletons? Making sweeping generalizations about Singletons is like making sweeping generalizations about gotos, global variables and so on. There is a place for all of them. None of these things are "evil", and their misuse doesn't make them bad to use, it just means they need to be used properly.

查看更多
老娘就宠你
7楼-- · 2019-03-20 14:50

The biggest problem I have with Monostate is that it can lead to confusion on the part of those using it. Generally speaking, if you create a new instance of an object, you expect it to be just that, a new instance, with its own state and lifecycle. I generally consider unexpected behaviors to be a bad thing, so I prefer the singleton, just because you know what your getting when you use it.

As for whether or not either pattern is evil, singletons tend to get misused, but then so do all of the other patterns, switch statements, for loops, or just about anything else you care to mention.

查看更多
登录 后发表回答