wnagrodzki / iOSProgrammingGuidelines

2 stars 0 forks source link

Accessing Singleton #11

Closed wnagrodzki closed 5 years ago

wnagrodzki commented 6 years ago

Do not access singleton instance directly via static method - Singleton.sharedInstance(). Decouple from it with dependency injection.

If a singleton is used directly there is no way to perform unit tests in isolation from it. Moreover, it may introduce unexpected shared state between unit tests if they are executed in one run.

wnagrodzki commented 5 years ago

@moskacz @pwetrifork What do you think?

pwetrifork commented 5 years ago

Maybe it's dependency injection that should be mentioned here instead of a protocol. I would say using protocols to enable unit testing should be formulated as a separate guideline, it's not that you only want it for singletons.

wnagrodzki commented 5 years ago

@pwetrifork I agree, using protocol is not the only way to decouple from singleton instance (one could provide a singleton subclass to act as a mock). Text has been updated.

Moskacz commented 5 years ago

@wnagrodzki agree