Monday, August 20, 2018

Difference between static class and singleton pattern?



What real (i.e. practical) difference exists between a static class and a singleton pattern?



Both can be invoked without instantiation, both provide only one "Instance" and neither of them is thread-safe. Is there any other difference?



Answer



What makes you say that either a singleton or a static method isn't thread-safe? Usually both should be implemented to be thread-safe.



The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces (or derive from useful base classes, although that's less common, in my experience), so you can pass around the singleton as if it were "just another" implementation.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...