What are the main benefits of using the Singleton class for the utility class?

I saw many references to Singleton and saw how they were used for "utility" functions, such as general checks, conversions, etc. Assuming there is no need to refer to the โ€œIโ€, is there any advantage to using a singleton rather than just implementing a utility class using class methods and optional static variables? Style? Representation? Concurrency?

In other words, instead of calling foo through an instance of MySingleton:

BOOL b = [[MySingleton sharedInstance] foo:xyz]; 

You can call foo through the method of the MyUtility class:

 BOOL b = [[MyUtility class] foo:xyz]; 

The implementation of any of the classes is implied, and I will refuse to list them if this is normal.

+4
source share
1 answer

There are no advantages, except that you do not accept any references to yourself. And โ€œyouโ€ I do not mean your implementation of MyUtility , I mean those classes that are called MyUtility - in other words, you end up investing knowledge about the implementation of MyUtility in other classes, in contrast to the usual object-oriented principles.

However, the same argument works differently. If you assume that an instance requires an instance, then MyUtility should act as an instance. If you call class methods, then it can pass them to one instance of Singleton.

However, since instances are the norm, and not the exception, stylistically less than imagined, to expect to speak with the instance.

0
source

Source: https://habr.com/ru/post/1439901/


All Articles